
November 23rd, 2012, 01:17 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 1
Time spent in forums: 4 m 43 sec
Reputation Power: 0
|
|
|
File IO - C program records and list monthly expenses
I am trying to write a C program using fseek, fopen, fwrite, SEEK_CUR, SEEK_END to record my monthly expenses, every purchase that I make I can add in to the program and look up how much I have spend. I am having trouble writing the code to delete an item? and when it records to the file that I have written it doubles de recording, and I dont know why?
Please help!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
void Expense_type (int);
void Expense_type2 (int option);
int main (void)
{
int option;
printf("\n Please choose an option below\n\n"
"\n (1) Launch income / expense \n"
"\n (2) Cancel Expense || Edit entry \n"
"\n (3) See types of launch\n"
"\n (4) Register types of launch\n"
"\n (0) Exit\n");
scanf("%d", &option);
if (option > 0 && option <= 5)
{
Expense_type(option);
}
else
printf("\nOption invalid!");
system ("pause");
return 0;
}
void Expense_type (int option)
{
int option2, found = 0;
char purchase[20];
float amount, sum;
FILE *p;
while (option == 1)
{
printf("\nPlease choose an option to add your Expenses:\n\n"
"\n (1) Include \n"
"\n (2) Make a change \n"
"\n (3) Exclude an item \n"
"\n (4) Consult \n"
"\n (5) List all \n"
"\n (0) Exit \n\n");
scanf("%d", &option2);
// ************************************************** ************************************************** ***
if (option2 == 1)
{
p = fopen("CADASTRO.DAT","a+"); /* apend file (add text to
a file or create a file if it does not exist.*/
if (!p)
{
printf("File could not be opened\n\n");
}
printf("\n Purchase Type: ");
scanf("%s", purchase);
printf("\n How much: ");
scanf("%f", &amount);
fprintf(p, "%s\t%f\n", purchase, amount);
fclose (p);
} // Option 1 if statement
//************************************************** ************************************************** ***********
if (option2 == 2)
{
p = fopen("CADASTRO.DAT","a+"); /* apend file (add text to
a file or create a file if it does not exist.*/
if (!p)
{
printf("File could not be opened\n\n");
}
// ???? Como alterar o file???
} // chave que fecha option2 == 2
//************************************************** ************************************************** *****************
if (option == 3)
{
p = fopen("CADASTRO.DAT","a+"); /* apend file (add text to
a file or create a file if it does not exist.*/
if (!p)
{
printf("File could not be opened\n\n");
}
// Excluir algo do arquivo
} // chave que fecha option == 3
//************************************************** ************************************************** **********************
if (option == 4)
{
p = fopen("CADASTRO.DAT","a+"); /* apend file (add text to
a file or create a file if it does not exist.*/
if (!p)
{
printf("File could not be opened\n\n");
}
//Consultar o arquivo CADASTRO.DAT
} // chave que fecha option 4
//************************************************** ***************************************
if (option2 == 5)
{
p = fopen("CADASTRO.DAT","r"); // open file para listar tudo
if (p == NULL)
{
perror("\nError when trying to open file CADASTRO.DAT\n");
}
else
while (!feof(p) )
{
fscanf(p, "%s ", purchase);
fscanf(p, "%f", &amount);
sum += amount;
printf("\n Type of purchase: %s Valor R$: %.2f \n", purchase, amount);
}
printf("\n Total Value: %.2f \n", sum);
fclose(p);
}
//************************************************** ************************************************** ***********
if (option2 == 0)
{
exit (0) ;
}
} //bracket do while
} // Close function number 2
|