-2

So, i have an input file that looks like this:

line1
line2

and i want to write the output file like this:

line1,line2

And for now the output is with newline like this:

line1,
line2,

If I replace \r with \n the output will be like the input.

Here is my code:

int main()
{
 /* Pointer to the file */
 FILE *fp1;
 FILE *fp2;
 /* Character variable to read the content of file */
 char c;


 /* Opening a file in r mode*/
 fp1= fopen ("Newfile.txt", "r");
 fp2 = fopen ("result.txt","w");
 /* Infinite loop –I have used break to come out of the loop*/
 while(1)
 {
    c = fgetc(fp1);
    if(c==EOF)
        break;
        else if(c=='\r')
                 fprintf(fp2,",");
        else
            fprintf(fp2,"%c", c);


 }

 fclose(fp1);
 fclose(fp2);
 return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
Alex Lucaci
  • 620
  • 8
  • 18

3 Answers3

2

You are reading the files in "text mode". That means the runtime library does some translation, specifically it will convert CRLF ("\r\n") to LF ("\n"). Your code will never see a '\r'.

Option 1: substitute '\n' with a comma

Option 2: open the files in binary mode: fopen("foobar", "rb")

pmg
  • 106,608
  • 13
  • 126
  • 198
0

You can use fgets and then replace \n with , using strchr:

char str[512], *p;
...
while (fgets(str, sizeof str, fp1)) {
    p = strchr(str, '\n');
    if (p != NULL) {
        *p = ',';
    }
    fprintf(fp2,"%s", str);
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • not by much. all f* functions are buffered internally. when you call fgetc the first time, it'll fill some fixed-size buffer, and every proceeding fgetc call after that will try to read from that buffer. – DeftlyHacked Aug 19 '16 at 10:50
  • You are right, I was reading http://stackoverflow.com/questions/5186457/c-fgets-versus-fgetc-for-reading-line and it seems that the difference is minimal, thanks. – David Ranieri Aug 19 '16 at 10:57
0

The EOF value isn't used for standard files. It's used for data transmission. You'll need to actually call feof.

DeftlyHacked
  • 405
  • 2
  • 9
  • There is nothing wrong checking `EOF` with files, of course the type must be an `int` instead of a `char` – David Ranieri Aug 19 '16 at 10:56
  • To quote the documentation: "If a read error occurs, the function returns EOF and sets the error indicator for the stream (ferror)." An EOF alone doesn't mean EOF, it means a generalized error. EOF only has meaning in data transmission. – DeftlyHacked Aug 19 '16 at 10:59
  • 1
    http://stackoverflow.com/a/4292738/1606345 and http://stackoverflow.com/a/4292802/1606345 – David Ranieri Aug 19 '16 at 11:04
  • ok, you are missing the point, the eof check is correctly done, I only need to know how I can delete the newline for my output to be in only one line – Alex Lucaci Aug 19 '16 at 16:42
  • ...I was the one who upvoted you man. To be blunt, the format of the question kinda sucked. You didn't state exactly what your problem was, you just threw code in our faces and said, "fix it!". I saw the comment that said, "Infinite loop –I have used break to come out of the loop" and assumed that was the problem (when you said 'I have used 'break', I also assumed you meant Ctrl-C) – DeftlyHacked Aug 19 '16 at 16:47