How to read data from an unknown input type (filestream or stdin) C

I wanted to know how to read data from an unknown input source, that is, I do not know if the user will simply enter a sentence or is he going to give me a text file. I tried using fscanf , as I read it, it is for an unformatted input type, this is my code, I suppose to get some type of input (file or just a sentence (echo bla bla bla) and "int" ) and print only the first words "int" . The program should be used to connect, meaning that the command will look like this:
There are two ways to use the program:

 1.echo "blabla" | myprog 2 (number of words to print) 2.cat input.txt | myprog 2 (number of words to print) 

Problem line - line 16, I tried using fscanf
Thanks!

  1 #include <stdio.h> 2 #include <ctype.h> 3 #include <string.h> 4 #include <stdlib.h> 5 6 7 int main(int argc, char *argv[]){ 8 char *words[32]; 9 int numofwords = atoi(argv[2]); 10 int i=0; 11 int len; 12 char *word = malloc (32 * sizeof(char)); 13 char c; 14 while (i<=numofwords){ 15 if ((c = getc (argv[1])) != EOF){ 16 fscanf(argv[1],"%s",&word); 17 len = strlen (word); 18 words[i] = malloc ((len+1) * sizeof(char)); 19 i++ 20 } 21 printf(words[i]); 22 } 23 return 0; 24 } 25 
+3
source share
3 answers

Perhaps I understood your need correctly.

I do not correct my code, but I write it myself.

The following is a simple code that is read from the console: code: main.c

 #include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> int main(int argc, char* argv[]){ if(argc!=2){ printf("\n wrong number of argument\n"); exit(1); } int numofwords = atoi(argv[1]); char buffer[128]; //printf("%d\n",numofwords); while(numofwords--){ scanf("%s",buffer); printf("%s\n",buffer); } return 0; } 

How it works:

 ~$ gcc main.c -o main 

execute:

 :~$ ./main wrong number of argument :~$ ./main 2 grijesh grijesh yourname yourname :~$ 

I hope he understood him. the program simply reads (scan) from the console and displays on the console. The while loop works for the amount of time you pass on command line input.

Now, the text file dumy.txt input file:

 :~$ cat dumy.txt yourname myname hisname hername :~$ 

Now look at what you want to achieve through the code:

 :~$ cat dumy.txt | ./main 2 yourname myname :~$ 

If you want to go through echo :

 :~$ echo $'one\ntwo\nthree' | ./main 2 one two :~$ 

Do you want this?

If yes:

What you missed realized that:

[your code]

(error 1,2)
Your fsacnf is wrong in two ways:

 fscanf(argv[1],"%s",&word); 
  • The first argument argv[1] is char* , which is incorrect, you need to pass the type FILE* . As Mr. Oli Charlesworth explained.

  • Secondly, you still need to read with stdin . Operator | redirects output from the first command to the second command.

(error 3, 4, 5, 6)
By sending the echo "blabla" you just send a single sting, you need to do something like what I did (I added \ n to the echo line for a new line, and my echo line starts with $ so that it does not print like a raw string). an echo so you can read the code on the second argument, which argv[1] not argv[2] . So in your code the next line is incorrect.

 int numofwords = atoi(argv[2]); 

I fixed this line in my code.

and i initialized to zero i = 0 , while the loop condition is <= , I think it should be < .

(error 7)
The way you run your code is incorrect echo "blabla" | myprog 2 echo "blabla" | myprog 2 your program does not know how mygrog you need to go the full way. as I did ./main , ./ means the current directory.

this is just my opinion about your question. Read also the answer in the comment given by Mr. William Pursel.

Let me know if you have any other doubts.

+4
source

I don’t know exactly what your question is, but I think it could be β€œhow can I handle the input file and console input the same?”.

Do you know that stdin (standard input) is already FILE * ? Thus, this means that you can pass it as the first argument to fscanf , like a regular file:

 FILE *normal_file = fopen(...); // Read from normal file fscanf(normal_file, "%d", &x); // Read from stdin fscanf(stdin, "%d", &y); 
+3
source

An alternative choice is to pass the file name as an argument.

  • echo "blabla" | myprog 2 (number of words to print)
  • myprog 2 input.txt (number of words to print)

Then you should:

  • if argc == 1, then do fine and handle stdin (stdin - FILE *) using fgetc or another alternative f * method.
  • If argc == 2, then get the file name from argv [1], use fopen (filename) to get FILE * and process it.

Otherwise, you would do what Olya said and say the first word, then either fopen, or go to your processing function, or fseek (stdin, SEEK_SET, 0), then pass stdin to your processing function.

+3
source

Source: https://habr.com/ru/post/1493912/


All Articles