Thread: Error in using fscanf

  1. #1
    Registered User
    Join Date
    Oct 2010

    Error in using fscanf

    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)

    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;
    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:

    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


    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 04:39 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    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
    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, 05:48 AM
  2. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  3. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  4. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 01:32 PM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM

Tags for this Thread

Website Security Test