Thread: file access problem

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    47

    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)

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    47
    I plan on doing that, I still was wondering though why it did that

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    its prolly cause of "eof" and how come you dont use c++ file i/o
    such as fstream?

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    47
    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

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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?

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    47
    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

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    47
    thanks for the link

    makes sense now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM