Thread: How to ignore the pound sign?

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

    How to ignore the pound sign?

    I'm writing a program that will read files that will check to see if it's the right type (PGM, type P2), within the max height and width (200 each), max greyscale value, and can later be smoothed out. Right now i'm having trouble trying to figure out how to ignore strings denoted by a pound sign

    P2
    #blahblah this is a comment
    height width grayscalevalue


    Because comments can be anywhere

    P2
    height width
    #comment
    grayscale value

    How would I go about this? (I have the basic open/read/close file thing down, while looping with fgets to go through the string...) And then to later take the values individually of the height, width, and grayscale? I think we're supposed to use sscanf(string, format, pointer) or fscanf(file..??)

    Er, stuck on this for hours. I've been reading the man pages on them but I think if you could point me to examples, they would be more useful. Thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It can be pretty easy if the comment always starts the line.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char line[128];
          while ( fgets(line, sizeof line, file) )
          {
             if ( line[0] != '#' )
             {
                fputs(line, stdout);
             }
          }
          fclose(file);
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* file.txt
    P2
    height width
    #comment
    grayscale value
    */
    
    /* my output
    P2
    height width
    grayscale value
    */
    Last edited by Dave_Sinkula; 12-02-2005 at 10:17 AM. Reason: Added color.
    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
    Oct 2005
    Posts
    8
    That's that was super helpful! You really didn't have to do all that but I appreciate it a lot.

    Right now, I'm having a problem trying to save a file into another file that is going to be named by the stdin.


    Code:
    void savePGM(FILE *infile)
    {
       FILE *outfile;
       char string[70], buffer[70];
       
       printf("Please provide a file name: ");
       scanf("%s", string);
       outfile = fopen(string, "w");
    
       sprintf(buffer, "%s", fgets(string, 70, infile));
    
    }
    When I run this in my program, I get to the point where i can input the string and then open it like so. But what I'm confused with is how to copy it string by string (70 characters) into the file that I made--my teacher was telling me to use--I'm not sure sprintf/fprintf or sscanf/fscanf to copy it into the file because it's PGM...and maybe there might be an issue with newline...? Well, the program compiles and creates the file but it's empty. Any help, please?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    One of these days your function may end up looking like this.
    Code:
    void savePGM(FILE *infile)
    {
       FILE *outfile;
       char filename[70], buffer[70];
    
       printf("Please provide a file name: ");
       fflush(stdout);
       if ( fgets(filename, sizeof filename, stdin) != NULL )
       {
          char *newline = strchr(filename, '\n');
          if ( newline != NULL )
          {
             *newline = '\0';
          }
       }
       outfile = fopen(filename, "w");
       if ( outfile != NULL )
       {
          while ( fgets(buffer, sizeof buffer, infile) != NULL )
          {
             fputs(buffer, outfile);
          }
          fclose(outfile);
       }
       else
       {
          perror(filename);
       }
    }
    [edit]Or perhaps:
    Code:
       outfile = fopen(filename, "wb");
       if ( outfile != NULL )
       {
          size_t read, written;
          do {
             read = fread(buffer, 1, sizeof buffer, infile);
             written = fwrite(buffer, 1, read, outfile);
          } while ( read == sizeof buffer );
          fclose(outfile);
       }
    Last edited by Dave_Sinkula; 12-02-2005 at 11:19 PM.
    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.*

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    8
    I've been fiddling with it. It's beautiful, but unfamiliar, and neither version work with my program. Here is what I've got so far:

    Code:
    void savePGM(FILE *infile)
    {
       FILE *outfile;
       char filename[70], buffer[70];
    
       printf("Please provide a file name: ");
       fscanf(stdin, "%s", filename);
       outfile = fopen(filename, "w");
    
       while(fscanf(infile, "%s", buffer) != NULL)
       {
          fprintf(outfile, "%s", buffer);
       }
       fclose(outfile);
    }
    Right now, it is willing to create the file for me, but I do not it believe it is loopin correctly. I think I'm using fscanf wrong--that's why it's always asking for my input. I've tried

    fgets(buffer, sizeof buffer, infile) != NULL

    But as you probably know already, there were issues with the compatibilities of data types. :-/

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fscanf is going to have the same problem that fgets with regards to "compatibilities". Actually, it's worse, becuase it will be skipping whitespace. You should be either:
    a) Using something like fgetc to read one byte at a time.
    b) Using something like fread to read one block at a time.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sign ' is the same as \' ?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 11-23-2007, 07:32 AM
  2. My own itoa()
    By maxorator in forum C++ Programming
    Replies: 18
    Last Post: 10-15-2006, 11:49 AM
  3. Handle on MSN Messenger's Sign In "button"?
    By jmd15 in forum Windows Programming
    Replies: 3
    Last Post: 07-16-2005, 09:28 PM
  4. Sign Up!: The Third Round, both contests
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 54
    Last Post: 07-20-2002, 05:46 PM
  5. pound sign
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 02:01 PM