简体   繁体   English

fgets-省略了c的最后一行

[英]fgets - is omitting the last line in c

Basically i am having problems with my code - this is homework so would rather not post it in here for obvious reasons. 基本上我的代码有问题-这是家庭作业,因此出于明显原因不希望将其发布在这里。 If it becomes truely important for me to do so then i will have to as i am so stuck. 如果这样做对我来说真的很重要,那么我将不得不坚持下去。

i am reading 2 text files and it also has a separator , these values come from the command line, just assume that the separator in this case is xx 我正在读取2个文本文件,并且它也有一个分隔符,这些值来自命令行,只需假设在这种情况下分隔符为xx

File a
a
b
c


File b
d
e

Output should be
axxd
bxxe
cxx

the problem is that my code just doesn't do the last line correctly 问题是我的代码无法正确执行最后一行

i get an output of 我得到的输出

axxd
bxxe

I hope you guys can gather what i am doing wrong without me posting all my code, but my logic works on this principle; 我希望你们能在不发布所有代码的情况下收集我做错的事情,但是我的逻辑基于此原则。

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

This is your first logic problem: while(!feof(a) && !feof(b)) 这是您的第一个逻辑问题: while(!feof(a) && !feof(b))

This means that if either file has reached the end then you stop processing, even if there are more lines in the other file. 这意味着,如果任一文件已经到达末端,那么你停止处理,即使是在其他文件中更多的行。

Maybe you don't print a newline after printing the last line? 也许在打印最后一行后不打印换行符? Then it may not display on your screen at all. 然后它可能根本不会显示在屏幕上。 Notice that fgets only puts a newline in your buffer if the original line had one and the last line of a file might not. 请注意,如果原始行只有一行,而文件的最后一行可能没有,则fgets仅在缓冲区中放入换行符。

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

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