linked lists and file i/o in C
what would be the best way to read from a file, say, a text file that just had string data in it seperated by spaces into an array of strings? As in, there could be any amount of individual strings in the file, and the file opens and it is read and a string array is loaded. I have a string struct with a "char* str" and an "int capacity" data field. I cant seem to read the strings into my string array without previously knowing how many strings there are in the file. How would you count the number of strings in a file? So my string struct is
typedef struct {
char* str;
int capacity;
}String;
and in main i have an array declared: String* stringArray;
and i cant read a file of string data (ex. "apple banana car.. .etc") without knowing how many strings are in their in the first place. Because i need to know how many are in there to allocate enough space and then i would need to know the length of each to allocate enough space for each 'char'. can someone help?
if u have to use c then i think this will work but im not sure
and theres probally a better way to do it
Edit: oh didnt read whole thing at first
Edit: one mistake fixed
[Edited by - Julian90 on October 8, 2006 11:07:24 PM]
and theres probally a better way to do it
File* file = fopen("filename", "r");char temp = '1';int numRead = 1;int numStrings = 0;int i;String* strings;while (numRead != 0){ numRead = fread(&temp, 1, 1, file); if (temp == '/n') { numStrings++; }}strings = (String*)malloc(sizeof(String) * numStrings);for (i = 0; i < numStrings; i++){ strings.capacity = 1; // for null}fclose(file);file = fopen("filename", "r");i = 0;while (numRead != 0){ numRead = fread(&temp, 1, 1, file); strings.capacity++; if (temp == '/n') { i++; }}for (i = 0; i < numStrings; i++){ strings.str= (char*)malloc(str.capacity);}fclose(file);file = fopen("filename", "r");for (i = 0; i < numStrings; i++){ fread(strings.str, 1, strings.capacity, file); strings.str[strings.capacity - 1] = '/0';}fclose(file);
Edit: oh didnt read whole thing at first
Edit: one mistake fixed
[Edited by - Julian90 on October 8, 2006 11:07:24 PM]
<snip>
Yeah, read Julian's code.
Except, the null char is '\0' (you had the wrong slash)
The '1' and 'str.capacity' arguments should be swapped. Not a big deal, but the first argument of the middle two is the size of each block, and the second of the middle two arguments is the number of blocks. I think it makes more sense to read a single block of 100 bytes, rather than 100 blocks of a single byte each. Again, it doesn't change the working of that function in this situation, but it's probably something everyone should remember.
Yeah, read Julian's code.
Except, the null char is '\0' (you had the wrong slash)
fread(strings.str, 1, strings.capacity, file);
The '1' and 'str.capacity' arguments should be swapped. Not a big deal, but the first argument of the middle two is the size of each block, and the second of the middle two arguments is the number of blocks. I think it makes more sense to read a single block of 100 bytes, rather than 100 blocks of a single byte each. Again, it doesn't change the working of that function in this situation, but it's probably something everyone should remember.
also just to point out i didnt include any error checking and i pretend that each new string starts on a new line but thats easy to change
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement