Thread: How to take a SINGLE UNKOWN character from a string

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    Question How to take a SINGLE UNKOWN character from a string

    I have a character array, or string, that has a series of...'X'...' '...'F'...'S'...

    I have a maze (only one level) where the walls are marked with X's, the walkable space is marked with a space and the start and finish are S and F respectively. I have to read in the maze file in, then make it a 2D array. here is some of my code...


    Code:
    typedef struct MazeStruct
    {
    	char **maze;	     // 2D maze map
    	int row;		     // row of the map
    	int col;		     // column of the map
    	int start_row;	     // row of the starting point
    	int start_col;	     // column of the starting point
    } Maze;
    
    //this is the prototype given to me by my instructor. It returns a 1 if successful or a -1 if not.
    int GenerateMaze(char* filename, Maze *Mymaze)
    {
    	if ((fr=fopen(filename,"r")) == NULL) {
    		printf("%s could not be found\n",filename);
    		return -1;
    	}
    	char coords[30];
    	char *tokptr;
    	int row, col;                              **the file has the dimension of the maze at the top
    	fgets(coords,15,filename);       **so i use strtok() when i know what the 'token' is
    	tokPtr = (strtok(coords, ","));
    	row = atoi(tokptr);		//get rows
    	tokPtr = (strtok(NULL, ","));
    	col = atoi(tokptr);		//get cols
    
    	Mymaze->row = row;
    	Mymaze->col = col;
    
    	//Maze allocation                                **not sure how accurate this chunk of code is
    	char **Mz;                                          
    	int i;
    	Mz = (int**)malloc(row*sizeof(int*))
    	for (i=0; i<col; i++)
    		Mz[i] = (int*)malloc(col*sizeof(int));
    
    	char line[100];
    	int c,k, counter = 0;
    	
                                **this is where I was going to put the maze into the 2Darray
                                **but idk how to take one character at a time
    	for (c=0; c<Mymaze->row;c++) {          
    		fgets(line, sizeof(line), filename);
    			
    			for (k=1; k<Mymaze->col; k++)
    	}
    }
    Also, if there is a way to take a single character at a time from a file, that would work as well...

    Thanks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgetc perhaps? Any basic C book should cover this in the chapter about files.


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

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    fgetc - C++ Reference

    ^^^^says that fgetc() returns an integer. How would that work when i need to put it into a character array?
    Also, when fgetc() reaches the end of the line, does it automatically go down to the next line?
    Last edited by stmty9; 11-30-2010 at 10:14 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Characters have integer values. Basically, a valid character gets returned in the range of the character's possibilities, otherwise a different value gets returned for error states.

    Go read about data types and the ranges they store.

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

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    So i could use the code...
    Code:
    int k;
    char n;
    k = fgetc(filename);
    n = (char)k;
    to turn the output of fgetc() into a character, correct? or would i have to match up the 0-255 values of the characters i need?
    or could i even just do...

    Code:
    char n;
    n = fgetc(filename);
    Last edited by stmty9; 11-30-2010 at 10:59 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int c;
    while( ( c = fgetc( filepointer ) != EOF )
        printf( "Read \'%c\': %d.\n", c, c );
    A char is just an int that's small.

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

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > fgets(coords,15,filename);
    Did you even compile this!?
    fr is the opened file handle, not filename (which is a char pointer)

    > **not sure how accurate this chunk of code is
    Well
    1. Remove all the casts (but then again, you're compiling this as a C++ program aren't you)
    2. change all the (int) in sizeof to char, since your base type in the pointer declaration is char
    3. the for loop has the wrong limit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    its standard C

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    OK, so post your latest attempt if you're still stuck.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. How to read single characters in a string
    By MaxxMan-X in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2005, 02:52 PM
  4. Printing single character from a string
    By TJJ in forum C Programming
    Replies: 4
    Last Post: 11-05-2003, 06:25 PM

Tags for this Thread