Thread: reading int into 2-d char array

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    reading int into 2-d char array

    In my program I'm trying to read a text file that contains integers into a 2-D char array. I believe my problem is that fgetc reads chars and not ints so when I try to print the value it shows ascii value.

    The text files looks like this:
    11111111111111111111111111111111
    10000000000000000000000000000001
    12010011111111111111111111001001
    10110000000000000000000000001101
    10001100111111111111111100110001

    and my loadmap function looks like this:

    Code:
    char map[24][32];
    
    void loadMap(void)
    {
    	FILE *fp;
    	int i,j;
    	char ch;
    
    	fp = fopen("map01.txt","r");
    	if(!fp)
    	{
    		printf("Failed to load map01.txt");
    		getch();
    		exit(1);
    	}
    	for(i = 0; i < MAXY; i++)
    	{
    		for(j = 0; j <MAXX; j++)
    		{
    			ch = fgetc(fp);
    		}
    	}
    	getch();
    	fclose(fp);
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I believe my problem is that fgetc reads chars and not ints so when I try to print the value it shows ascii value.
    No, fgetc actually returns an int.

    Code:
    for(i = 0; i < MAXY; i++)
    	{
    		for(j = 0; j <MAXX; j++)
    		{
    			ch = fgetc(fp);
    		}
    	}
    No, your problem is that you aren't doing anything with 'ch'. Sure, you're sticking something into it, but then what? You don't assign the value of ch to your array, so it gets overwritten the next cycle through in the loop.

    Try something like:

    array[i][j] = fgetc(fp);


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

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by quzah

    Try something like:
    array[i][j] = fgetc(fp);
    It'd be better to store the return in an int of it's own, then check it for validity first, before moving it to the array. That way, if it's EOF, you don't put it into the array and confuse it with the real data.
    Usual stuff:
    Code:
    while ((c = fgetc(fp)) != EOF)
        array[i][j] = c;
    /* do something with i and j */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    The thing is that fgetc reads integers as if they were characters and I want to store the integers in a 2-d character array. I'm having the same sort of problem as post here:
    http://www.cprogramming.com/cboard/s...threadid=17314

    expect for I'm using a 2-d char array.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    So, let me check this... if you have a file with this:
    >123456
    you want the int array to have this:
    a[1] = 1
    a[2] = 2
    a[3] = 3
    ..and so on. Correct? (well, you might want a 2-d version of the array).

    If so, you can do something like this:
    >if (isdigit(c)) map[row][col] = c - '0';

    This simply checks that c is a number, and then subtracts it from '0', the character constant that represents zero.

    If this isn't what you want, just ask again.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by lambs4
    The thing is that fgetc reads integers as if they were characters and I want to store the integers in a 2-d character array. I'm having the same sort of problem as post here:
    http://www.cprogramming.com/cboard/s...threadid=17314

    expect for I'm using a 2-d char array.
    fread simply reads one byte from the file stream. Period. If that byte is say... 65 decimal value, then you end up with the letter 'a' (or whatever it is, I don't feel like looking it up right now) if you cast it to a character, or if you simply treat it like an integer, it will be 55.

    fread doesn _nothing_ to the data other than simply read a single byte from the file. That is all. Nothing more, nothing less.

    It's up to you to ensure that the data is valid in the file.

    If you create a text file that contains a single character '1', (without the quotes) fread will simply read the character value of '1', and return it to you. It does not return decimal one. It returns character value of one.

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

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Originally posted by Hammer
    So, let me check this... if you have a file with this:
    >123456
    you want the int array to have this:
    a[1] = 1
    a[2] = 2
    a[3] = 3
    ..and so on. Correct? (well, you might want a 2-d version of the array).

    If so, you can do something like this:
    >if (isdigit(c)) map[row][col] = c - '0';

    This simply checks that c is a number, and then subtracts it from '0', the character constant that represents zero.

    If this isn't what you want, just ask again.
    No, I want to store the 123456 in a 2-d char array so it be something like:

    chArray[0][0] = 1
    chArray[0][1] = 2
    chArray[0][2] = 3

    Here's the code I've tried writting based the information that quzah has given:

    Code:
    void loadMap(void)
    {
    	FILE *fp;
    	int i,j;
    	char ch,temp[2],buffer[786];
    
    	fp = fopen("map01.txt","r");
    	if(!fp)
    	{
    		printf("Failed to load map01.txt");
    		getch();
    		exit(1);
    	}
    	i = 0;
    	while((ch = fgetc(fp)) != EOF)
    	{
    		if(ch == '\n')
    			continue;
    		temp[0] = ch;
    		temp[1] = '\0';
    		buffer[i] = atoi(temp);
    		i++;
    	}
    	for(i = 0; i < MAXY; i++)
    	{
    		for(j = 0; j <MAXX; j++)
    		{
    			map[i][j] = buffer[i];
    			printf("%d",map[i][j]);
    		}
    	}
    	getch();
    	fclose(fp);
    
    }
    Last edited by lambs4; 07-24-2002 at 10:45 AM.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    lambs... a couple of points on your code:

    >ch and EOF
    fgetc() returns an int, so you must assign it's return to an int, not a char as you have done. This is important because EOF is an int with value of -1. If you assign to a char, you will loose the ability to test against EOF correctly.

    >atoi()
    For what your doing, you don't need to use atoi(), it's extra effort with no gain, imo.

    Here's a version that does what you want (I think it does anyway, if not you can adapt it to suit). It doesn't do any bounds checking on the array, a thing which should do be done.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
        FILE    *fp;
        int     row, col, c;
        char    map[2][10] = { { 0, 0 } };
    
        if ((fp = fopen("map01.txt", "r")) == NULL)
        {
            perror("map01.txt");
            return(1);
        }
    
        row = 0;
        col = 0;
        while ((c = fgetc(fp)) != EOF)
        {
            if (c == '\n')
            {
                row++;
                col = 0;
            }
            else
            {
                if (isdigit(c)) 
                {
                    map[row][col] = (char) (c - '0');
                    col++;
                }
            }
        }
    
        fclose(fp);
    
        for (row = 0; row < 2; row++)
            for (col = 0; col < 10; col++) 
                printf("map[%d][%d] = %02x\n", row, col, map[row][col]);
    
        return(0);
    }
    
    /* The input file contains:
    123456
    789012
    
    And the output looks like this:
    map[0][0] = 01
    map[0][1] = 02
    map[0][2] = 03
    map[0][3] = 04
    map[0][4] = 05
    map[0][5] = 06
    map[0][6] = 00
    map[0][7] = 00
    map[0][8] = 00
    map[0][9] = 00
    map[1][0] = 07
    map[1][1] = 08
    map[1][2] = 09
    map[1][3] = 00
    map[1][4] = 01
    map[1][5] = 02
    map[1][6] = 00
    map[1][7] = 00
    map[1][8] = 00
    map[1][9] = 00
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM