c - 读取文本文件中的下一行 C 编程

标签 c text-files

我知道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/

相关文章:

java - 缓冲写入器在文本文件上写入内存垃圾

java - 如何将一列数据追加到文本文件末尾

c - 使用缓冲区/动态内存的笔划空间

c++ - Objective-C++ 是与 Objective-C 完全不同的语言吗?

c++ - 如何读取寄存器 0x​​104567911 RBX、RCX、RDX、RSP。 C 或 C++ 中的 RBP、RSI、RDI?

c - 通过 UDP 发送包含字符串的结构

java - 通过 XSLT 将异常 XML 数据转换为 CSV

powershell - 如何在 powershell 中以预定义字符串分割非常大的文本文件 (4GB) 并快速完成

c - 生产者-消费者和条件变量程序陷入无限循环

c - 从文本文件读取C中不同类型的变量