944,179 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 12815
  • C RSS
Jan 7th, 2007
0

Read file with unknown buffer length

Expand Post »
I try read a story text file using c. the problem is i want one paragraf in one variabel. i am confuse how much i should alocate the buffer size?
  1. #include <stdio.h>
  2. int main() {
  3. char buf[255]; <<HOW MUCH SHOULD WE ALOCATE TE BUFFER?
  4. FILE* fp = fopen("somefile.txt","r");
  5. if( fp == NULL) {
  6. printf("Can't open the file\n");
  7. return 1;
  8. } // read each line of the file
  9.  
  10. while( fgets(buf,sizeof(buf),1,fp) != NULL) {
  11. // count the lines or do other stuff here
  12. }
  13. // close the file fclose(fp); return 0; }

:cheesysorry for my bad english, i hope u understand)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xibnoe is offline Offline
7 posts
since Jun 2006
Jan 7th, 2007
0

Re: Read file with unknown buffer length

Well, since you're only reading in one line of the file at a time, you should have some idea of how long the lines will be. Choose a buffer size that is large enough to hold these requirements. Since we don't know what files you're planning to read, we can't give a perfect guess.

This question is similar to "how large should I make this field in my database?" Well, the answer is however much you need.

In C++ this problem doesn't exist since you can use strings to dynamically allocate the memory needed to store the data.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jan 7th, 2007
0

Re: Read file with unknown buffer length

you have a couple options: (1) put each of the strings in an array of strings, or (2) keep a linked list of the lines that are read.

Here is an example of using the array.
  1. #include <stdio.h>
  2. int main() {
  3. int linecount = 0;
  4. char *array[255] = {0}; // hold 255 lines
  5.  
  6. char buf[255]; <<HOW MUCH SHOULD WE ALOCATE TE BUFFER?
  7. FILE* fp = fopen("somefile.txt","r");
  8. if( fp == NULL) {
  9. printf("Can't open the file\n");
  10. return 1;
  11. } // read each line of the file
  12.  
  13. while( fgets(buf,sizeof(buf),1,fp) != NULL) {
  14. // count the lines or do other stuff here
  15. array[linecount] = malloc(strlen(buf)+1);
  16. strcy(array[linecount],buf);
  17. linecount++;
  18. }
  19. // close the file
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Jan 7th, 2007
0

Re: Read file with unknown buffer length

Click to Expand / Collapse  Quote originally posted by xibnoe ...
I try read a story text file using c. the problem is i want one paragraf in one variabel. i am confuse how much i should alocate the buffer size?
Unless you know how many characters in your paragraph, you can't just allocate a buffer. As AD suggests, you can set up an array of pointers, assuming you know how many paragraphs you need. There's another function you can use named realloc(). You malloc() for the first line you read, then realloc() to add more space for each line additional line you read. When you get to the end of the paragraph, increment the paragraph count (like AD did) and start again with a new malloc().
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,753 posts
since May 2006

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: How to deal with integer overflow
Next Thread in C Forum Timeline: Try This





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


Follow us on Twitter


© 2011 DaniWeb® LLC