Thread: Almost there... need More Help

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

    Almost there... need More Help

    hello again.. this is an update from a post from a few days ago.. ive come a long way but still need help..

    in my code below i am reading from a file called transact.txt and which has entries like ''id, name, dollar amount" ie. 1000 joe 100.00

    i am trying to use fprint to output the information ive put in an array into a file called accounts.txt. the when i do a printf every line prints. however when i do a fprint only the last line of the file prints.. also i need to figure out how to sort the output by id... any ideas are appreciated..

    #define MAXACCTS 16
    #define BUF 64
    #define NAME_SZ 10

    int main()
    {
    FILE *fp;
    FILE *out;
    char name[10];
    int id[MAXACCTS], i, j, k;
    float amount[MAXACCTS];
    char buffer[BUF];


    //open file for reading
    if ((fp = fopen("transact.txt", "r")) == NULL)
    {
    perror("Unable to open transact.txt\n");
    system("pause");
    return -1;
    }
    else


    while (fgets(buffer, BUF, fp) !=NULL)
    {

    if(sscanf(buffer, "%s%s%s", id, name, amount) ==3)
    { k = atoi(id);
    if (k > 999 && k <10000)
    {
    if (strlen(name) < NAME_SZ)
    { out = fopen("accounts.txt", "w+");
    if (out == NULL)
    { perror("unable to open accounts.txt\n");
    system("pause");
    exit (-2);
    }

    printf("%s\t%s\t\t%.2f\n", id, name, amount);
    fprintf(out, "%s\t%s\t\t%.2f\n", id, name, amount);
    }
    else
    printf("Account Name: \"%s\" is over %d characters\n", name, NAME_SZ -1);
    }
    else
    printf("Invalid ID: %s\t%s\t\t%.2f\n",id, name, amount );
    }


    }
    fcloseall();
    system("pause");
    return 0;
    }




  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Your types and calls to *scanf/*printf were way off, and the reason you only got the last line of the file was because you reopened the output file for every new line read. This effectively truncated everything in the file and gave you a clean slate. Compare this with what you have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXACCTS 16
    #define BUF 64
    #define NAME_SZ 10
    
    int main()
    {
      FILE *fp;
      FILE *out;
      char name[MAXACCTS][NAME_SZ];
      int id[MAXACCTS], i = 0;
      float amount[MAXACCTS];
      char buffer[BUF];
    
      printf ( "%f\n", 123.456f );
      if ((fp = fopen("transact.txt", "r")) == NULL)
      {
        perror("Unable to open transact.txt\n");
        return -1;
      }
      out = fopen("accounts.txt", "w+");
      if (out == NULL)
      {
        perror("unable to open accounts.txt\n");
        return -2;
      }
      while (fgets(buffer, BUF, fp) !=NULL)
      {
        if(sscanf(buffer, "%d %s %f", &id[i], name[i], &amount[i]) == 3)
        {
          if (id[i] > 999 && id[i] < 10000)
          {
            if (strlen(name[i]) < NAME_SZ)
            {
              printf("%d\t%s\t\t%.2f\n", id[i], name[i], amount[i]);
              fprintf(out, "%d\t%s\t\t%.2f\n", id[i], name[i], amount[i]);
            }
            else
              printf("Account Name: \"%s\" is over %d characters\n", name[i], NAME_SZ -1);
          }
          else 
            printf("Invalid ID: %d\t%s\t\t%.2f\n",id[i], name[i], amount[i] );
        }
      }
      fclose ( fp );
      fclose ( out );
    
      return 0;
    }
    >also i need to figure out how to sort the output by id
    Your book should cover bubblesort, it will suffice for this program. Just remember that any swaps you perform on one array must be performed on the others so that all of the fields in a record remain at the same index.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed