#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Help with File I/O


    I'm trying to write a program that simulates a bank account. Starts with a username and beginning balance then user can withdraw, deposit, view their previous transactions, view a summary of their account, and quit the program. I'm horrible at File I/O and it's showing in this project. If anyone could tell me what I'm doing wrong with my fprintf statement it would really help me because right now when I run it, nothing gets written to the .txt file.

    Code:
    #include stdio.h
    
    int main()
    {
    FILE *out = fopen("register.txt", "rw");
    if(out == NULL)
    {
            printf("The file was not successfully opened.\n");
            return -1;
    }
    
    fprintf(out, "Trans #\tType \t\t+/- Amount\t Balance\n");
    
    return 0;
    }
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,947
    Rep Power
    4033
    rw is not a valid mode parameter for fopen. I'm not sure how fopen is handling that but assuming you're not getting the error message probably it is opening the file read-only.

    Look at the man page for fopen to view the valid modes and pick the correct one for your needs.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0
    Wow, that's a stupid thing to mess up. It's weird though, my professor actually taught us that that's what you use to open it for read and write. Anyway, changed it to r+, thanks for your help!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0
    Just one more question on File I/O. I'm trying to print the file to the screen one line at a time but I can't figure out how to do it. So far I have:

    Code:
    void PrintRegister(float begBalance,float balance)
    {
            char line[80];
            FILE *out = fopen("register.txt", "r");
            printf("-----------------------------------------\n");
            printf("\tAccount Register\n\n");
            printf("Beginning balance: $%.2f\n", begBalance);
            printf("   Ending balance: $%.2f\n\n", balance);
            printf("Trans #\tType \t\t+/- Amount\t Balance\n");
            int t;
            for(t = 0; t < 500000; t++)
            {
                    while(fgets(line, 80, out) != NULL)
                    {
                            sscanf(line, "%s%8.2f%8.2f", typeString, delta, balance);
                            printf("%6d\t%s\t$%8.2f\t%8.2f\n", t, typeString, delta, balance);
                    }
            }
            fclose(out);
    
    }
    I'm trying to increment a transaction count and put it before the rest of the information that is printed out. I know my code is probably extremely wrong right now but any help is appreciated, thanks.
  8. #5
  9. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,947
    Rep Power
    4033
    Your for loop makes no sense. You're essentially telling to repeat your file-read loop 500,000 times. Repeating the file-read loop wouldn't have any effect though because you never rewind the file so each iteration past the first would just cause fgets to fail (due to EOF).

    If all you want is for t to be a counter for each line read, then just increment it within your while loop, there is no need for the for loop at all so remove it.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo