Char arrays and file input and out put Update!

So here the story I trying to create a program that will read a file and based on the file info . It choice the correct switch statement and does the assigned function.


The compiler report no errors so I assume it all logic errors on my part. When I run program it does only first file input and ends . I'm sure it something to do with fopen and fscanf. I just cant figure what .


example of file input

+ 2 -4
+ 99 5
+ 55 32
Q




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

# define FILENAME "CommandsProj1.txt"

void Adding_Integer ( FILE * Calculatorfile );


int main()
{



    FILE *Calculatorfile;
    Calculatorfile = fopen( FILENAME, "r");

    char StoredCommand = 'Q';

    if ( Calculatorfile == NULL ) // Confirm access to file
        {
         printf("Error opening file!\n\n\a");
         printf(" Make sure file is in default location\n\n " );
         return 1; // return 1 instead of 0 to state program has error
        }

    printf(" Program is running now\n\a ");


    do {

        fscanf( Calculatorfile ,"%c  " , &StoredCommand);

        switch( StoredCommand )
        {
            case '+' : Adding_Integer (Calculatorfile);

            break;
           // case '-' : Subtraction_Integer();
            //break;
            //case '*' : Multiplication_ Integer();
            //break;
            //case '/' : Divisions_Interger();
            //break;

            case 'Q' :
            break;

            }

            rewind(Calculatorfile);

    } while( StoredCommand  == 'Q');


    fclose(Calculatorfile);

    return 0;
}

void Adding_Integer(FILE * Calculatorfile )
{
    int Addvalue1 = 0;
    int Addvalue2 = 0;
    int sum = 0;

    fscanf( Calculatorfile ,"%i%*c%i " , &Addvalue1 , &Addvalue2 );

    sum = Addvalue1 + Addvalue2 ;

    printf(" The sum of %i + %i = %i \n " , Addvalue1 , Addvalue2 , sum );
    fprintf(Calculatorfile ," The sum of %i + %i = %i \n " , Addvalue1 , Addvalue2 , sum ) ;
}
Last edited on
One thing you can do to improve predictability of your code is replace fscanf with getc().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main(){
        char c=0;

        printf("Enter some text:\n> ");
        while(1){
                c=getc(stdin); //Get a character
                if(c=='\n')break; //End of a line? Break!
                printf("%c ",c); //Print the character.
        }

        return 0;
}
Last edited on
Tip: } while( StoredCommand == 'Q');
Just like EssGeEich said, try
while( StoredCommand != 'Q');

but that may create an infinite loop since you are calling
rewind(Calculatorfile);

another is you are trying to write to the file
fprintf(Calculatorfile ," The sum of %i + %i = %i \n " , Addvalue1 , Addvalue2 , sum ) ;

if you want to be able to read and write, try to open it with r+ flag
Calculatorfile = fopen( FILENAME, "r+");

Hope that helps..
What about getting a line at a time and using sscanf?
Like this:
1
2
3
4
5
6
7
8
9
10
11
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 '+': // If it's '+' ...
            Add(Buffer);
    }
}

And in "Add" function, do something like this:
1
2
3
4
5
6
7
void Add(const char * Line)
{
    int Operator1 = 0;
    int Operator2 = 0;
    sscanf(Line,"+ %d %d",&Operator1, &Operator2); // It's like a scanf from a string, so a sscanf.
    printf("Result of %d * %d = %d",Operator1, Operator2,Operator1*Operator2);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

#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 ( )

*/

Thanks everyone for your input really ! That said I posted the updated code .



I did try to change the while loop to

1
2
3
4
5

do{

}while (StoredCommand!= 'Q');


and it did indeed create inf loop like you said
blackcoder41. My computer almost crash .


For some reason it didn't store the results . I almost went mad

right now I trying to figure out how to quit the program when it encounter q

and what do mean by

P i k [Print k-th Digit ] Print out the k-th digit of integer i


The sample input of this program will encounter is below

JP
+ -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
1. again don't call rewind(Calculatorfile); (remove it)

2. remove extra space " \t Q [Quit ] Quit \n" );

3. for lowercase change C to c sscanf(Line,"C %s\n", down_Word );

4. your Subtract and Multiply function got mixed up (interchanged)

5. P i k [Print k-th Digit ] - i represent the integer k represents the index. so in your example
P 2134 3
you should print 4
char* i= "2134";
int k = 3;
print (i[k]); /* this prints 4 */

6. for separating the integer and decimal part. use the function modf
http://www.cplusplus.com/reference/clibrary/cmath/modf/

Topic archived. No new replies allowed.