problem I am having with fscanf()

This is a discussion on problem I am having with fscanf() within the C Programming forums, part of the General Programming Boards category; Well I am trying to write a program for a class and part of it I will have to scan ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    15

    problem I am having with fscanf()

    Well I am trying to write a program for a class and part of it I will have to scan in information that is supposed to represent a maze. This information is included in a txt file (example below) and I am just trying to read it in to a **char and print it out to make sure it worked. There is probably a better way I could go about the program but since I just started and am already having this problem I would like to find out what is causing it. anyways...

    Program:
    Code:
    int main () {
    	FILE *mazeSpecs = fopen("maze2a.txt", "r");
    	int i, j, rowSize, colSize;
    
    	fscanf(mazeSpecs, "%d", &rowSize);
    	fscanf(mazeSpecs, "%d", &colSize);	
    	char **mazeArray = (char **)checked_malloc((rowSize)*sizeof(char *));
    	for(i = 0; i<rowSize; i++) {
    		mazeArray[i] = (char *)checked_malloc((colSize)*sizeof(char));
    		for(j = 0; j<colSize; j++) 
    			fscanf(mazeSpecs, "%s", &mazeArray[i][j]);
    	} fclose(mazeSpecs);
    
    	for(i = 0; i<rowSize; i++) {
    		for(j = 0; j<colSize; j++)
    		printf("%s ", &mazeArray[i][j]);
    	printf("\n");
    	}
    
    	free(mazeArray);
    	return 0;
    }


    The output is:

    Code:
    ESEEESEEEW SEEESEEEW EEESEEEW EESEEEW ESEEEW SEEEW EEEW EEW EW W
    SESEEEEWEW ESEEEEWEW SEEEEWEW EEEEWEW EEEWEW EEWEW EWEW WEW EW W
    ESEWESESSS SEWESESSS EWESESSS WESESSS ESESSS SESSS ESSS SSS SS S
    SEESSESEWSN EESSESEWSN ESSESEWSN SSESEWSN SESEWSN ESEWSN SEWSN EWSN WSN SN
    EEWSESEEEWN EWSESEEEWN WSESEEEWN SESEEEWN ESEEEWN SEEEWN EEEWN EEWN EWN WN
    SESESESNESW ESESESNESW SESESNESW ESESNESW SESNESW ESNESW SNESW NESW ESW SW
    SEEWSSSEESWN EEWSSSEESWN EWSSSEESWN WSSSEESWN SSSEESWN SSEESWN SEESWN EESWN ESWN SWN
    SSEEWSNEEWN SEEWSNEEWN EEWSNEEWN EWSNEEWN WSNEEWN SNEEWN NEEWN EEWN EWN WN
    SSEEESEEEW SEEESEEEW EEESEEEW EESEEEW ESEEEW SEEEW EEEW EEW EW W
    SEEWEWESESW EEWEWESESW EWEWESESW WEWESESW EWESESW WESESW ESESW SESW ESW SW
    SNESNEEEWN NESNEEEWN ESNEEEWN SNEEEWN NEEEWN EEEWN EEWN EWN WN N
    ESEWESESESW SEWESESESW EWESESESW WESESESW ESESESW SESESW ESESW SESW ESW SW
    SEEEWEEWSSN EEEWEEWSSN EEWEEWSSN EWEEWSSN WEEWSSN EEWSSN EWSSN WSSN SSN SN
    EEWEEWEWSSN EWEEWEWSSN WEEWEWSSN EEWEWSSN EWEWSSN WEWSSN EWSSN WSSN SSN SN
    EESEEEEEWSN ESEEEEEWSN SEEEEEWSN EEEEEWSN EEEEWSN EEEWSN EEWSN EWSN WSN SN
    SEWEWESNEWN EWEWESNEWN WEWESNEWN EWESNEWN WESNEWN ESNEWN SNEWN NEWN EWN WN
    EEWEWSESESW EWEWSESESW WEWSESESW EWSESESW WSESESW SESESW ESESW SESW ESW SW
    EEEEEESEWN EEEEESEWN EEEESEWN EEESEWN EESEWN ESEWN SEWN EWN WN N
    EEWEESNEESW EWEESNEESW WEESNEESW EESNEESW ESNEESW SNEESW NEESW EESW ESW SW
    EEEEWEEEWN EEEWEEEWN EEWEEEWN EWEEEWN WEEEWN EEEWN EEWN EWN WN N
    but the text file is:

    Code:
    20 10
    
    ES SW E ESW EW SW ES EW ESW W
    SN EN SW ESN EW EWN ESWN W EN W
    ESN SW EN WN ES SW EN SW S S
    SN EN EW SW SN EN SW EN WN SN
    ESN ESW W SN EN SW EN ESW ESW WN
    
    SN EN SW EN SW ESN SW N EN SW
    SN ES EWN W SN SN SN E EW SWN
    SN SN E EW WN SN N ES EW WN
    SN SN ES EW EW SWN ES EWN ESW W
    SN ESN ESWN W ES WN EN SW EN SW
    
    SN N EN SW N ES EW EWN W N
    ESN SW E WN E SWN E SW ES SW
    SN EN EW ESW W EN ESW WN SN SN
    EN ESW W EN EW W ESN W SN SN
    ES EWN SW E ESW EW EWN ESW WN SN
    
    SN ES WN ES WN ES SW N E WN
    EN EWN W ESN W SN EN SW ES SW
    E ESW EW EWN EW EWN SW EN WN N
    ES EWN W ES EW SW N ES EW SW
    EN EW EW EWN W EN EW EWN W N

    Looking at it in an attempt to find what is going wrong I noticed every first and last char of a row is correct, while every last string of a row is correct as well. So does anyone have any ideas as to how I can fix this?

    Thanks for any assistance!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    For a 2D array of strings, you need a 3D array of chars.

    Minimal adjustments:
    Code:
    	char ***mazeArray = malloc(rowSize * sizeof *mazeArray);
    	for(i = 0; i<rowSize; i++) {
    		mazeArray[i] = malloc( colSize * sizeof *mazeArray[i]);
    		for(j = 0; j<colSize; j++) 
          {
             mazeArray[i][j] = malloc(5 * sizeof *mazeArray[i][j]);
    			fscanf(mazeSpecs, "&#37;s", mazeArray[i][j]);
          }
    	} fclose(mazeSpecs);
    
    	for(i = 0; i<rowSize; i++) {
    		for(j = 0; j<colSize; j++)
    		printf("%-4s ", mazeArray[i][j]);
    	printf("\n");
    	}
    Last edited by Dave_Sinkula; 09-29-2007 at 09:54 PM. Reason: Touchups: the necessary and the superficial.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    15
    Ah! it seems so obvious, but at the same time obviously it was not since I spent a lot of time getting frustrated with it. I really appreciate the help, it works perfectly now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Replies: 4
    Last Post: 01-10-2006, 12:23 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. Replies: 12
    Last Post: 10-17-2005, 06:49 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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