Sign in to follow this  
josh howard

linked lists and file i/o in C

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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


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[i].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[i].str, 1, strings[i].capacity, file);
strings[i].str[strings[i].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]

Share this post


Link to post
Share on other sites
<snip>

Yeah, read Julian's code.

Except, the null char is '\0' (you had the wrong slash)

fread(strings[i].str, 1, strings[i].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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this