C input and output redirection error? - c

I am encountering this weird message at the moment with my file.
I have a very basic C file that reads from a file and outputs upon request.
I am running the following command in Linux:
filename <filenametest.txt >filenameoutput
and it brings the following message:
filename: command not found
Any idea why it's not working?
Really frustrating.

Assuming that filename is the name of the executable generated on compiling your C code, maybe you should try using
./filename < filename.txt > filenameoutput.
Since filename is not in the search path by default.

Related

How to allocate an input file instead of the stdin in an Eclipse CDT application?

My application is a simple executable used from the command line and takes stdin as input and stdout as output, so it behaves like many GNU tools.
To test it, I want to set up an Eclipse CDT DEBUG Configuration to pass a file to stdin and another one to stdout.
I have tried unsuccessfully a few solutions, all inside the DEBUG Configuration GUI :
In Common / Standard Input and Output / Input File: I put inputfile.txt and in the same section Output file: I put outputfile.txt. As the GUI indicates that the working directory is ${workspace_loc:/myprogram}, it should be alright, but when the debugger is started, it warns :
[Invalid file specified for console output: test/WittenACM87ArithmCoding-1.txt.coded]
[Invalid file specified for stdin file: test/WittenACM87ArithmCoding-1.txt]
In Arguments I put < inputfile.txt > outputfile.txt which is obviously not designed for that
Of course, both files are in the working directory. All attempts fails on the ch = getc(stdin); code line with some strange message:
Can't find a source file at "/build/glibc-p3Km7c/glibc-2.24/io/../sysdeps/unix/syscall-template.S"
Locate the file or edit the source lookup path to include its location.
Here is the stack:
Thread #1 [myprogram] 31960 [core: 5] (Suspended : Signal : SIGINT:Interrupt)
__read_nocancel() at /build/glibc-p3Km7c/glibc-2.24/io/../sysdeps/unix/syscall-template.S:84 0x7ffff7811700
_IO_new_file_underflow() at /build/glibc-p3Km7c/glibc-2.24/libio/fileops.c:600 0x7ffff77a9a00
__GI__IO_default_uflow() at /build/glibc-p3Km7c/glibc-2.24/libio/genops.c:413 0x7ffff77aab02
_IO_getc() at /build/glibc-p3Km7c/glibc-2.24/libio/getc.c:38 0x7ffff77a54f0
main() at /xxxxxx/src/myprogram.c:20 0x555555554f01
When I run the application directly in the console, it works:
./myprogram < inputfile.txt > outputfile.txt
I assume from this that Eclipse does not manage to realise the files redirections to stdin and stdout, so obviously, I do it the wrong way. I have searched for it, but here and here don't provide solution for my use case.
So, in order to be able to use the debugger from Eclipse, what is the right way to set up the Eclipse DEBUG Configuration ?
In fact, solution 1 was using a relative path not related to the working directory. Using either button Workspace... or File System... in the GUI enables to select the files which shall already exist.
For example with Workspace definition, the field becomes :
${workspace_loc:/myprogram/inputfile.txt} (same for output)
And it works. Debuggers says :
[Console output redirected to file:/.../myprogram/outputfile.txt]

Trying to use the Yajl example code, don't know how to give the input to the program

I've compiled the code provided by YAJL library in C. Don't know why the compiled program not parsing my file or is my file format is wrong?
I'm quite new in parsing JSON file.
This is where C code example located at https://lloyd.github.io/yajl/
Don't know do I need to paste the entire code or link is fine?
Myfile.
cat input_file.json
helllooooooooooo
When I ran the program ./a.out json_reformat input_file.json, it doesn't do anything. ./a.out -m json_reformat input_file.json this also didnt work.
I tried with -u and -m option nothing worked.
It print out the usage in STDOUT like this.
usage: json_reformat [options]
-m minimize json rather than beautify (default)
-u allow invalid UTF8 inside strings during parsing

Can't read file generated by bash line

I have written a C code where the bash script lines are used inside this C code, and this is how I wrote it:
printf("wc -l < smallerthan > number_lines\n");
if( (fr=fopen(fname_rcut,"r"))==NULL ) { printf("error in rcut file: %s\n",fname_rcut); exit(1); }
I need to read the file "number_lines" which is generated from "smallerthan" file, the problem is when I source the C code to run automatically like:
$gcc myC_code.c -lm
$./a.out > run.sh
$source run.sh
Then if I view the run.sh
& vi run.sh
I get this inside run.sh:
wc -l < smallerthan > number_lines
ls
error in rcut file: /home/number_lines
which mean the code upto this point didn't find my "number_lines" file yet since the number_lines file is yet to appear, but if I copy the line and run it separately, instead of "automatically", then it works because the file is there now.
My question is, how to make my code run automatically and my C code to read the file which is generated by bash line or how to generate the file and read it properly?
Any idea please because I'm really new to programming and I have to use bash inside C for my work.
Note: the above is only small part of my C code but I used several bash lines inside my C code.
There are a number of observations in your code. I assume that char *fname_rcut indeed points to "/home/number_lines".
First observation: if you write commands to a file, they will not be executed.So the file number_lines is created only after you run run.sh. Therefore, the file will not exist during the execution of your C program. You might look into int system(const char *command) (man 3 system).
Second observation: /home/number_lines is probably not the correct filename. It would probably be /home/your_name/number_lines; try a pwd to see what the exact directory name is.
Third observation: Why do you want to source run.sh? Source executes the file in the current shell. There is usually no need for that.
I have solved it :
what we need actually is using system(command) after each shell command
for example :
printf("wc -l < smallerthan > number_lines\n");
will be after solving :
sprintf(command1,"wc -l < smallerthan > number_lines\n");
system(command1);

Why won't this program work with the OS redirection command in C?

I am working with command prompt features with a simple program to generate a text file in C. Here is the program:
#include <stdio.h>
int main(void)
{
char buf[80];
fgets(buf, 30, stdin);
printf("the input was %s\n", buf);
return 0;
}
My programming book is wanting to show how to play with the command prompt to make text files from programs, and instructs typing the word 'redirect' followed by '>' then the name of the program name with '.txt'. as below:
redirect> programname.txt
Now this IS generating a file 'programname.txt' on the desktop, but it is empty. The book purports that recipe should allow me to enter a string (as the program is DESIGNED to do) and that this string will be inside a generated programname.txt file. Also, there is a warning in the command line: "not recognized as an internal or external command". I've had this schpill before, but the text file generation did WORK, in that it did generate the .txt file. What am I missing here, for this program to work as intended?
You seem to be confused by the fact that it is not your program, but the shell which creates the file programname.txt, before it even tries to run your program.
And after the first succeeded and created an empty file, the latter probably fails because there is no command redirect in your PATH or such a thing exists as a builtin in your shell, as has already been suggested.
The usual way to perform output redirection in a shell is to use the > filename, but not with redirect before it contrary to what you say. The thing that comes before the > is the command to be redirected.
So, let's say you compile your program and save it as foo in the current directory (e.g. cc -o foo myprogram.c). In that case, you can redirect its output by saying:
./foo > filename.txt

System() function in C showing buggy output

I am using the system() function in C to run a system command. While using the system(), the command I am using is:
system("C:\splint-3.1.2\bin\splint first.c>output.txt");
However, the output of this is not being correctly sent to the txt file. To be more specific, the txt file is created, however the output is not appended to the file.
On running the same command from the CMD, the output is correctly sent to the txt file.
Any idea on what's going wrong?
Escape your backslashes so the compiler interprets them correctly:
system("C:\\splint-3.1.2\\bin\\splint first.c>output.txt");

Resources