Thread: Need help reading next line from input file

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by xrayextra
    Actually it does. Why bother with closing files in the first place if it didn't make a difference?

    the exit() statement closes all open streams before returning control to the system. A simple return doesn't do that.
    A "simple return" will do what the exit function does:
    Quote Originally Posted by C99 Clause 5.1.2.2.3 Paragraph 1
    If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.
    Quote Originally Posted by powerfox0
    The DA was there originally from my teacher..I even changed input file and removed DA. still not giving me anything printed to screen for + or - as op values....
    What is the input that you tested with, the expected output, and the actual output? (I presume the code has not changed since post #13.)
    Last edited by laserlight; 06-01-2014 at 11:37 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Sep 2013
    Posts
    20
    Quote Originally Posted by laserlight View Post
    What is the input that you tested with, the expected output, and the actual output? (I presume the code has not changed since post #13.)
    the input is
    DA
    H
    + 3 5
    - 5 3
    * 10 7
    / 90 10
    H
    * 8 10
    + 2 8
    - 2 8
    / 7 2
    Q

    the expected output is for the program to read from the file and print to the screen the result of whichever operator is read. if it reads H prints help menu. when a Q is read the program exits.

    The actual output is
    Need help reading next line from input file-blah-jpg

    as you can see it is failing to print anything when it reads a + or a - as a op value
    Last edited by powerfox0; 06-02-2014 at 12:02 AM. Reason: forgot to mention

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by powerfox0
    the input is
    DA
    H
    + 3 5
    - 5 3
    * 10 7
    / 90 10
    H
    * 8 10
    + 2 8
    - 2 8
    / 7 2
    Q
    This means that your approach of just using fscanf is not a good one and in fact will currently fail. You are trying to parse the input for sets of items that match "%c %f %f". This immediately fails when you attempt to match "DA". Furthermore, in your switch statement, you use a multicharacter constant that is unlikely to work: 'DA'.

    Seeing that your input appears to be line based, I suggest a change of approach: use fgets to read the input line by line in a loop. The line is read into a buffer of suitable size. You can assume that the input does not exceed the buffer size for now. Remove the newline character, if any, that is read and stored by fgets. Then, compare the line to the special commands, e.g., use strcmp to compare it with "DA" and then with "H". If the input does not match any of the special commands, then you use sscanf (a version of fscanf that uses a string as input) to parse the string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Sep 2013
    Posts
    20
    Quote Originally Posted by laserlight View Post
    You are trying to parse the input for sets of items that match "%c %f %f". This immediately fails when you attempt to match "DA". Furthermore, in your switch statement, you use a multicharacter constant that is unlikely to work: 'DA'.
    wouldn't the default case in my switch statement make up for that? i took the case "DA" out, still same issues


    EDIT: I tried using fgets and I ran into a bunch of errors.... i think fscanf will suffice. But the problem still occurs. can you explain why the program reads and prints regularly for all the other cases EXCEPT for + and - ?
    Last edited by powerfox0; 06-02-2014 at 02:34 AM.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of this:
    Code:
    while (EOF != fscanf(cmd, "%c %f %f", &op, &num1, &num2))
    {
        float result;
    
        switch (op)
        {
            case '+':
                result = num1 + num2;
                break;
            /* ... */
            case 'Q':
                printf("\nThank you for using Calculator  (Version 1.0)... Goodbye\n");
                goto cleanupArea;
            /* ... */
        }
        printf("\n%4.2f %c %4.2f = %4.2f \n", num1, op, num2, result);
        fflush(stdout);
    }
    I am suggesting something like:
    Code:
    #define LINE_MAXLENGTH 80
    
    /* ... */
    
    char line[LINE_MAXLENGTH + 1];
    while (fgets(line, LINE_MAXLENGTH + 1, cmd))
    {
        float result;
    
        line[strcspn(line, "\n")] = '\0'; /* remove trailing newline, if any */
        if (strcmp(line, "DA") == 0)
        {
            /* ignore or do whatever DA is supposed to mean */
        }
        else if (strcmp(line, "Q") == 0)
        {
            printf("\nThank you for using Calculator  (Version 1.0)... Goodbye\n");
            goto cleanupArea;
        }
        /* ... check for other special commands ... */
        else if (sscanf(line, "%c %f %f", &op, &num1, &num2) == 3)
        {
            switch (op)
            {
                case '+':
                    result = num1 + num2;
                    break;
                /* ... */
            }
            printf("\n%4.2f %c %4.2f = %4.2f \n", num1, op, num2, result);
            fflush(stdout);
        }
        else
        {
            /* handle input error */
        }
    }
    Last edited by laserlight; 06-02-2014 at 02:40 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Sep 2013
    Posts
    20
    Quote Originally Posted by laserlight View Post
    I am suggesting something like:
    Code:
    #define LINE_MAXLENGTH 80
    
    /* ... */
    
    char line[LINE_MAXLENGTH + 1];
    while (fgets(line, LINE_MAXLENGTH + 1, cmd))
    {
        float result;
    
        line[strcspn(line, "\n")] = '\0'; /* remove trailing newline, if any */
        if (strcmp(line, "DA") == 0)
        {
            /* ignore or do whatever DA is supposed to mean */
        }
        else if (strcmp(line, "Q") == 0)
        {
            printf("\nThank you for using Calculator  (Version 1.0)... Goodbye\n");
            goto cleanupArea;
        }
        /* ... check for other special commands ... */
        else if (sscanf(line, "%c %f %f", &op, &num1, &num2) == 3)
        {
            switch (op)
            {
                case '+':
                    result = num1 + num2;
                    break;
                /* ... */
            }
            printf("\n%4.2f %c %4.2f = %4.2f \n", num1, op, num2, result);
            fflush(stdout);
        }
        else
        {
            /* handle input error */
        }
    }
    Could you explain to me why did you change what you changed?

  7. #22
    Registered User
    Join Date
    Sep 2013
    Posts
    20
    I implimented your changes and I got it to work:
    Code:
    // PA
    #include <stdio.h>
    #include <stdlib.h>
    #define LINE_MAXLENGTH 80
    
    
    int main(void)
    {
        float num1, num2;
        char op;
    
    
        FILE* cmd = fopen("CommandsProj1.dat", "r");
    
    
        if (cmd == NULL)
        {
            printf("File could not be opened");
            return EXIT_FAILURE;
        }
    
    
        printf("Welcome to Pameer Ahmad's Calculator (Version 1.0)\n");
    
    
    
    
        char line[LINE_MAXLENGTH + 1];
        while (fgets(line, LINE_MAXLENGTH + 1, cmd))
        {
            float result;
            line[strcspn(line, "\n")] = '\0';
            if (strcmp(line, "DA") == 0)
            {
                /* ignore  */
            }
            else if (strcmp(line, "Q") == 0)
            {
                printf("\nThank you for using Calculator  (Version 1.0)... Goodbye\n");
                goto cleanupArea;
            }
            else if (strcmp(line, "H") == 0)
            {
                printf("\nHelp: accepted operations are '+' '-' '*' '/' 'H' 'Q'\n");
                continue;
            }
            /* ... check for other special commands ... */
            else if (sscanf(line, "%c %f %f", &op, &num1, &num2) == 3)
            {
                switch (op)
                {
                case '+':
                    result = num1 + num2;
                    break;
                case '-':
                    result = num1 - num2;
                    break;
                case '*':
                    result = num1 * num2;
                    break;
                case '/':
                    result = num1 / num2;
                    break;
                default:
                    continue;
                }
            printf("\n%c Operator: %4.1f %c %4.1f = %4.1f \n", op, num1, op, num2, result); /*  Print calculation information.  */
            fflush (stdout);
        }
    }
    cleanupArea:
        fclose(cmd); /*  Close file */
        return EXIT_SUCCESS; /*  exit program */
    }
    Need help reading next line from input file-blah-jpg
    Thank you so much for your help!
    Last edited by powerfox0; 06-02-2014 at 03:10 AM.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by powerfox0
    Could you explain to me why did you change what you changed?
    Yes: you are currently trying to parse a line with two possible formats. In one format, the line contains one or more letters that denote some special command. In the other format, the line contains a dyadic arithmetic operation in prefix notation, with operator and operands separated by whitespace. Hence, my example reads the line into a string, and then attempts to match the string according to the first format. If it fails, it then parses the string according to the second format. If that still fails, then there must be an input error (or maybe the line is just blank, which could be an input error).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
            line[strcspn(line, "\n")] = '\0';

    I remember this Scanning new line


    It changed the way I programmed as well.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-07-2012, 02:50 AM
  2. reading input from command line
    By ueg1990 in forum C Programming
    Replies: 4
    Last Post: 01-26-2012, 11:29 PM
  3. reading line by line - input function
    By Lindira-123 in forum C Programming
    Replies: 11
    Last Post: 02-06-2011, 03:34 PM
  4. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  5. Reading Input from a file, line-by-line
    By Acolyte in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 01:03 PM

Tags for this Thread