User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 401,946 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,344 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 1502 | Replies: 3 | Solved
Reply
Join Date: Dec 2006
Location: Texas
Posts: 33
Reputation: rwagnes is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rwagnes's Avatar
rwagnes rwagnes is offline Offline
Light Poster

C file stream

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Novi Sad, Serbia
Posts: 273
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Rep Power: 6
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: C file stream

  #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 4:16 am.
If you want to win, you must not loose (Alan Ford)
Reply With Quote  
Join Date: Dec 2005
Posts: 3,393
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 21
Solved Threads: 385
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: C file stream

  #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
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Dec 2006
Location: Texas
Posts: 33
Reputation: rwagnes is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rwagnes's Avatar
rwagnes rwagnes is offline Offline
Light Poster

Re: C file stream

  #4  
Feb 9th, 2007
thanks y'all, that was a big help.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 5:19 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC