Help needed with File IO and structures (current code inside)

This is a discussion on Help needed with File IO and structures (current code inside) within the C Programming forums, part of the General Programming Boards category; Hi, i know this has been asked a few times before, but even all the tutorials i have looked at ...

  1. #1
    The Determined
    Join Date
    Nov 2005
    Location
    England
    Posts
    5

    Help needed with File IO and structures (current code inside)

    Hi, i know this has been asked a few times before, but even all the tutorials i have looked at and past threads can't make me understand.

    Basically what i have got is a structure and a file which is created when a user 'logs in'. I am trying to read from this file directly into the structure, and then send it back again when the client quits to update any changed information. Here is what i have:

    Code:
    //The Structure
    
    typedef struct
    {
    	int			level;	//user level
    	int			points;	//user points
    } client_aldarninfo_t;
    
    //The logon function (ignore the fact it wont create the file if there isn't already one, also qboolean is valid)
    
    qboolean client_login(edict_t *ent)
    {
    	FILE *file;
    
    	if ((file = fopen(ent->client->pers.netname, "r")) == NULL) {
    	gi.cprintf (ent, PRINT_HIGH, "Error: can't open file.\n");
        return 1;
    	} 
    	else 
    	{
    
    		int size = sizeof(client_aldarninfo_t);
    
    		//file opened, lets read it into the structure
    		fread(&ent->client->aldarn, size, 2, file);
    
    		gi.cprintf (ent, PRINT_HIGH, "Hey, your level is: %d, and your points are: %d\n", ent->client->aldarn.level, ent->client->aldarn.points);
    
    		return 0;
    	}
    }
    ent->client->pers.netname is the clients username.

    Here is an example of what happens with "example.txt" having this contents:

    Code:
    Code:
    555
    1
    Prints out: level: (big ASCII number), points: 49 (ASCII for 1)
    Another example:

    Code:
    Code:
    5
    1
    Prints out: level: (big ASCII number), points: 0

    Basically, i have no idea how to assign the variables in the structure to exactly the numbers in the text file, seeing as i have no way of knowing the size of them before hand. Any help is appreciated, and feel free to e-mail/instant message. Thanx!!

    - Aldarn

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    If the file looks like you posted, then it cannot be read using fread(). fread() is for binary files, yours is a text file. try using fscanf() instead of fread()
    Code:
    fscanf(file, "%d %d", &ent->client->aldarn.level, &ent->client->aldarn.points);

  3. #3
    The Determined
    Join Date
    Nov 2005
    Location
    England
    Posts
    5
    Aha, thanks a lot! I'll post the results when im done if anyone else is interested / ever gets the same problem. Im going to try the dreaded writing now too lol .

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Code:
    fread(&ent->client->aldarn, size, 2, file);
    I'm puzzled by the above statement. It indicates that you are reading 16 bytes into the memory address of ent->client->aldarn since your stock client_aldarninfo_t struct is 8 bytes in length. Thus, I would assume that you have allocated storage for two client_aldarninfo_t structs in your ent struct?

    If this is the case, then I would assume you need two fscanf's to accomodate both allocated structs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and file io
    By mungyun in forum Linux Programming
    Replies: 1
    Last Post: 05-18-2002, 02:48 PM
  2. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21