Problem with fread and malloc

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 2
Reputation: Puliver is an unknown quantity at this point 
Solved Threads: 0
Puliver Puliver is offline Offline
Newbie Poster

Problem with fread and malloc

 
0
  #1
Nov 15th, 2008
Hi, I'm curently creating a command line tool in VC++ 2008 Express Edition for myself which requires reading many files and merging them together. Everything is OK and during debugging (F5) there is not a single problem, but at run time (ctrl+F5) a problem with fread occurs at the same location each time. It reads many files without any problem and suddenly it stops at one file (each time the same one) and shows windows error dialog.

  1. Document *load(string fileName){
  2.  
  3. char *buffer;
  4. FILE *pFile;
  5. long lSize;
  6. size_t result;
  7. string strBuff;
  8. errno_t err;
  9. Document *document = new Document();
  10.  
  11. err = fopen_s ( &pFile, fileName.c_str() , "rb" );
  12. strBuff = "Can't read file \""+fileName+"\"!";
  13. if (err) {fputs (strBuff.c_str(), stderr); exit (1);}
  14. if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
  15.  
  16. // obtain file size:
  17. fseek (pFile , 0 , SEEK_END);
  18. lSize = ftell (pFile);
  19. rewind (pFile);
  20.  
  21. // allocate memory to contain the whole file:
  22. buffer = (char*) malloc (sizeof(char)*(lSize+1));
  23. if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
  24.  
  25. // copy the file into the buffer:
  26. result = fread (buffer,1,lSize,pFile);
  27. if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
  28. /* the whole file is now loaded in the memory buffer. */
  29.  
  30. // Here occurs the error. File has been read, but buffer is clear,
  31. // strBuff.assign crashes.
  32. strBuff.assign(buffer);
  33. free (buffer);
  34. // terminate
  35. fclose (pFile);
  36. return parse(strBuff, document);
  37. }

As you can see from my code, all checks went ok, but for some reason, there is nothing in buffer. I've tried to find out maximal file size I'm going to read and it worked, but then the application is much slower (because of the conversion to string object). and it doesn't explain why it occurs. I would like to know, what is doing this error.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,648
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem with fread and malloc

 
0
  #2
Nov 15th, 2008
Have you tried replacing FILE with fstream and replace malloc()/free() with new/deleteo[] ? See example program here


Then, of course, replace fputs() with cout statements.
Last edited by Ancient Dragon; Nov 15th, 2008 at 9:26 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem with fread and malloc

 
0
  #3
Nov 15th, 2008
The assign(const char*) wants C-string terminated by zero byte. You forget to assign buffer[lsize] = '\0'; after fread. It's enough to raise erratical memory access error in assign.

Another notes:
1. No need to rewind pFile, use fseek again.
2. Think about exit() in C++ programs. Automatic objects destructors are not called when exit terminates a program. May be it's OK, may be not...
3. Avoid mixing of malloc/free and new/delete mechanics. Better use only new/delete operators in C++ programs.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: Puliver is an unknown quantity at this point 
Solved Threads: 0
Puliver Puliver is offline Offline
Newbie Poster

Re: Problem with fread and malloc

 
0
  #4
Nov 15th, 2008
Thank you, now I see it too. Most of the time I'm developing in other languages, so I forgot to add that '\0' at the end of string . It works fine now, thanks. I've also changed malloc/free to new/delete.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 688 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC