I have an assignment that involves steganography. We have to read in a header of the picture file and all of its binary content and then modify certain parts of it. I'm having issues with reading in the header of a .ppm files which is written like this:

P6
#comment text
(width) (height)
color depth

Here's my issue. I can read in a file that doesn't have the comment line perfectly or I can read a file that does have a comment perfectly but I cannot seem to write code that handles both cases. The code that I currently have can only read headers without the comment line. Can someone help me please?

#include <stdio.h>
#include <stdlib.h>
#include "stegoHeader.h"

void reader(FILE *);

int main(int argc, char **argv){
  char *p;
  FILE *fp;
  char *fname;

  /*Check to see if we have the proper number of parameters*/
  if(argc < 4){
    usage(argv[0]);
    exit(1);
  }

  /*assigns the first parameter to fname*/
  fname = argv[2];

  /*Store our message to be encoded into our file*/
  p = argv[1];

  /*If the file can't be opened, return an error message*/
  if((fp=fopen(fname, "rb+"))==NULL){
    printf("error opening the file '%s'.\n", fname);
    exit(2);
  }else {
    printf("argv 1 is %s. argv 2 is %s. argv 3 is %s.\n", p/*argv[1]*/, argv[2], argv[3]);    
    reader(fp);
  }
  /*If the file can be opened, send it to be read.*/

  /*Close the file*/
  fclose(fp);
  return 0;
}

/*Shows user how to use the program if it isn't called correctly*/
void usage(char *fname){
  printf("usage: %s message inputfile outputfile\n", fname);
}

void reader(FILE *fp){
  headerInfo t;
  char c;
  int i = 0;
  char input[300];

  /*Check to see if this is the proper file format*/
  fscanf(fp, "%c%d", &t.letter, &t.magicNum);
  if((t.letter != 'P') || (t.magicNum != 6)){
    printf("This is not the proper file format\n");
    exit(1);
  }

  fgets(input, 299, fp);
printf("input[0] is %c", input[1]);

/*   /\*If the next line is a comment, read it and ignore it*\/ */
/*   fgets(input, sizeof(input-1), fp); */
/*     if(input[0] == '#'){ */
/*       i = 1; */
/*       while(input[0] == '#'); */
/*       //fscanf(fp, "%d %d", &t.width, &t.height); */
/*     } */
/*     i = 0; */


 /*If we are not reading a comment line, assign the data to the appropriate variables*/
   if(i==0){
 fscanf(fp, "%d %d", &t.width, &t.height);
 printf("height is %d. width is %d\n", t.height, t.width);
 fscanf(fp, "%d", &t.colorDepth);
 printf("color depth is %d\n", t.colorDepth);
   }
  
}

Nevermind, i figured out what was wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.