You are here:

C++/C++ Programming

Advertisement


Question
How would I do this?
A C program lab10ex1.c is required that will modifies the greyscale values of a pgm image file as
follows:
a) Read the first line of the pgm file into a character array called filetype
b) Read the first the next three numbers into the varables w, h and val
c) Read the rest of the pixel greyscale values into a data array
d) If the data values are greater than 128 assign 255 to the array element
e) If the data values are NOT greater than 128 assign 0 to the array element
f) Write out the filetype, w,h val and array data values out to a new pgm image file

This is what i have so far
/---------------------------------------------------------------------------------/


/* Program to demonstrate how to read from a text file */

#include <stdio.h>
#define SIZE 72000

/*---------------------------------------------------------------------------*/
int main(void)
{
   int data[SIZE] = {0};
   int i = 0;        
  float avg = 0;
   int w, h, val, filetype;
   
   /* Declare a pointer to a file */
   FILE *input_file;
   FILE *output_file;
  
   /* Open a file for READ access - modify filename as required */
   input_file = fopen("sheldon.pgm", "r");

   /* check file could be opened before trying to read from file */
   if( input_file != NULL )
   {
     fscanf(input_file, "%s", &filetype);
     fscanf(input_file, "%d", &w);
     fscanf(input_file, "%d", &h);
     fscanf(input_file, "%d", &val);
     
     /* add your code here */
     
   if(
  


  }
  else
  {
     printf("Error could not open file
");
  }

  system ("pause");
  return 0;
}
/*---------------------------------------------------------------------------*/

Answer
/* Program to demonstrate how to read from a text file */

#include <stdio.h>
#include <stdlib.h>
#define SIZE 72000

/*---------------------------------------------------------------------------*/
int main(void)
{
   char data [ SIZE + 1 ] = { 0 } ;
   int i = 0;        
  float avg = 0;
   int w, h, val, filetype;
   
   /* Declare a pointer to a file */
   FILE *input_file;
   FILE *output_file;
  
   /* Open a file for READ access - modify filename as required */
   input_file = fopen("sheldon.pgm", "r");

   /* check file could be opened before trying to read from file */
   if( input_file != NULL )
   {
     /*fscanf(input_file, "%s", &filetype);
     fscanf(input_file, "%d", &w);
     fscanf(input_file, "%d", &h);
     fscanf(input_file, "%d", &val);*/

     fread ( & filetype , sizeof filetype , 1 , input_file ) ; // This can also be done with c++ fstream.  I do not recommend doing it with scanf.
     fread ( & w , sizeof w , 1 , input_file ) ;
     fread ( & h , sizeof h , 1 , input_file ) ;
     fread ( & val , sizeof val , 1 , input_file ) ;
     
     /* add your code here */

     fread ( data , sizeof data [ 0 ] , sizeof data - 1 , input_file ) ; // I even thought about just reading everything in in just one read statement.  Then I thought about it and the above just seemed more self explanatory because you would then be faced with choosing which bytes in the stream to assign to which variables.  Although it could be done that way.
     
     fclose ( input_file ) ;

  
     // ... for ( int i = 0 ; data [ i ] ; i ++ )
     for ( int i = 0 ; i < sizeof data ; i ++ ) // Come to think of it, this will be safer because one of the pixel values may be zero.
        if ( data [ i ] > 128 )
         // ... data [ i ] = 256 ;
         data [ i ] = 255 ; // Correction.
        else
         data [ i ] = 0 ;

  


  }
  else
  {
     printf("Error could not open file");
  }


  output_file = fopen ( "sheldon.out.pgm" , "w" ) ;
  fwrite ( & filetype , sizeof filetype , 1 , output_file ) ;
  fwrite ( & w , sizeof w , 1 , output_file ) ;
  fwrite ( & h , sizeof h , 1 , output_file ) ;
  fwrite ( & val , sizeof val , 1 , output_file ) ;
     
  /* add your code here */

  fwrite ( data , sizeof data [ 0 ] , sizeof data - 1 , output_file ) ;

  fclose ( input_file ) ;
     
  system ( "pause" ) ;
   return 0;
}
/*---------------------------------------------------------------------------*/  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Titus B. Ledbetter, Jr.

Expertise

C , MFC, Object Oriented, Artificial Intelligence

Experience

I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Education/Credentials
Master's Degree Computer Science from Johns Hopkins

©2016 About.com. All rights reserved.