How do I read file into a command line?
Solution 1
You can use standard redirect operations in a *nix shell to pass files as input:
./myprogram < inputfile.txt
This statement executes your program (myprogram) and pumps the data inside of inputfile.txt
to your program
You can also redirect the output of program to a file in a similar fashion:
./myprogram > outputfile.txt
Solution 2
Instead of doing
for(int i = 1; i < argc; i++)
{
insert(&trie, argv[i]);
}
you could doing something like
FILE *input;
char *line;
....
while (fscanf(input, "%ms", &line) != EOF) {
insert(&trie, line);
/* If you make a copy of line in `insert()`, you should
* free `line` at here; if you do not, free it later. */
free(line);
}
Solution 3
Use redirection
yourprogram < youtextfile
will offer the content of yourtextfile
as standard input (stdin) to yourprogram. Likewise
yourprogram > yourothertextfile
will send everything the program writes to standard output (stdout) to yourothertextfile
You'll notice when reading man pages that most system calls have a version that works directly with stdin or stdout
For example consider the printf family:
printf ("hello world\n");
is a shorter version of
fprintf (stdout,"hello world\n");
and the same goes for scanf and stdin.
This is only the most basic usage of redirection, which in my opinion is one of the key aspects of "the unix way of doing things". As such, you'll find lots of articles and tutorials that show examples that are a lot more advanced than what I wrote here. Have a look at this Linux Documentation Project page on redirection to get started.
EDIT: getting fed input via redirection ior interactively "looks" the same to the program, so it will react the same to redirected input as it does to console input. This means that if your program expects data line-wise (eg because it uses gets()
to read lines), the input text file should be organized in lines.
deviance
Updated on July 28, 2022Comments
-
deviance almost 2 years
Basically what I want to do is have a program with int main(argc, *argv[]) and instead of writing chars into command line, I want to have my program read those words from a file. How could I accomplish this? Is there a special command in Linux for that?
-
deviance over 10 yearsWell it's weird, any idea on why it doesn't work? Text file has words in it, but when I do ./a.out < mytextfile.txt it just doesn't give anything, argc is 1
-
fvu over 10 yearshard to say without seeing both the code and the text file... I have to go now, I'll make a quick edit that may help you understand why it could fail.
-
deviance over 10 yearsHere it is: gist.github.com/anonymous/928ada24aa8e6e1adda9 program number 1 uses this txt file to read upper case words and put them into tree, while program 2 converts any text into upper case words without numbers or any other non alpha characters. File 3 is the text, but much shortened - it has few thousand of words and each one of them has to go to command line
-
deviance over 10 yearsHow would I put these words in a 2d char array though? I posted a link somewhere up in the comments
-
deviance over 10 yearsBut I won't have argc tell me how many words are there, right?
-
deviance over 10 yearsAlso, 2 errors when I added this to code: finale.c:163:5: warning: ISO C does not support the 'm' scanf flag [-Wformat] finale.c:163:18: warning: ‘input’ is used uninitialized in this function [-Wuninitialized
-
deviance over 10 yearsWell when I forced it to compile it core dump'd
-
fvu over 10 years
%ms
is not available in all language dialects - cfr the man page. Here you can see how to switch dialects with gcc. Alternatively (but that's only a quick, temp fix as it's unsafe), redeclarechar line[128];
andfscanf(input, "%s", line)
and drop the free. It's unsafe because it's vulnerable to buffer overruns, but at least it should allow you to test the rest of the logic of your program. -
Diti over 10 yearsAn array of arrays of
char
, or an array of pointers tochar
? You could either do something likememcmp(double_array + index, line, bytes_read)
(withindex
a counter you increment each time you insert an element), or, for an array of pointers,double_array[index] = strdup(line)
. You can do whatever you want withline
in my code snippet; including duplicate it in whatever control structure (array, list node, etc.) you want. I am not going to give you a more specific code, since I believe I should just answer your original question: how to read lines from a file and process it. -
vonbrand over 4 yearsThis is also available e.g. in MS-DOS (and Windows).