Thread: program wont read char + or -

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    9

    program wont read char + or -

    I need help with reading data from a file. The program was able to read other character and integers from a file, but for some reason it skips all + or - characters.

    The .dat file contains the following information:
    DA
    H
    + 3 5
    - 5 3
    * 10 7
    / 90 10
    H
    * 8 10
    + 2 8
    - 2 8
    / 7 2
    Q

    Code:
    #include <stdio.h>
    #define FILENAME1 "CommandsProj1.dat"
    #define FILENAME2 "storedData.dat"
    
    //function prototype
    void calculations(FILE*data1, FILE*data2, char ch);
    
    int main()
    {
        //variables for main
        char initials[3];
        char ch;
    
        //initialize these variables
        FILE*data1;
        FILE*data2;
        //open file to be read
        data1 = fopen(FILENAME1, "r");
        //open file to be written
        data2 = fopen(FILENAME2, "w");
        //test if file is there or not, if not, then prints error
        if(data1 == NULL)
        {
            printf("Error. File not loaded.\n");
            fprintf(data2, "Error. File not loaded.\n");
        }
        else
        {
        //reads the file, get initial, prints out the initial
            fscanf(data1, "%s", initials);
            printf("Initial is: %s\n", initials);
            fprintf(data2, "Initial is: %s\n", initials);
    
             //get the command from the file
            while(data1)
            {
                fscanf(data1, "%c", &ch);
    
                if(ch == 'H')
                {
                    printf("Caculator Commands:\n\n");
    
                    printf(" + [Add Integers]\n");
                    printf(" - [Subtract Integers]\n");
                    printf(" * [Multiply Integers]\n");
                    printf(" / [Divide Integers]\n\n");
                }
                else if(ch == 'Q')
                {
                    fclose(data1);
                    fclose(data2);
                    return(0);
                }
                else
                    calculations(data1, data2, ch);
            }
        }
        return(0);
    }
    
    
    void calculations(FILE*data1, FILE*data2, char ch)
    {
        int i, j, answer;
        fscanf(data1, "%d %d", &i, &j);
        switch(ch)
        {
            case '*':
            answer = i * j;
            printf("%d * %d = %d\n", i, j, answer);
            fprintf(data2, "%d * %d = %d\n", i, j, answer);
            break;
    
            case '/':
            answer = i / j;
            if(j == 0)
            {
            printf("Value is undefined.");
            fprintf(data2, "Value is undefined.");
            }
            else
            {
            printf("%d / %d = %d\n", i, j, answer);
            fprintf(data2,"%d / %d = %d\n", i, j, answer);
            }
            break;
    
            case '+':
            answer = i + j;
            printf("%d + %d = %d\n", i, j, answer);
            fprintf(data2,"%d + %d = %d\n", i, j, answer);
            break;
    
            case '-':
            answer = i - j;
            printf("%d - %d = %d\n", i, j, answer);
            fprintf(data2, "%d - %d = %d\n", i, j, answer);
            break;
        }
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    fscanf(data1, "%c", &ch);
    Did you try printing out ch every time through, so you could see what it was and why it wasn't adding? If you did, you probably would have noticed that it printed a new line. Most format specifiers for scanf ignore white space, but the %c modifier does not. That means the new line after the 'H' is left in the file. The next time through, when you scan for ch, it gets a '\n' instead of a '+'. Put a space in front of the %c, like so:
    Code:
    fscanf(data1, " %c", &ch);
    EDIT: That space will get rid of any white space before it reads the next character, so any leftover space, tab or newline, etc characters.

    Note, data1 and data2 are not good names, in_file and out_file are much more descriptive. On a slightly related note:
    Code:
    while (data1) {
        ...
    }
    Is not a valid file-reading loop. Try
    Code:
    do {
        if (fscanf(in_file, " %c", &ch) != 1) {
            // fscanf error, print message
            break;  // leave the loop early
        }
        ...
    } while (ch != 'Q');
    fclose(in_file);
    fclose(out_file);
    Last edited by anduril462; 03-28-2012 at 01:57 PM.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    I don't know the reason it doesn't work, but your code will work if you change the '+' and '-' to a different char. I change + = p and - = m and it worked.

    You might try reading one line at a time and get the individual pieces using strtok.
    Last edited by DaveH; 03-28-2012 at 02:08 PM.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Thank You! Finally got it working, been trying to fix this part for a while. Though i did try printing out every character and integer it read, i probably have missed the new line, and thank you for the hint on the loop statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read a number with scanf() and read char[] with fgets()
    By nutzu2010 in forum C Programming
    Replies: 5
    Last Post: 03-11-2011, 05:05 AM
  2. else wont read input?
    By King_of_Hearts in forum C Programming
    Replies: 2
    Last Post: 05-02-2010, 05:13 AM
  3. files > 65535 bytes wont read in dos
    By Pharoh in forum C Programming
    Replies: 4
    Last Post: 11-11-2003, 11:51 AM
  4. my program wont read my text files in??
    By Neildadon in forum C++ Programming
    Replies: 2
    Last Post: 12-15-2002, 10:08 AM
  5. my program wont read vector array
    By MKashlev in forum C++ Programming
    Replies: 5
    Last Post: 08-10-2002, 03:51 PM