0

I'm trying to do I/O redirection in the open shell on repl.it. Taking the input from one file to run through the program. Outputting to a new file but nothing shows up? I'm only used to doing it from Windows using the CMD.

Shell:

~/maybe-lab6and7$ clang-7 -pthread -lm -o main main.c < address.txt > 
open.txt
~/maybe-lab6and7$ 
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 1
    IF your `main.c` is really a compiled program AND you have changed the permissions for execute access with `chmod +x main.c`, then your current working directory is not included in your PATH variable, either specify a "full-path" to the program, i.e. `/full/path/to/main.c` or a relative path like `./main.c ...` Did you just type in your exercise code as `main.c` and then try running your scripts above? If yes, you need to complie it first maybe like `cc main.c -o myFirstProg`. Good luck. – shellter Nov 23 '20 at 05:56
  • 1
    You've destroyed whatever was in `address.txt` by using `> address.txt`. You're trying to read from a non-existent file. It's as if you have the meanings of `>` and `<` reversed in your thinking. You have to compile your program before it will run; you can't run `main.c` if it contains C source code. – Jonathan Leffler Nov 23 '20 at 06:01
  • It's not reversed in Windows command prompt... – Passer By Nov 23 '20 at 06:43
  • @HippiiDippii : I don't know `clang-7`, but from the context, I would assume that it is a C compiler. I find it strange that you try to feed something into the stdin, and probably it is simply ignoring it. If `open.txt` is empty afterwards, it means that the compiler did not write anything to stdout either. Did you check stderr? Did you check the exit code? Has `main` be generated? – user1934428 Nov 23 '20 at 06:47
  • 1
    strongly suggest posting your source code so we can see what your trying to do. – user3629249 Nov 23 '20 at 06:53
  • Main has been generated and this is what it shows clang-7 -pthread -lm -o main main.c ./main. In visual studios, after building the program, I would run the file through the .exe but with repl.it I'm confused on how to do that – HippiiDippii Nov 23 '20 at 06:55

1 Answers1

1

After compiling and linking your program, if no compile problems and no link problems occur, you will have an executable in the current directory. (Lets say the executable is named: main.)

Then, after changing the program permissions so main is executable,
you can execute the program similar to:

./main  < sourcedata.txt  > destinationdata.txt

The result is that main can read the sourcedata.txt file from stdin and the output from the program (instead of being displayed on the terminal) will be written to destinationdata.txt.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • @Yunnosch, always a possibility, However that is not the question the OP ask – user3629249 Nov 23 '20 at 06:56
  • I'm using repl.it any advice on that would be nice. I'm aware that you have to use an executable file as I've done it before with visual studios/ windows. But repl.it is slightly different. – HippiiDippii Nov 23 '20 at 06:58
  • Please explain how repl.it changes things. This answer seems to match the question you asked. If there is a relevant other problem, on top of the mistakes you made in the way of using main.c, you might have to create a separate new question on that problem. Because here you would risk a "moving target question" with too much editing. – Yunnosch Nov 23 '20 at 07:01
  • @Yuunosch this is the result that I get in shell on repl.it: ~/maybe-lab6and7$ main < address.txt > open.txt bash: main: command not found – HippiiDippii Nov 23 '20 at 07:04
  • If `repl,it` is linux based, then after compiling + linking, the resulting program can be executed via `./main` OR you can use `chmod +x main` to make it executable, then you will not need the `./` infront of `main` – user3629249 Nov 23 '20 at 07:07
  • The executable should already be marked executable by the compiler? The executable permission isn't related to needing `./`, it's always required unless the executable is on the path – Alan Birtles Nov 23 '20 at 07:41
  • in C, on linux, the output from the compiler+linker does not contain the `execute` permission. It can be executed via `./nameOfExecutable` Much more useful to change the permissions to include the `executable permission, then it can be executed via: `nameOfExecutable` – user3629249 Nov 23 '20 at 07:44
  • 1
    @user3629249 have you tried this on Linux? having to use `./` is nothing to do with the executable permission, if a program isn't marked executable you won't be able to execute it at all – Alan Birtles Nov 23 '20 at 07:59
  • Actually, @AlanBirtles, it does not have to have the `x` permission to be executed, (assuming the file is otherwise able to be executed.) the leading `./` enables execution of the file, even though it does not have the `x` permission – user3629249 Nov 23 '20 at 19:15
  • not on any Linux installation I've ever used: https://imgur.com/QWlAWeh – Alan Birtles Nov 23 '20 at 20:47