2

I'm reading a normal text file and write all the words as numbers to another text. When a line finishes it looks for a "new line character (\n)" and continues from the new line. In Ubuntu it executes perfectly but in Windows (DevC++) it cannot operate the function. My problem is the text in Windows which I read haven't got new line characters. Even I put new lines by my hand my program cannot see it. When I want to print the character at the end of the line, it says it is a space (ascii = 32) and I am sur that I am end of the line. Here is my end of line control code, what can I do to fix it? And I read about a character called "carriage return (\r)" but it doesn't fix my problem either.

c = fgetc(fp);
printf("%d", c);
fseek(fp, -1, SEEK_SET);
if(c == '\n' || c == '\r')
    fprintf(fp3, "%c%c", '\r', '\n');
mrtgnccn
  • 173
  • 2
  • 2
  • 6
  • 3
    The windows line seperator is "\r\n". – Elliott Frisch May 21 '14 at 18:25
  • How can I mention it together in if statement I don't know it, like '\r\n' ) @ElliottFrisch – mrtgnccn May 21 '14 at 18:27
  • 2
    The problem is Windows doesn't use a single character at end of line. It uses two. First a carriage return ('\r') then a new line ('\n'). Ancient versions of Mac (e.g. pre X) used '\r' alone. – Elliott Frisch May 21 '14 at 18:29
  • 1
    `\r\n` was what you actually sent to terminals and printers to get a new line; still is, to terminal/console emulators. There is no ASCII newline, `\n` is translated to ASCII LF, linefeed without carriage return; its interpretation as newline on unix is entirely conventional. – jthill May 21 '14 at 19:24
  • Related: http://stackoverflow.com/questions/1279779/what-is-the-difference-between-r-and-n/9549183#9549183 – Adrian McCarthy May 21 '14 at 20:41

2 Answers2

2

If you are opening a text file and want newline conversions to take place, open the file in "r" mode instead of "rb"

FILE *fp = fopen(fname, "r");

this will open in text mode instead of binary mode, which is what you want for text files. On linux there won't appear to be a difference, but on windows, \r\n will be translated to \n

A possible solution it seems, is to read the numbers out of your file into an int variable directly.

int n;
fscanf(fp, "%d", &n);

unless the newline means something significant to you.

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
  • I tried to open the file both in "rb" and "r"; there is no difference. My text haven't got new lines I think, but I put them by myself too. I don't understand the problem. Can the problem is about text file? – mrtgnccn May 21 '14 at 18:37
  • @mrtgnccn if it doesn't have newlines then why are you checking for `\n` – Ryan Haining May 22 '14 at 01:31
0

There are a couple of questions here

  1. What is the difference between windows text newline and unix text newline?

    UNIX newline is LF only. ASCII code 0x0a. Windows newline is CR + LF. ASCII code 0x0d and 0x0a

  2. Does your file have LF or CR ?

    Use a hex editor to see the contents of the file. I use xxd on linux.

    $ xxd unix.txt
    0000000: 0a0a 
    $ xxd windows.txt
    0000000: 0d0a 
    
Community
  • 1
  • 1
akn
  • 56
  • 3