Read file with unknown buffer length

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

Join Date: Jun 2006
Posts: 7
Reputation: xibnoe is an unknown quantity at this point 
Solved Threads: 0
xibnoe xibnoe is offline Offline
Newbie Poster

Read file with unknown buffer length

 
0
  #1
Jan 7th, 2007
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)
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Read file with unknown buffer length

 
0
  #2
Jan 7th, 2007
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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,578
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: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Read file with unknown buffer length

 
0
  #3
Jan 7th, 2007
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
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: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Read file with unknown buffer length

 
0
  #4
Jan 7th, 2007
Originally Posted by xibnoe View 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?
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().
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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