4

I am trying to read from a .txt file that has some numbers in lines.

It looks like that.

example.txt

123
456
789
555

I open this as a binary file for reading wanted to read this file line by line so i know that in every line theres 4 characters (3 numbers and 1 new line character '\n').

I am doing this:

FILE * fp;

int page_size=4;
size_t read=0;
char * buffer = (char *)malloc((page_size+1)*sizeof(char));
fp = fopen("example.txt", "rb"); //open the file for binary input

//loop through the file reading a page at a time
do {
    read = fread(buffer,sizeof(char),page_size, fp); //issue the read call

    if(feof(fp)!=0) 
      read=0;

    if (read > 0) //if return value is > 0
    {   
        if (read < page_size) //if fewer bytes than requested were returned...
        {
            //fill the remainder of the buffer with zeroes
            memset(buffer + read, 0, page_size - read);
        }

        buffer[page_size]='\0';
        printf("|%s|\n",buffer);
    }

}
while(read == page_size); //end when a read returned fewer items

fclose(fp); //close the file

In printf is expected this result then

|123
|
|456
|
|789
|
|555
|

but the actual result i am taking is:

|123
|
456|
|
78|
|9
6|
|66
|

so it looks like that after the first 2 fread it reads only 2 numbers and something goes completely wrong with the new line character.

So what is wrong with fread here?

Bigood
  • 9,457
  • 3
  • 36
  • 65
Blenikos
  • 693
  • 11
  • 18
  • Your code does the expected thing on my system (linux) – bchurchill Mar 15 '13 at 09:23
  • 5
    If you're on windows, your example.txt probably has not 4, but 5 characters per line, since on windows, line delimiter is \r\n, and not just \n. – user2155932 Mar 15 '13 at 09:24
  • Do a hex dump of the strings you read. Hint: Dos Line feeds. – Aki Suihkonen Mar 15 '13 at 09:25
  • btw why don't you use the debugger and check what's in `buffer` ? – AndersK Mar 15 '13 at 09:34
  • 1
    windows: end of line in windows consists of two characters, when you open the file in text mode you get only one i.e. `\n` if you open in binary mode you get the two characters `\r\n`. – AndersK Mar 15 '13 at 09:39
  • I used the debugger and in buffer was 123 first time 456 second loop and then it seems it reads only 2 characters its time 78 3rd time 95 4th 55 5th – Blenikos Mar 15 '13 at 09:40
  • Maybe this is the problem (\r\n) let me check that – Blenikos Mar 15 '13 at 09:42
  • @Blenikos your program is working fine on my system... – Barath Ravikumar Mar 15 '13 at 09:52
  • Sure there was a problem with \r\n character. I changed the size in 5 bytes and it worked. I created a file in linux and with pagesize=4 now that every line has xxx\n it makes a segmantation fault in fread – Blenikos Mar 15 '13 at 09:55
  • @BarathBushan what system do you use? windows or unix-like? – Blenikos Mar 15 '13 at 09:56
  • Ok! it works fine now! The problem was \r\n character that windows textfiles put at the end. I didn't know that. Thank you all very much!!! – Blenikos Mar 15 '13 at 10:06
  • Just for the info can anyone tell me what \r character is and what is printed when you try to print that? – Blenikos Mar 15 '13 at 10:09

2 Answers2

2

You can use the following to read the file line by line.

   FILE * fp;
   char * line = NULL;
   size_t len = 0;
   ssize_t read;

   while ((read = getline(&line, &len, fp)) != -1) {
       printf("Line length: %zd :\n", read);
       printf("Line text: %s", line);
   }
Yasir Malik
  • 431
  • 2
  • 9
  • I don't want to read the file line by line in every case. I just want to read 4bytes (pagesize) every time and in that case something goes wrong – Blenikos Mar 15 '13 at 09:38
1
FILE * fp;
int page_size=4;
size_t read=0;
char * buffer = (char *)malloc((page_size+1)*sizeof(char));
fp = fopen("example.txt", "rb"); //open the file for binary input

//loop through the file reading a page at a time
do
{
read = fread(buffer,sizeof(char),page_size, fp); //issue the read call

if (read > 0) //if return value is > 0
{
    buffer[page_size]='\0';
    printf("|%s|\n",buffer);
}

}
while(read == page_size); //end when a read returned fewer items

fclose(fp);

you can try with this code, this code is running fine.

I tried with your code and that is also running fine on my system.

µtex
  • 830
  • 5
  • 12
  • It looks like a simplified version of mine. Well the problem was that i used windows textfiles that had two characters at the end of every line (\r\n). So now yes it works – Blenikos Mar 15 '13 at 10:11