| | |
Read file with unknown buffer length
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2006
Posts: 7
Reputation:
Solved Threads: 0
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?
:cheesy
sorry for my bad english, i hope u understand)
C Syntax (Toggle Plain Text)
#include <stdio.h> int main() { char buf[255]; <<HOW MUCH SHOULD WE ALOCATE TE BUFFER? FILE* fp = fopen("somefile.txt","r"); if( fp == NULL) { printf("Can't open the file\n"); return 1; } // read each line of the file while( fgets(buf,sizeof(buf),1,fp) != NULL) { // count the lines or do other stuff here } // close the file fclose(fp); return 0; }
:cheesy
sorry for my bad english, i hope u understand) 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.
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.
All my posts may be freely redistributed under the terms of the MIT license.
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.
Here is an example of using the array.
C Syntax (Toggle Plain Text)
#include <stdio.h> int main() { int linecount = 0; char *array[255] = {0}; // hold 255 lines char buf[255]; <<HOW MUCH SHOULD WE ALOCATE TE BUFFER? FILE* fp = fopen("somefile.txt","r"); if( fp == NULL) { printf("Can't open the file\n"); return 1; } // read each line of the file while( fgets(buf,sizeof(buf),1,fp) != NULL) { // count the lines or do other stuff here array[linecount] = malloc(strlen(buf)+1); strcy(array[linecount],buf); linecount++; } // 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.
•
•
•
•
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?
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- how to read a file from client side without browsing (ASP.NET)
- C++ How can read from file.txt & where can save this file(file.txt) to start reading (C++)
- create, write & read file to/from folder (C++)
- struct linked list problem (C)
- Read in a file and store in char array (C)
Other Threads in the C Forum
- Previous Thread: How to deal with integer overflow
- Next Thread: Try This
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures suggestions system systemcall test testautomation unix user voidmain() wab win32api windows.h






