944,124 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 12257
  • C RSS
Feb 9th, 2007
0

C file stream

Expand Post »
Quick Question: Is there a way in c to read an entire file into a string or char buffer?

Thanks, Elise
Reputation Points: 10
Solved Threads: 0
Light Poster
rwagnes is offline Offline
36 posts
since Dec 2006
Feb 9th, 2007
-1

Re: C file stream

Click to Expand / Collapse  Quote originally posted by rwagnes ...
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.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Feb 9th, 2007
0

Re: C file stream

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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Feb 10th, 2007
0

Re: C file stream

thanks y'all, that was a big help.
Reputation Points: 10
Solved Threads: 0
Light Poster
rwagnes is offline Offline
36 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: My own read function
Next Thread in C Forum Timeline: not sure what this error means





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC