且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何读取未知输入类型(FILESTREAM或标准输入)C数据

更新时间:2023-11-17 23:34:40

可能是我正确地理解您的需要。

我不是你的整顿code,但写我自己。

下面是简单的code,从控制台读取:code:的main.c

 的#include<&stdio.h中GT;
#包括LT&;&文件ctype.h GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
INT主(INT ARGC,CHAR *的argv []){
    如果(的argc!= 2){
        的printf(\\ n错误的号码参数的\\ n);
        出口(1);
    }
    INT numofwords =的atoi(ARGV [1]);
    炭缓冲器[128];    //的printf(%d个\\ N,numofwords);    而(numofwords - ){
        scanf函数(%S,缓冲区);
        的printf(%S \\ n,缓冲区);
    }
    返回0;
}

它如何工作的:

 〜$ GCC的main.c -o主

执行

 :〜$ ./main错号码说法
:〜$ ./main 2
grijesh
grijesh
你的名字
你的名字
:〜$

我希望它理解为你。程序简单地从控制台读取(扫描),并打印出控制台。 while循环运行,为你传递命令行输入的时间数。

现在,一个文本文件, dumy.txt A输入文件:

 :〜$猫dumy.txt
你的名字
我的名字
他的名字
她的名字
:〜$

现在看你想通过你达到code的内容:

 :〜$猫dumy.txt | ./main 2
你的名字
我的名字
:〜$

如果您想通过通过回声

 :〜$ $回声'一\\ ntwo \\ nthree| ./main 2


:〜$

这是你想要的吗?

如果是:

你错过明白了:

[您code]

错误1,2 的)结果
你fsacnf是错误的两种方式:

 的fscanf(的argv [1]%s,&安培;字);


  • 第一个参数是的argv [1] 的char * 这是错误的,你需要通过 FILE * 键入。正如查尔斯沃思奥利先生的回答解释。


  • 二,你仍然需要从标准输入阅读。 | 运营商从重定向命令第一第二命令的输出。


(错误3,4,5,6 )搜索
通过发送回声布拉布拉你是刚刚送你需要做一些事情,像我一样的单一刺(我添加\\ n的回波串新线还我回声字符串 $ 启动,因此无法打印为原始字符串的)。回声,这样就可以根据这是第二个参数从code读的argv [1] 不是的argv [2] 。因此,在您code下面一行是错误的了。

  INT numofwords =的atoi(argv的[2]);

我在code纠正了这一行。

I 被初始化为零 I = 0 ,在while循环的条件是 < = ,我觉得应该是<

错误7 的)结果
您运行code的方式是错误的回声布拉布拉| MYPROG 2 你的程序不知道的 mygrog 你必须通过完整路径。像我一样 ./主 ./ 表示当前目录。

这只是我对你的问题的观点。另请参阅由威廉Pursell先生给出评论的答案。

让我知道,如果你有其他疑问。

I wanted to know how to read data from an unknown source of input, meaning I don't know if the user is going to just type a sentence or is he going to give me some text file. I've tried using fscanf since I've read it is meant for unformatted input type this is my code, Im suppose to get some type of input(file or just a sentence (echo bla bla bla) and "int" and print only the "int" first words. The program should be used for piping meaning the command would look like that :
There are 2 ways to ways of using the program:

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

The problematic line is 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

May be I am correctly understood your need.

I am not rectifying your code but writing my own.

Below is simple code that read from 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 does it works:

~$ gcc  main.c -o main  

execute:

:~$ ./main 

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

I hope its understood to you. the program simply read (scan) from console and print out to console. The while loop runs for number of time you pass on command line input.

Now, A text file dumy.txt a input file:

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

Now see what you want to achieve through you code:

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

If you want to pass through echo :

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

Is this you want?

If yes:

What you miss understood that:

[your code]

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

fscanf(argv[1],"%s",&word);  

  • First argument is argv[1] is char* that is wrong you need to pass FILE* type. As explained in Mr. Oli Charlesworth's answer.

  • Second you still need to read from stdin. | operator redirects the output from first command to second command.

(mistake 3, 4, 5, 6)
By sending echo "blabla" you are just sending a single sting you need to do something like I did (I added \n in echo string for new line also my echo string start with $ so it not print as raw string). echo so that you can read from code according to second argument that is argv[1] not argv[2]. So in your code following line is wrong too.

int numofwords = atoi(argv[2]);   

I corrected this line in my code.

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

(mistake 7)
The way you run your code is wrong echo "blabla" | myprog 2 your program not know as mygrog you have to pass complete path. like I did ./main, ./ means current directory.

this just my view about your question. Read also the answer in comment given by Mr. William Pursell.

Let me know if you have other doubts.