C file stream

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2006
Posts: 35
Reputation: rwagnes is an unknown quantity at this point 
Solved Threads: 0
rwagnes's Avatar
rwagnes rwagnes is offline Offline
Light Poster

C file stream

 
0
  #1
Feb 9th, 2007
Quick Question: Is there a way in c to read an entire file into a string or char buffer?

Thanks, Elise
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: C file stream

 
0
  #2
Feb 9th, 2007
Originally Posted by rwagnes View Post
Quick Question: Is there a way in c to read an entire file into a string or char buffer?

Thanks, Elise
Yes. With fopen, SEEK_END and ftell you can determine the size of file and after SEEK_SET read the the whole file with fread.
[EDIT]
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. char * buff = NULL;
  7. int fLength = 0;
  8. FILE * file = NULL;
  9.  
  10. if ((file = fopen("doc.txt", "r")) == NULL)
  11. {
  12. printf("No file \n");
  13. return 1;
  14. }
  15.  
  16. fseek(file, 0, SEEK_END);
  17. fLength = ftell(file);
  18. if ((buff = malloc(fLength * sizeof(char) + 1)) == NULL)
  19. {
  20. fclose(file);
  21. return 1;
  22. }
  23. fseek(file, SEEK_SET, 0);
  24. fread(buff, sizeof(char), fLength, file);
  25. buff[fLength + 1] = '\0';
  26. puts(buff);
  27. free(buff);
  28. fclose(file);
  29. return 0;
  30. }
[/EDIT]
Last edited by andor; Feb 9th, 2007 at 5:16 am.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: C file stream

 
0
  #3
Feb 9th, 2007
Except simply seeking to the end doesn't take into account any translations which may occur when the file is read byte by byte (even if you do read them all in one call).
http://c-faq.com/osdep/filesize.html
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 35
Reputation: rwagnes is an unknown quantity at this point 
Solved Threads: 0
rwagnes's Avatar
rwagnes rwagnes is offline Offline
Light Poster

Re: C file stream

 
0
  #4
Feb 10th, 2007
thanks y'all, that was a big help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC