This is not my actual code but a knock off of what i made so that i dont go copying and pastin whatever ppl send me... this is kind of a short version.
Code:
#include <stdio.h>
#include <stdlib.h>
struct acct
{
int accountNumber;
char givenName[20];
float balance;
};
struct allAccounts
{
struct acct a000001;
struct acct a000002;
}accounts;
void clearKeyboardBuffer()
{
char ch;
while ((ch = getchar()) != '\n' && ch != EOF);
}
char mainMenu(int numOfAccounts)
{
char menuChoice;
menuChoice = '0';
while (menuChoice != '6')
{
system("clear");
printf("* 1 - Print all the accounts *\n");
printf("* 2 - Edit an existing account *\n");
printf("* 3 - create new account *\n");
printf("Enter your choice here (1, 2, 3,): ");
menuChoice = getchar();
clearKeyboardBuffer();
if (menuChoice == '1')
printFile(numOfAccounts);
if (menuChoice == '2')
edit(numOfAccounts);
if (menuChoice == '3')
newAccount(numOfAccounts);
}
return menuChoice;
}
int printFile(int numOfAccounts)
{
system("clear");
FILE *File;
ipFile = fopen("info.csv", "r");
char *t`,*t2,*t3;
char stuff[80];
char *search = ",";
float balance;
fgets(buf, 80, File);
t1 = strtok(input, ",\0");
account=atoi(t1);
printf("%d\n", account);
t2 = strtok(NULL, search);
printf("%s\n", t2);
t3 = strtok(NULL, search);
balance =atof(t7);
printf("%.2f\n", balance);
fclose(ipFile);
clearKeyboardBuffer();
}
main()
{
int numOfAccounts;
numOfAccounts = 0;
mainMenu(numOfAccounts);
}
struct info is held in a .csv file instead of placed in code with 3lines saying (goin account id,name, balance)
4,joe,55
3,fred,77
2,bill,109
I believe my printFile function is opening up my file, reading it so it can take in the info and use the strok command to extract the data. (do I have to do it again and again depending on how many lines my .csv file has?) Mainly all this function does is print everything in my file.
Code:
newAccount(numOfAccounts);
char str1[160];
sprintf(str1," %d, %s, %s, %s, %0.2f,",a1.accountNumber,\
a1.givenName,a1.balance);
opFile = fopen("data.csv","w");
if (opFile == NULL)
{
printf("sorry cant open the file\n");
return(-1);
}
fprintf(opFile, "%s\n",str1);
fclose(opFile);
opFile = fopen("data.csv","r");
if (dataFile == NULL)
{
printf("sorry cant open the file\n");
return(-1);
}
fgets(str1,160,opFile);
fclose(opFile);
I have this code from a previous assignment to convert it to a string, BUT what do i modify to make sure it writes into the file correctly under the rest of the file (got an error using this format by the way it looked in my .csv file) .
Any suggestions or code snippets on how to edit previously saved text from a .csv file is helpful as i'm completly stuck on how to do that one
thanks, and hope this isn't confusing