Drama with file pointers and loop to next command

This is a discussion on Drama with file pointers and loop to next command within the C Programming forums, part of the General Programming Boards category; I'm about done with the code but I don't know how to file pointer to a function . Another issue ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    Question Drama with file pointers and loop to next command

    I'm about done with the code but I don't know how to file pointer to a function . Another issue I have and had is the program doesn't go to next line of code .



    I need help with the following

    - how to put a file pointer to a function
    - deal with program not looping to next of file
    - End the program when program encounter Q

    Sample of Input file

    PJ
    + -3 4
    + -4 -71
    - 10 2
    - -41 -7
    H
    * 3 -4
    * 2 2
    / 141 13
    / 14 -3
    H
    c F
    C g
    P 2134 3
    R 3.141500000 2
    R 3.141500000 3
    R 0.010157 2
    S -3.141500
    S 31.41500
    Q
    H


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
        void Add(  FILE *Calculatorfileoutput ,const char * Line );
        void Multiply ( FILE * Calculatorfileoutput , const char * Line);
        void Subtract (FILE * Calculatorfileoutput , const char * Line);
        void Divide (FILE * Calculatorfileoutput , const char * Line);
        void Changecase_UP (FILE * Calculatorfileoutput , const char * Line);
        void Changecase_Down (FILE * Calculatorfileoutput , const char * Line);
        void PrintKD (FILE * Calculatorfileoutput , const char * Line);
        void PrintRR (FILE * Calculatorfileoutput , const char * Line);
        void Separate (FILE * Calculatorfileoutput , const char * Line);
        void Help (FILE * Calculatorfileoutput , const char * Line);
        void Quit (FILE * Calculatorfileoutput , const char * Line);
    
    
    
    
    int main ( void )
    {
    
    
        FILE *Calculatorfile;
        Calculatorfile = fopen( "input.txt", "r");
    
    
        FILE *Calculatorfileoutput;
        Calculatorfileoutput = fopen("output.txt" , "w");
    
    
    
    
    
    
            while(!feof(Calculatorfile))
            {
                char Buffer[256];
                if(!fgets(Buffer,256,Calculatorfile)) // fgets returns 0 when EOF or Error occurs
                {
                   break;
                }
    
    
    
    
                switch(Buffer[0]) // Check the first char in the current line
                {
                    case '+' :
                     Add(  Calculatorfileoutput , Buffer );
                     break;
    
    
                    case '*' :
                     Multiply ( Calculatorfileoutput , Buffer );
                     break;
    
    
                    case '-' :
                     Subtract (Calculatorfileoutput , Buffer );
                     break;
    
    
                    case '/' :
                     Divide (Calculatorfileoutput , Buffer );
                     break;
    
    
                    case 'C' :
                     Changecase_UP (Calculatorfileoutput , Buffer );
                     break;
    
    
                    case 'c' :
                     Changecase_Down (Calculatorfileoutput , Buffer );
                     break;
    
    
                    case 'P' :
                     PrintKD (Calculatorfileoutput , Buffer );
                     break ;
    
    
                    case 'R' :
                     PrintRR (Calculatorfileoutput , Buffer );
                     break;
    
    
                    case 'S' :
                     Separate (Calculatorfileoutput , Buffer );
    
    
                    case 'H' :
                     Help (Calculatorfileoutput , Buffer );
                     break;
                    //case 'Q' :
                     //Quit (Calculatorfileoutput , Buffer );
                     //break;
    
    
                    //default :
    
    
                     // some command
                }
    
    
                rewind(Calculatorfile);
            }
    
    
    
    
    
    
            fclose(Calculatorfile);
            fclose(Calculatorfileoutput);
    
    
            return 0 ;
    
    
    }
    
    
    void Add( FILE *Calculatorfileoutput ,const char * Line )
    
    
    {
    
    
        int Operator1 = 0;
        int Operator2 = 0;
    
    
        sscanf(Line,"+ %d %d\n",&Operator1, &Operator2); // It's like a scanf from a string, so a sscanf.
        printf("Result of %d + %d = %d\n",Operator1, Operator2,Operator1+Operator2);
        fprintf( Calculatorfileoutput,"Result of %d + %d = %d\n",Operator1, Operator2,Operator1+Operator2);
    
    
    }
    
    
    void Multiply ( FILE * Calculatorfileoutput , const char * Line)
    {
        int Operator1 = 0;
        int Operator2 = 0;
    
    
        sscanf(Line,"- %d %d\n",&Operator1, &Operator2); // It's like a scanf from a string, so a sscanf.
        printf("Result of %d - %d = %d\n",Operator1, Operator2,Operator1-Operator2);
        fprintf( Calculatorfileoutput,"Result of %d - %d = %d\n",Operator1, Operator2,Operator1-Operator2);
    }
    
    
    void Subtract (FILE * Calculatorfileoutput , const char * Line)
    {
        int Operator1 = 0;
        int Operator2 = 0;
    
    
        sscanf(Line,"* %d %d \n",&Operator1, &Operator2); // It's like a scanf from a string, so a sscanf.
        printf("Result of %d * %d = %d \n",Operator1, Operator2,Operator1* Operator2);
        fprintf( Calculatorfileoutput,"Result of %d * %d = %d \n",Operator1, Operator2,Operator1*Operator2);
    }
    void Divide (FILE * Calculatorfileoutput , const char * Line)
    {
        int Operator1 = 0;
        int Operator2 = 0;
    
    
        sscanf(Line,"/ %d %d \n",&Operator1, &Operator2); // It's like a scanf from a string, so a sscanf.
        printf("Result of %d / %d = %d \n",Operator1, Operator2,Operator1 / Operator2);
        fprintf( Calculatorfileoutput,"Result of %d * %d = %d\n",Operator1, Operator2,Operator1/Operator2);
    
    
    }
    void Changecase_UP (FILE * Calculatorfileoutput , const char * Line)
    {
        char UP_Word[256];
    
    
        sscanf(Line,"C %s\n", UP_Word ); // It's like a scanf from a string, so a sscanf.
    
    
    
    
        printf("UPPER CASE : %s\n", strupr(UP_Word ) );
        fprintf( Calculatorfileoutput,"UPPER CASE : %s \n", strupr(UP_Word ) );
    
    
    }
    void Changecase_Down (FILE * Calculatorfileoutput , const char * Line)
    {
        char down_Word[256];
    
    
        sscanf(Line,"C %s\n", down_Word ); // It's like a scanf from a string, so a sscanf.
    
    
    
    
        printf("lower case : %s\n", strupr(down_Word ) );
        fprintf( Calculatorfileoutput,"lower case : %s \n", strlwr(down_Word ) );
    
    
    }
    void PrintKD (FILE * Calculatorfileoutput , const char * Line)
    {
    
    
    }
    void PrintRR (FILE * Calculatorfileoutput , const char * Line)
    {
    
    
    }
    
    
    void Separate (FILE * Calculatorfileoutput , const char * Line)
    {
          char String_Separate [256];
    
    
          sscanf(Line , "S %s" , String_Separate );
    
    
          char * PtrtoString;
    
    
          PtrtoString = strtok (String_Separate," ");
          while (PtrtoString != NULL)
          {
            printf ("%s ",PtrtoString);
            fprintf( Calculatorfileoutput,"%s" ,PtrtoString);
            PtrtoString = strtok (NULL, " ");
          }
    
    
    }
    
    
    
    
    void Help (FILE * Calculatorfileoutput , const char * Line)
    {
    
    
        printf(  " \t + i j [Integer Add]  Add integers i and j and print out result \n "
                 " \t * i j [Integer Multiply]  Multiply integers i and j and print out result \n "
                 " \t - i j [Integer Subtract ]  Subtract integer j from i and print out result \n "
                 " \t / i j [Integer Divide ]  Divide integer i by j and print out result of integer division \n "
                 " \t C Ch [Character Case Change ]  Change character Ch to uppercase and print it out \n "
                 " \t c Ch [Character Case Change]  Change character Ch to lowercase and print it out \n "
                 " \t P i k [Print k-th Digit ]  Print out the k-th digit of integer i \n "
                 " \t R x i [Round Reals ]  Round double value x to i decimal places \n "
                 " \t S x [Separate ]  Separate out the sign, integer part and fractional part of double value x \n "
                 " \t H [Help ]  Print a short synopsis of all the available commands \n "
                 " \t Q [Quit ]  Quit \n " );
    
    
         fprintf( Calculatorfileoutput  ," \t + i j [Integer Add]  Add integers i and j and print out result \n "
                 " \t * i j [Integer Multiply]  Multiply integers i and j and print out result \n "
                 " \t - i j [Integer Subtract ]  Subtract integer j from i and print out result \n "
                 " \t / i j [Integer Divide ]  Divide integer i by j and print out result of integer division \n "
                 " \t C Ch [Character Case Change ]  Change character Ch to uppercase and print it out \n "
                 " \t c Ch [Character Case Change]  Change character Ch to lowercase and print it out \n "
                 " \t P i k [Print k-th Digit ]  Print out the k-th digit of integer i \n "
                 " \t R x i [Round Reals ]  Round double value x to i decimal places \n "
                 " \t S x [Separate ]  Separate out the sign, integer part and fractional part of double value x \n "
                 " \t H [Help ]  Print a short synopsis of all the available commands \n "
                 " \t Q [Quit ]  Quit \n " );
    
    
    
    
    
    
    }
    /*void Quit (FILE * Calculatorfileoutput , const char * Line)
    {
    
    
       if ( )
    
    
    */
    Last edited by nathansetokaiba; 04-15-2012 at 12:47 AM. Reason: Posted old code

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > rewind(Calculatorfile);
    Well this seems fairly inexplicable, if you're wanting to read the whole file, and not just the first line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    What do you mean by that? Rewind was put so it would loop the whole file. Thanks if that the case that would be helpful Insight! Is there any other holes for you see?

  4. #4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (*pointers) & Command Line problem...?
    By allan01123 in forum C Programming
    Replies: 5
    Last Post: 03-17-2011, 05:48 PM
  2. Replies: 6
    Last Post: 11-04-2010, 04:33 PM
  3. Loop Help with Pointers.
    By 1BadRbt in forum C Programming
    Replies: 3
    Last Post: 12-20-2006, 12:36 AM
  4. 486 drama
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-09-2002, 11:09 PM
  5. Winsock drama
    By jinx in forum Windows Programming
    Replies: 3
    Last Post: 11-21-2001, 05:28 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21