Thread: Beginning C - I/O issues

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    7

    Beginning C - I/O issues

    Hello,

    I am learning C and my friend that offered to give me some help with I/O gave me a sample program from his last semester's class to help get me started. It is supposed to be a 'simple payroll system.'

    I think I have it almost coded correctly, but it isn't functioning correctly.

    The input is a text file is as follows:
    38.5 12.75 Bob, Joe
    17.5 9.25 Bob, Jolene
    20.0 9.65 Smith, Janet
    25.0 8.25 Public, John Q.
    40.0 8.75 Doe, Jane
    15.0 6.25 Ling, Grunt


    The desired output is as follows:
    name hours rate pay
    Bob, Joe 38.50 12.75 490.88
    Bob, Jolene 17.50 9.25 161.88
    Smith, Janet 20.00 9.65 193.00
    Public, John Q. 25.00 8.25 206.25
    Doe, Jane 40.00 8.75 350.00
    Ling, Grunt 15.00 6.25 93.75



    However, when I run my program, I am only getting:
    name hours rate pay

    Here is my code:

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    int main()
    {
        char a[] = "name";
        char b[] = "rate";
        char c[] = "hours";
        char d[] = "pay";
        float hours;
        float rate;
        char name[100];
        FILE *fptr;
        fptr=fopen("input.txt", "r");
    
        printf("%s%20s%10s%10s\n", a, b, c, d);
    
    
        while(!scanf("%f%f", &hours, &rate)  && fgets(name, 100,stdin))
        {
            name[strlen(name)+1] = 0;
    
            printf("%s%20f%10f%10f\n", name, rate, hours, rate*hours);
        }
    
        fclose(fptr);
        return 0;
    }
    Any suggestions on how I should modify my code to gain the desired result?

    Thank-you very much!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    scanf() doesn't return what you seem to think it does, given your code.

    strlen(name) + 1 makes no sense either, since it doesn't affect the current string, and is possibly an out of bound access.
    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.

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Any suggestions on how I should modify my code to gain the desired result?
    ...
    If by "desired result" you mean you learning C, then my suggestion would be to press CTRL+A, then hit DEL.
    There's enough code in there to tell me that the person who wrote it should know why that is occuring... which tells me you didn't write it.
    Until MacGuyver comes, the only other hint I'd give you is to try typing the magic C password: "Dennis M. Ritchie", then hit enter. keep doing this until it works.

    edit: okay, now to be useful:
    You only see the column headers because your program is blocking, waiting for input from stdin at the call to fgets(). The standard "this is an assignment from school" project usually has you redirect input from a file (which is why it probably worked for your friend). Furthermore, as Salem said, if you knew what fgets actually did (hint: pay attention in class), you'd know that it automatically terminates its strings with '\0' so that you do not need to. There's more, but as I said, take my first suggestion and learn it yourself.
    Last edited by @nthony; 09-06-2007 at 05:31 PM.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    Quote Originally Posted by Salem View Post
    scanf() doesn't return what you seem to think it does, given your code.

    strlen(name) + 1 makes no sense either, since it doesn't affect the current string, and is possibly an out of bound access.
    I apologize for possibly asking a noob question. But how do I go about fixing those problems you layed out? I was going to take the '!' away from the scanf() statement...would that solve the problem?

    Sorry, I'm just really confused on 'what I should try' to do to fix the problem.

    Thanks...

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    Quote Originally Posted by @nthony View Post
    If by "desired result" you mean you learning C, then my suggestion would be to press CTRL+A, then hit DEL.
    There's enough code in there to tell me that the person who wrote should know why that is occuring... which tells me you didn't write it.
    Until MacGuyver comes, the only other hint I'd give you is to try typing the magic C password: "Dennis M. Ritchie", then hit enter. keep doing this until it works.
    Well, i am not going into C blind...I have taken a course on C++ and I know it is kinda similar. I am just trying to learn how to use it with some of the limited knowledge I know and applying to the sample problem he gave me. Yes, I wrote that code.

    So yes, my desired result is learning C...but I have a small understanding of it. Regardless, I want to learn how to read in from the text file and output the values to the screen...and that is what I am having a hard time doing.
    Last edited by robdapeepschamp; 09-06-2007 at 05:26 PM. Reason: addition

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by @nthony View Post
    Until MacGuyver comes
    I'm famous!

    Quote Originally Posted by robdapeepschamp View Post
    I apologize for possibly asking a noob question. But how do I go about fixing those problems you layed out?
    Something along the lines of RTM and STW.

    Quote Originally Posted by robdapeepschamp View Post
    I was going to take the '!' away from the scanf() statement...would that solve the problem?
    No.

    Quote Originally Posted by robdapeepschamp View Post
    Sorry, I'm just really confused on 'what I should try' to do to fix the problem.
    Check what scanf() returns. Use the man pages if you're on a *nix system or just simply google for info on scanf().

    From there, fix the loop condition. And whatever you're doing to variable name inside the loop is messed up. Check up on what fgets() does and fix that. If you're trying to get rid of the '\n', that's not what you're doing.

  7. #7
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Quote Originally Posted by robdapeepschamp
    Well, i am not going into C blind...I have taken a course on C++ and I know it is kinda similar. I am just trying to learn how to use it with some of the limited knowledge I know and applying to the sample problem he gave me.
    Question: how did you know when/how to use fgets and what to check against its return value? Using float for rational numbers does not seem like a C++ student to me... no, to me it looks like example code that has been floating around (excuse the pun) for years, the sort you copy straight out of a textbook back from ninteen-ninety-something when saving space was all the rage.
    Tell you what, since you have C++ under your belt (although knowing C++ technically mandates that you know C as well, but I won't get into that), try writing it in C++. It should be a simple task for someone who has taken a course in C++, and aftwerards, a simple case to replace the odd cout, endl, <<. etcs with their respective C counterparts.
    Last edited by @nthony; 09-06-2007 at 05:41 PM.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    Quote Originally Posted by @nthony View Post
    Question: how did you know when/how to use fgets and what to check against its return value?
    google and some guessing

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well "thar's ya problem!"
    Guessing what a function does based on some observations of its use doesn't work.

    scanf() returns the number of "conversions and assignments", or EOF
    In this case, success would mean that scanf() would be expected to return 2
    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.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    I've coded your problem, but just giving it to you is pointless. So, I'll go through each line/section of my code, and be as helpful as I can in terms of telling you what to do.

    The algorithm for how I did it is as follows:

    a) Create variables for every piece of data you want to read in from the file. Also, create a string variable large enough to read in a line at a time from the file.

    b) Create a file pointer, and use it to open your input file.

    c) Print column headers for your table

    d) Use the fgets function, inside a while loop, to iterate through each line of the file until fgets returns NULL. With each iteration - in other words, inside the body of the while loop - use printf to send the data to the screen.

    e) Close file.

    All you need to do now is to read the prototype for fgets, so that your arguments are in the right order. In your original post, it was point d) which was wrong, so follow what I said and you should be fine
    Last edited by osiris^; 09-08-2007 at 12:20 PM.

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    Well, I've recoded some things after finding out I didn't need to use file pointers. Here's my code that I got working:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    int main()
    {
        char a[] = "name";
        char b[] = "hours";
        char c[] = "rate";
        char d[] = "pay";
        float hours;
        float rate;
        char name[256];
        char * tmp = malloc(256);
    
        printf("%-20s %10s %10s %10s\n", a, b, c, d);
    
        /* do the first scan */
        scanf("%f%f", &rate, &hours);
    
        while(fgets(name,256,stdin) != NULL)
        {
            /*if a new line is found set it to NULL*/
            if( ( tmp = strrchr( name, '\n') ) )
                {
                    *tmp = '\0';
                }
    
                /*prints out the name, rate, hours, and amount to be paid*/
    
                printf( "%-20s %10.2f %10.2f %10.2f\n", name, rate, hours, rate*hours );
    
                /*reads in the rate and hours*/
    
                scanf( "%f%f", &rate, &hours );
        }
    
        return 0;
    }
    I compile as follows: gcc -Wall -ansi main.c
    run: ./a.out < input.txt

    After I figured out thats how the text file was read in, it made it simpler. I made it harder than it needed to be. thanks for all the help offered...I'm sure I'll be needing some more as time goes on.

    once again, thanks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String I/O issues - need help
    By thatzilla in forum C Programming
    Replies: 13
    Last Post: 04-15-2009, 10:53 AM
  2. asynchronized I/O == multiplexing I/O?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 07-24-2006, 10:06 AM
  3. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  4. Nonblocking I/O
    By fnoyan in forum Linux Programming
    Replies: 4
    Last Post: 11-29-2005, 04:37 PM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM