Originally Posted by
DecoratorFawn82
This crashes too, so I guess that is not how you meant?
And what do I need to do to use a char pointer?
Wait there is a fast way to enter n persons without malloc and
using an array of for example 100 elements?
ok you asked, so I show. Also shows two ways for this char thing 1. char *pointer, and 2. char var[50]; just need to look to see it. I am kind of reluctant to post everything GReaper was helping you, but this is just not complete if I remove some of it. I know, I'll just pull a "Oh well"
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// to get inout off command line and
//put it into a struct array
//fprintf to file.
//then read and print back file
typedef struct Accounts
{
char fristName[50];
char *lastName;
int IDNumber;
float decimal_point_number;
}account;
int main (int argc, const char **argv)
{
if (argc < 2)
{
printf("no outfile specified\n");
return -1;
}
FILE *outfile;
if ( (outfile = fopen(argv[1], "w+")) == NULL )
{
printf("File < %s > Not Able to be Opened\n", argv[1]);
return -1;
}
char buffer[1024];
char Fname[50], Lname[50];
int id = 0;
float dpn = 0.0;
int newRecords = 0, i = 0;
printf("Amount of new records needed?\n");
scanf(" %d", &newRecords);
account newAccounts[newRecords];
memset(newAccounts, 0, newRecords * sizeof(account));
while ( i < newRecords)
{
printf("Add Frist name Last Name Id, decimal point number\n");
getchar();
fgets(buffer, sizeof buffer, stdin);
sscanf(buffer, "%s%s%d%f", Fname,Lname,&id,&dpn);
// The part where you do that special thing to get char's to work
// removed intentionally.
// now the rest of the stuff
newAccounts[i].IDNumber = id;
newAccounts[i].decimal_point_number = dpn;
i++;
printf("\n\nRecord %d gotten\n"
"%d Left to go \n",i, ( newRecords - i));
}
for (size_t a = 0; a < sizeof(newAccounts)/sizeof(newAccounts[0]); a++)
fprintf(outfile, "Name %s %s ID %d some Decimal Number %.2f\n", newAccounts[a].fristName,
newAccounts[a].lastName,newAccounts[a].IDNumber,newAccounts[a].decimal_point_number);
for (size_t a = 0; a < sizeof(newAccounts)/sizeof(newAccounts[0]); a++)
printf("Name %s %s ID %d some Decimal Number %.2f\n", newAccounts[a].fristName,
newAccounts[a].lastName,newAccounts[a].IDNumber,newAccounts[a].decimal_point_number);
fclose(outfile);
return 0;
}
use it how ever you want.