Question

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

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
Was it helpful?

Solution

This is your first logic problem: 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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top