0

基本上我的代码有问题——这是家庭作业,所以出于明显的原因,我宁愿不把它贴在这里。如果这样做对我来说真的很重要,那么我将不得不这样做,因为我被困住了。

我正在阅读 2 个文本文件,它也有一个分隔符,这些值来自命令行,假设这种情况下的分隔符是 xx

File a
a
b
c


File b
d
e

Output should be
axxd
bxxe
cxx

问题是我的代码没有正确执行最后一行

我得到一个输出

axxd
bxxe

我希望你们可以在我不发布所有代码的情况下收集我做错了什么,但我的逻辑是基于这个原则的;

while not at the end of the file for files a and b
    get a line using fgets from a
    create a character pointer and set it to the first occurrence of \n in the line using strchr
    if the pointer isn't null
        set the pointers value to be the end of line

get the line from b as above
and now write the line from a, the separator and the line from b to file
4

2 回答 2

3

这是您的第一个逻辑问题:while(!feof(a) && !feof(b))

这意味着如果任何一个文件已到达末尾,那么您将停止处理,即使另一个文件中有更多行。

于 2010-03-06T13:34:14.167 回答
0

也许您在打印最后一行后不打印换行符?然后它可能根本不会显示在您的屏幕上。请注意,如果原始行有一个换行符,而文件的最后一行可能没有,则 fgets 只会在缓冲区中放置一个换行符。

于 2010-03-06T13:13:35.473 回答