Thread: fgets help

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    24

    fgets help

    I'm making a program that has to read in and manipulate a pgm file. I need to make sure the first line in the file is P2, not P5. How would i go about doing this?

    so far i have something like this:

    Code:
    int continue;
    FILE *in;
    char buffer[MAXSIZE];
    
    in = fopen(image.pgm, "r");
    
    fgets(buffer, MAXSIZE, in);
    
    if( buffer[1] != 2 )
    {
        printf("Incorrect data type.\n");
        exit(0);
    }
    When i print out buffer[1] i get some big ass number. What am i doing wrong?

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You want to compare the character '2', not the numeric value 2. The value '2' in ASCII for example is 50.
    Code:
    if (buffer[1] != '2')
    Also, your filename needs to be in quotes.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    Quote Originally Posted by cwr
    You want to compare the character '2', not the numeric value 2. The value '2' in ASCII for example is 50.
    Code:
    if (buffer[1] != '2')
    Also, your filename needs to be in quotes.
    ahh thanks. And i know about the file name, I actually have a variable there, but changed it just so it was easier to interpret...just forgot the quotes when i changed it

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    second question. I'm reading a line such as this:

    Code:
    200 100
    how would i go about setting the first number to the image height and the next to the width?

    I know this isn't right....at all, I'm just having a brain fart b/c its late, and I'm posting some code for you to work with. Its something along the lines of:

    Code:
                i=0;
                while(!isspace(buffer[i]))
                {
                    image.width = buffer;
                    i++;
                    if(isspace(buffer[i]))
                    {
                        break;
                    }
                }
    then just do the another one for the height. But how would i go about setting buffer[0] buffer[1] and buffer[2] to image.width?

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Your input is formatted as two integers, so why don't you read it in with fgets, then use sscanf with a format specifier of "%d %d"? Check the return value of sscanf and the results for validation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM