?
Solved

problem with fread

Posted on 2008-11-04
20
Medium Priority
?
845 Views
Last Modified: 2011-10-19
Hello group,

I'm using fread() function to read a file as:

            bytes_read=fread(csv_buffer, 1, 1024, fp);

As far as I know since fp has value and it is no NULL so this function should be able to read the data. However, bytes_read is always 0.

Please find the code in snippet. Any help is appreciated.


Thanks,
ak


fp = fopen(outfile, "rb");
    if (!fp)
    {
      fprintf(stderr, "Failed to open %s\n", outfile, strerror(errno));
      exit(EXIT_FAILURE);
    }
 
    bytes_read=fread(csv_buffer, 1, 1024, fp);
    printf("\nbyte_read = %d", bytes_read);     >>>> shows always 0!

Open in new window

0
Comment
Question by:akohan
  • 9
  • 8
  • 2
  • +1
20 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22875418
Does the file have data?
clear errno before fread and check errno after call
Call ferror and see if it reports an error.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22875424
Note that fread doesn't return the number of bytes read. It returns the number of >elements< read. In this case, the size of your elements is 1 byte, so it comes down to the same, but it's an important distinction to keep in mind.

Now, regarding your problem.

Check the size of the file that you opened. Does it have size 0 ?
0
 

Expert Comment

by:stev75
ID: 22875589
when read binary, you can specify in fread(), how you interprete the binary data, eg. a struct.
arg1 // pointer to buffer
arg2 // size of each element of the array; in bytes, e.g. sizeof(char) reading text stored as bin
arg3 //simply how many elements you want to read
size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
 You simply need to tell your compiler how to interprete your data

 
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:akohan
ID: 22875595

Yes file has a size but can you let me know how I use errno in this case?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22875605
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22875616
>> Yes file has a size but can you let me know how I use errno in this case?

Are you sure ? Verify that the file you want to open is the same file as the file you are actually opening. What's the value of 'outfile' ?
0
 

Author Comment

by:akohan
ID: 22875623

 Simply I put:

    errno=0;
    bytes_read=fread(csv_buffer, 1, 1024, fp);
    printf("error no = %d", errno);                              

and it is 0.

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22875695
See my previous post ...
0
 

Author Comment

by:akohan
ID: 22876054

This is so strange! it works up in my code but it doesn't work in few blocks down!!!

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22876066
Can you show all of your code ?

>> it works up in my code but it doesn't work in few blocks down!!!

What do you mean by that ?
0
 

Author Comment

by:akohan
ID: 22879503

Sure. I have attached the code. The fread with issue is marked with comment as "problem starts here".

Thanks!
0
 

Author Comment

by:akohan
ID: 22879513

Sure. I have attached the code. The fread with issue is marked with comment as "problem starts here".

Thanks!
0
 

Author Comment

by:akohan
ID: 22879537

I cannot see it attached! retrying ...
abc.txt
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22879540
>> I have attached the code.

Can't see it ;)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22879687
Don't you want to pclose the fp file pointer before opening it again ?
0
 

Author Comment

by:akohan
ID: 22879718

Do you mean after 2nd while() ?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1000 total points
ID: 22879861
I mean right before you experience the problem.
0
 

Author Comment

by:akohan
ID: 22880444

Thanks! ok, now I found I was missing popen() in there and now I can read data however, the problem is that it still shows previous data from previous while loops.
I even cleaned csv_buffer[] but didn't help.

Doesn't it store read data into this buffer?

Regards,
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22880519
>> the problem is that it still shows previous data from previous while loops.

Can you give a bit more detail ?
0
 

Author Comment

by:akohan
ID: 22880683

Ok, as you know there are two series of while loops as:

 ... some code

 while(fread()....){
     if(csv_parse()....)
  }

 while(fread()....){
     if(csv_parse()....)
  }


  //starting a new set

 while(fread()....){
     if(csv_parse()....)
  }

 while(fread()....){
     if(csv_parse()....)
  }

In the second while loop set I had to call a function (library related) as init_csv() to clear previous data stored in memory. The problem was that I was getting previous processed data. Now, by calling this function library I'm getting what I was expecting.

Thank you so much for being so patient with me to learn!

Regards.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question