Input file Each line contains a format followed by a real nu

Input file: Each line contains a format, followed by a real number in fixed notation. The real number should be output in that format. See sample input file infile1.
Your program must prompt for the names of the input and output files. Assume that file names are at most 12 characters long. main() prompts for the file names, gets the names, opens the files, checks if the opening was successful (gives an error message if unsuccessful), calls the function printNumbers, and then closes the files. printNumbers prints each number according to its format, in a separate line; each output line must end with a ’|’ character. See sample output file outfile1 at the course website. Your output must match my output, character for character.
Format: The format for each number consists of three characters followed by two positive integers. The two integers give the width and precision. The three characters (not separated by blanks) are as follows:
First character: ’f’ or ’s’, corresponding to fixed point or scientific notation, respectively.
Second character: ’y’ or ’n’; says whether to output a ’+’ sign for positive numbers.
Third character: ’l’ or ’r’, corresponding to left or right justification, respectively.
No comments or other documentation is needed. Use meaningful names for all variables. Sample input and output files

infile.txt
fyl 11 3   111.6789
fyl 12 3 -222.6789
fyr 13 3   333.6789
fyr 14 3 -444.6789
fnl 15 3   555.6789
fnl 16 3 -666.6789
fnr 17 3   777.6789
fnr 18 3 -888.6789
syl 21 2   111.6789
syl 22 2 -222.6789
syr 23 2   333.6789
syr 24 2 -444.6789
snl 25 2   555.6789
snl 26 2 -666.6789
snr 27 2   777.6789
snr 28 2 -888.6789
fyl 11 3   111.6789

outfile should look this this

+111.679   |
-222.679    |
     +333.679|
      -444.679|
555.679        |
-666.679        |
          777.679|
          -888.679|
+1.12e+02            |
-2.23e+02             |
              +3.34e+02|
               -4.45e+02|
5.56e+02                 |
-6.67e+02                 |
                   7.78e+02|
                   -8.89e+02|
+111.679   |

Solution

#include<stdio.h>

int main()
{
   FILE *in,*out;
   char infile[30],outfile[30];
     
   void printNumbers(FILE *in, FILE *out);
   printf(\"Enter Input file name : \");
   scanf(\"%s\",infile);

   printf(\"Enter Output file name: \");
   scanf(\"%s\",outfile);

   in = fopen(infile,\"r\");
   out = fopen(outfile,\"w\");

   if( !in || !out)
   {
       printf(\"Not able to open file for reading or writing\ \");
       return -1;
   }
  
   printNumbers(in,out);
    fclose(in);
   fclose(out);
}

void printNumbers(FILE *in, FILE *out)
{
   char str[4];
   int w,p;
   float num;
   while(!feof(in))
   {
       fscanf(in,\"%s%d%d%f\",str,&p,&w,&num);
       //str[3]=\'\\0\';
       if( (str[0] == \'f\'))
       {
           if((str[1] == \'y\'))
           {
               if( num > 0 )
               {
                   fprintf(out,\"+\");
               }
               else
                   fprintf(out,\"-\");
           }
           if( str[2] == \'l\' )
           {
               fprintf(out,\"-*f\",w,p,num);
               fprintf(out,\"\\t |\ \");
           }
           if( str[2] == \'r\' )
           {
               fprintf(out,\"*f\",w,p,num);
               fprintf(out,\"\\t |\ \");
           }

       }
       if( str[0] == \'s\')
       {
           if(str[1] == \'y\')
           {
               if( num > 0 )
               {
                   fprintf(out,\"+\");
               }
               else
                   fprintf(out,\"-\");
           }
           if( str[2] == \'l\' )
           {
               fprintf(out,\"-*e\",w,p,num);
               fprintf(out,\"\\t |\ \");
           }
           if( str[2] == \'r\' )
           {
               fprintf(out,\"*e>\",w,p,num);
               fprintf(out,\"\\t |\ \");
           }
          
          

          
      
       }
   }
      
}

Input file: Each line contains a format, followed by a real number in fixed notation. The real number should be output in that format. See sample input file inf
Input file: Each line contains a format, followed by a real number in fixed notation. The real number should be output in that format. See sample input file inf
Input file: Each line contains a format, followed by a real number in fixed notation. The real number should be output in that format. See sample input file inf

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site