Thread: How to load numbers from a text file?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    How to load numbers from a text file?

    Hello, I am have been working on A sort of Custom games for my game, Revamped. I have successfully written a line of information into a text file, but I am having troubles reading the information, and loading them into integers.

    Here is what the Load code looks like:

    Code:
    /*
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    Load
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    */
    void Load_GT (void)
    	{
     	 FILE *pFile;
     	 int num_breaks = 0;
     	 int i;
    	pFile = fopen (va("%s/gt/var_%i.variant",fs_gamedir, current_option), "r");
    
    	if (pFile == NULL) return;
    
    				fscanf(pFile, "%s\n", &load_option[VARIANT_NAME]);
    				fscanf(pFile, "%i\n", &load_option[GAMETYPE]);
    
    				fscanf(pFile, "%i\n", &load_option[START_PLAYERS]);
    				fscanf(pFile, "%i\n", &load_option[GO_TEAM]);
    				fscanf(pFile, "%i\n", &load_option[G_WEPS]);
    				fscanf(pFile, "%i\n", &load_option[WEP_RESPAWN_TIME]);
    				fscanf(pFile, "%i\n", &load_option[NUM_ZOMBIES]);
    				fscanf(pFile, "%i\n", &load_option[NUM_DARKPLAYERS]);
    				fscanf(pFile, "%i\n", &load_option[LAST_MAN]);
    
    				fscanf(pFile, "%i\n", &load_option[PRIMARY_WEAPON]);
    				fscanf(pFile, "%i\n", &load_option[SECONDARY_WEAPON]);
    				fscanf(pFile, "%i\n", &load_option[GRENADES]);
    				fscanf(pFile, "%i\n", &load_option[INF_AMMO]);
    				fscanf(pFile, "%i\n", &load_option[BOTTOMLESS_CLIP]);
    				fscanf(pFile, "%i\n", &load_option[ABILITY]);
    				fscanf(pFile, "%i\n", &load_option[INF_ABILITY]);
    				fscanf(pFile, "%i\n", &load_option[HEADSHOTS]);
    
    				fscanf(pFile, "%i\n", &load_option[SHIELDS]);
    				fscanf(pFile, "%i\n", &load_option[RECHARGE_TIME]);
    				fscanf(pFile, "%i\n", &load_option[RECHARGE_RATE]);
    				fscanf(pFile, "%i\n", &load_option[DAMAGE_RESISTANCE]);
    				fscanf(pFile, "%i\n", &load_option[RESPAWN_TIME]);
    				fscanf(pFile, "%i\n", &load_option[GRAVITY]);
    				fscanf(pFile, "%i\n", &load_option[SPEED]);
    				fscanf(pFile, "%i\n", &load_option[JUMP_HEIGHT]);
    				fscanf(pFile, "%i\n", &load_option[ASSASINATION]);
    				fscanf(pFile, "%i\n", &load_option[DAMAGE_MODIFIER]);
    				fscanf(pFile, "%i\n", &load_option[MELEE_MODIFIER]);
    				fscanf(pFile, "%i\n", &load_option[WEAPON_PICKUP]);
    				fscanf(pFile, "%i\n", &load_option[SENSOR]);
    				fscanf(pFile, "%i\n", &load_option[P_SKIN]);
    				fscanf(pFile, "%i\n", &load_option[LIVES]);
    
    
      	fclose (pFile);
    	Set_Options_Value (); //Takes all the load_option[] and applies them
    }
    That is in the Same exact order that I saved it in the text file. I figured it would work because it just goes straight down the line, but I was wrong.

    This is what the text file looks like. It is saved 100% correctly.

    Default
    1
    0
    0
    0
    2
    30
    2
    1
    1
    1
    0
    3
    0
    0
    0
    0
    1
    100
    4
    1
    1
    7
    800
    200
    300
    0
    1
    1
    1
    1
    0
    0
    So is there a better Saving Convention I could use to locate specific Numbers in a text file, rather then relying on it going up to down, and a better code to Located the numbers in the text file?

    Thanks for help, if I am not clear, I will explain further.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What type of data is 'load_option'? Because you are mixing types of data, so it can't just be an array.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    load_option is an int array
    int load_option[50];
    And the Things in the arrays are #define macros.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    	fscanf(pFile, "%s\n", &load_option[VARIANT_NAME]);
    Well that's a string, so you can't just stick it into your array as an integer.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Well that particular line of code has no meaning right there. I just put it there so maybe it could go down the text file and load all the information.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It does have meaning if you have that in your code, because you will be trying to read into something that can't take what you are trying to give it (you should also be getting a compiler warning about that). You may want to combine fgets with sscanf and try that instead.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    So how would I integrate sscanf into this?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The same way you use fscanf, except instead of reading from a file, you read from a buffer. You might also consider just using a loop and walking through the array elements as you fill it. Take a look at this and this and see what you can come up with.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Picking numbers from text file
    By Scythed in forum C Programming
    Replies: 13
    Last Post: 11-09-2010, 01:50 AM
  2. Getting numbers from a text file with C
    By Help_with_C in forum C Programming
    Replies: 2
    Last Post: 10-19-2010, 04:37 PM
  3. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  4. I'd like to load a text file's contents cleanly at compile time
    By CharlieLewaska in forum C Programming
    Replies: 3
    Last Post: 01-31-2006, 11:10 AM
  5. Best way to load lines of text from a file to a combo box
    By ganonl in forum Windows Programming
    Replies: 6
    Last Post: 04-15-2004, 11:07 AM