Question: When I read the header of a file ( P 6 1 0 0 #comment #comment 2 0 0 #comment 2 5 5 ) it

When I read the header of a file ( P6100#comment #comment 200 #comment 255) it writes to the output file as : (P610020021953 #comment 255) this is incorrect as it should be ignoring all comments, here is the code i am using can you help fix it: /************************Preston Johnson *CPSC 2310 Fall 24*Username: prestoj *Instructor: Dr. Yvon Feaster **********************/ #include "ppmUtil.h" void read_header(FILE *file, header_t *header){ ignore_comments(file); fscanf(file,"%2s", header->type); ignore_comments(file); fscanf(file,"%u", &header->width); ignore_comments(file); fscanf(file,"%u", &header->height); ignore_comments(file); fscanf(file,"%u", &header->maxVal); } void write_header(FILE *file, const header_t *header){ fprintf(file,"%s
", header->type); fprintf(file,"%u
", header->width); fprintf(file,"%u
", header->height); fprintf(file,"%u
", header->maxVal); } void read_pixels(FILE *file, pixel_t **pixels, const header_t *header){*pixels = allocate_pixels(header); fread(*pixels, sizeof(pixel_t), header->width * header->height, file); } void write_pixels(FILE *file, const pixel_t *pixels, const header_t *header){ fwrite(pixels, sizeof(pixel_t), header->width * header->height, file); } pixel_t *allocate_pixels(const header_t *header){ return (pixel_t *)malloc(header->width * header->height * sizeof(pixel_t)); } void free_pixels(pixel_t *pixels){ free(pixels); } void ignore_comments(FILE *file){ int ch; while ((ch = fgetc(file))!= EOF){ if (ch =='#'){ while ((ch = fgetc(file))!='
' && ch != EOF); } else { ungetc(ch, file); break; }}}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!