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.