我知道fscanf()、fgets()以及其他读取文本文件下一行的函数。但是,如果 'cat msg1.txt | 给您一个文本文件。 ./anonymizer' 你会使用相同的功能吗? 对于我的程序,主要代码是:
int main (void)
{
char input[1000]= {'\0'}; //the sentence the user will enter
printf("Enter a sentence:");
scanf("%[^\n]", input);
char newSentence[1000]={'\0'};
sentence=(char *) &newSentence;
line=getText(input,0);
divide(input);
printf("%s\n",sentence);
return 0;
}
在命令行中我输入:
gcc -o anonymizer anonymizer.c
cat msg1.txt | ./anonymizer
我的 msg1 文本文件包含:
Hi, my email addresses are h.potter@hogwarts.edu and 1a@2b3c@lkj@ Although it's not an email addresses, I'd hate if@ you saw my secret@word. Gary.zenkel@nbcuni.comHoever, input variable only contains the first line: 'Hi, my email addresses are h.potter@hogwarts.edu and 1a@2b3c@lkj@'
如何让输入变量包含其他两行?
最佳答案
差不多了。虽然实际上可能没有以这种方式定义,但 scanf(...)
本质上等同于 fscanf(stdin, ...)
。与 gets
/fgets
类似。您应该能够使用其中任何一个来从标准输入流中读取数据。
关于c - 读取文本文件中的下一行 C 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16904862/