Error in using fscanf

This is a discussion on Error in using fscanf within the C Programming forums, part of the General Programming Boards category; Hello. I'm trying to create a game and in that game, the AI that I will make should fill in ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    Error in using fscanf

    Hello.
    I'm trying to create a game and in that game, the AI that I will make should fill in his TURN file depending on the STATE file of both my AI and the opponent's. The server will provide these data. I'm trying to read the STATE file and put it into my struct

    At first, I thought about making the parameters for the STATE file and TURN file included in structs.(Actually, I'm supposed to use it this way, lol)

    Code:
    struct Statefile{
    
    	char*	tank1Fired; 
    	char*	tank2Fired; 
    	char*	tank1Alive;
    	char*	tank2Alive;
    	int		mapDimensionsX;
    	int		mapDimensionsY;
    	int		tank1PositionX;
    	int		tank1PositionY;
    	int		tank2PositionX; 
    	int		tank2PositionY; 
    	int		shellVelocity; 
    	int		initialAmmo;
    	int		initialFuel;
    }State;
    struct Statefile *s =&State;
    
    	struct Turnfile{
    		char* firing;
    		int targetX;
    		int targetY;
    		char* movement;
    	} Turn;
    	struct Turnfile *p = &Turn;
    I have made a sample STATE file so I would "fscanf" it. So, I decided to test and check if I'm able to read the first line. My STATE file looks like this:

    mapDimensions 32 24
    shellVelocity 10
    initialAmmo 100
    initialFuel 50
    tank1Position 1 12
    tank2Position 30 12
    tank1Fired false
    tank2Fired false
    tank1Alive true
    tank2Alive true
    And this is how my code look like:

    Code:
    FILE *outfile, *infile;
    char unwanted[10];
    char buffer1 [30];
    char buffer2 [30];
    int  t=1;
    
    sprintf(buffer1, "p%dt%03d.state", i, t); /* i is the number of player, which is the command line argument's number located to my programm */
    infile = fopen (buffer1,"r");
    fscanf(infile, "%s %d %d", unwanted[1], &s->mapDimensionsX, &s->mapDimensionsY);
    these captions are unwanted as my programme would just fill in numbers/states

    mapDimensions
    shellVelocity
    initialAmmo
    initialFuel
    tank1Position
    tank2Position
    tank1Fired
    ...


    My problem:
    " The variable 'unwanted' is being used without being defined " is the message I receive from running my programme.

    Is there another way to just obtain the numbers/states and put them into my struct?
    Thanks for reading =)
    Last edited by nawaf; 10-22-2010 at 05:39 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    fscanf(infile, "%s %d %d", unwanted, &s->mapDimensionsX, &s->mapDimensionsY);
    or just use
    fscanf(infile,%*s %d %d",&s->mapDimensionsX, &s->mapDimensionsY);

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    thanks so much =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf fails?
    By erjoalgo in forum C Programming
    Replies: 13
    Last Post: 04-05-2010, 06:48 AM
  2. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-29-2008, 12:11 AM
  3. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  4. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 02:32 PM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 02:22 PM

Tags for this Thread


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