-
file access problem
I have a chat system that I have made and the server supports reserved usernames. Everytime a user logs in the system opens the user file and checks for the user permissions. After several times of doing this the fopen command seg faults. I do not know why as I have examined the code and it is properly being closed everytime.
I even tried leaving the file open throughout program execution and using fseek to reset the position to 0 in the file. However now the fseek causes the crash and it is not ever closed.
I was wondering if anyone had any idea as to why this would happen
pseudocode would be
Code:
FILE *data = NULL;
data = fopen(filename,"r");
if(data == NULL)
{
return -1;
}
while(!feof(data))
{
//read data with fgets in this case
}
fclose(data)
-
Why don't you just read the file once at the beginning and store the data into an array/linked-list or other container of some sort (STL vector/list/set for example). From that point on, any request to check permissions just needs to search through the container for the user in question.
-
I plan on doing that, I still was wondering though why it did that
-
its prolly cause of "eof" and how come you dont use c++ file i/o
such as fstream?
-
fread and stuff is habit from C-style which to me seems better, I can do more low level stuff with C-style and its faster
-
This FAQ gives information about the feof problem: http://faq.cprogramming.com/cgi-bin/...&id=1043284351
And when you say "faster", do you mean faster to develop, faster to re-use, faster to maintain, faster to extend, or faster in the timing tests you've done or seen others do?
-
mainly just timing tests, and I think its faster to write too. I mean it is shorter to use the fstream I just have been in the habit of using fopen and stuff so its faster for me
-
thanks for the link
makes sense now