简体   繁体   English

C ++-读取和打印整个文件

[英]C++ - Read and print whole file

Like in the topic, I'd like to read from standard input and print to standard output whole file with no difference between them. 像本主题中一样,我想从标准输入中读取并打印到标准输出整个文件,两者之间没有区别。

program < data.txt > data.out
diff data.txt data.out // <- the same

File contains Unicode letters. 文件包含Unicode字母。

I've managed write following piece of code: 我设法编写了以下代码:

char s[100000];

int main()
{
    setmode(1, _O_BINARY);

    char *n;

    do {
        n = gets(s);
        s[strlen(s)-1] = '\n';
        printf("%s", s);
    }
    while(n);

    return 0;
}

but input and output is slighty different (input: 76 465KB, output: 76 498KB) 但是输入和输出略有不同(输入:76 465KB,输出:76 498KB)

Thanks in advance. 提前致谢。

EDIT: 编辑:

Now, it's only 2KB difference. 现在,仅相差2KB。

EDIT: 编辑:

It's OK. 没关系。

It could happen if the input file has \\n line endings. 如果输入文件的行尾有\\ n,则可能会发生这种情况。 The output file will have \\r\\n line endings on Windows. 在Windows上,输出文件将以\\ r \\ n结尾。 That could explain the difference. 那可以解释差异。

If you don't want to output the \\rs, you can follow this answer 如果您不想输出\\ rs,则可以按照以下答案进行操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM