-3

I have written the following piece of code:

#include<stdio.h>

void add_linebreak()
{
    FILE *fp ;
    char c, NEWL=10 ;
    fp=fopen("text1.txt","a+") ;
    if(fp==NULL)
    {
        printf("\nFile Not Found") ;
    } 
    fseek(fp,0,SEEK_CURR) ;
    while(!feof(fp))
    {
        //if(!feof(fp))
        //{ 
        c=fgetc(fp);
        if(c==NEWL)
        {
            fprintf(fp,"%c",NEWL) ;
        }       
        //}
    }
    fclose(fp) ;
}

int main()
{ 
    add_linebreak() ;
    printf("\nEditing Complete") ;
    return 0 ;
}

The program took following data as input from a file named text1.txt :

1 this
2 is
3 a
4 text
5 file
6 to
7 test a
8 program
9 written
10 in c

Actual Output :

1 this
2 is
3 a
4 text
5 file
6 to 
7 test a
8 program
9 written
10 in c
11

Expected Output:

1  this
2
3  is
4
5  a
6
7  text
8
9  file
10
11 to
12
13 test a
14
15 program
16
17 written
18
19 in c
20

I scratched my head on this for hours but wasn't able to get the expected output, please help me.

Harsh
  • 1
  • 5
  • I don't understand. What is the input, what is the desired output and what is the actual output? Can you write it clearly? And indent the code properly. – Eugene Sh. Jul 11 '16 at 17:22
  • Thanks, Eugene Sh. I have just edited my post. Looking forward for ur help. – Harsh Jul 11 '16 at 17:34
  • 1
    http://stackoverflow.com/q/5431941/3185968 – EOF Jul 11 '16 at 17:34
  • `while(!feof(fp))` will be true only *after* you have failed to read, and already displayed an extra newline. Instead check the result of the input operation and stop immediately when it fails. – Bo Persson Jul 11 '16 at 17:34
  • 2
    You can't *insert* data in a file. You have to have an input file and an output file. Or at least a memory buffer for either input or output. – Eugene Sh. Jul 11 '16 at 17:37
  • C11 draft standard n1570: *7.21.5.3 The fopen function 7 When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file.* – EOF Jul 11 '16 at 17:47

2 Answers2

1

you gotta make a copyy of the file

Also fgetc returns int not char

void add_linebreak()
{
    FILE *fin, *fout ;
    char NEWL='\n' ;
    fin=fopen("text1.txt","rb") ;
    if(fp==NULL)
    {
        printf("\nFile Not Found") ;
    } 
    fout = fopen("copy.txt","w");
    while(1)
    {
        int  c=fgetc(fin);
        if(c==EOF)
            break;
        fprintf(fout, "%c", c);
        if(c==NEWL)
        {
            fprintf(fout,"%c",NEWL) ;
        }       
    }
    fclose(fout) ;
    fclose(fin);
}

renaming the file back to the original and adding some more error handling is left as a test :-)

pm100
  • 48,078
  • 23
  • 82
  • 145
0

You may not perform the modifications in-place. Not with stuff like text.

You have to instead read from one file, write to a temporary then perhaps rename the copy to overwrite the original update.

The other answer says almost everything besides the rename.

Oh, another thing: I have found often that newlines tended to get stripped in my old test programs; that may be another thing. But the modifying-in-place is the most likely culprit.

Paul Stelian
  • 1,381
  • 9
  • 27