Thread: array and file I/O help

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    array and file I/O help

    i am new to programming and am struggling with a homework project. Please help

    Here are the requirements
    need a program to store account information of a bank
    need to store
    id, one-word name, current balance.

    i need to read transactions from a file. typical transactions look like this

    1234 jon 123.45
    4001 jake 1005.50

    i have a few rules to go by..
    can only have a max of 16 accounts
    id's are int between 1000 and 9999
    names are one word with less than 10 letters
    names on transactions must match names in account
    reject transactions that produce negative balances

    i need to use arrays for id, name, amounts and store them in ascending order based on id.

    i need to provide a reason for any account transaction being rejected... i.e invalid id, name, etc..

    after reading all transactions from file need to write the id, name and final balance to file called account.txt..

    heres what i have so far... i need some direction what to do next.. thanks




    #define LIMIT 48
    #define LENGTH 16


    int main()
    {
    char acct_info[LIMIT][LIMIT], buffer[4*LENGTH];
    FILE *file;
    int i, size = 0;

    file = fopen("transact.txt", "r");
    if(file ==NULL)
    {
    perror("UNABLE TO OPEN transact.txt\n");
    system("pause");
    return -1;

    }
    while(fscanf(file, "%s", buffer) ==1)
    {
    if(strlen(buffer) < LENGTH -1 )
    {
    if(size < LIMIT)
    {
    strcpy(acct_info[size], buffer);
    ++size;
    }
    else
    printf("there is no room in the array for %s\n", buffer);
    }
    else
    printf("%s is too long\n", buffer);
    }
    fclose(file);


    file = fopen("account.txt", "w");
    if(file ==NULL)
    {
    perror("UNABLE TO OPEN account.txt\n");
    system("pause");
    exit (-2);
    }

    for(i = 0; i < size; ++i)
    fprintf(file, "%s\n", acct_info[i]);
    fclose(file);





    system("pause");
    return 0;
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    How about keeping this to one thread: http://cboard.cprogramming.com/showthread.php?t=51443

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    I couldnt figure out how to delete the other thread.. but i added more info to this thread

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i need some direction what to do next..
    Scrap what you have an start over. I generally get bad reactions when I suggest that, but if you use a well structured approach from the start it makes your task much easier.

    When you start over, write a simple program to handle a single transaction on one record read from stdin. This saves you from fiddling with files and loops until you know that you can successfully work with one account.

    Write the code piece by piece and test it thoroughly. For example, first write the code to read in a single account and validate it. Then when that works the way you want, write the code to perform a transaction. When that works, set up your error checking and messages.

    At this point you should have a working program for a single record. Now you can start reading from a file. When that works properly, convert your single variables to arrays and wrap a loop around the code to read and handle one record. Tidy things up and you're done.

    Most importantly, have a good understanding of the problem and any solutions before you write any code. So many people make the mistake of writing the code before they understand the problem. The result is bad, ugly, patchy code, and they end up duct taping it until it compiles and runs. But they can't guarantee correctness and the programs are usually bloated and error-prone.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. File I/O problem for dynamically allocated struct array
    By veecee in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2006, 09:28 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM