How to make monthly and yearly calculations from a txt file in C language? I'm trying to make a C language read and write program, but I'm confused about how to perform operations from it. I'm confused, please help me
This is an example of a txt file that was written
Jaka 2022 12 10 50000
Juki 2020 10 12 750000
Jaka 12 10 10 4000
And here's the code that I made, it's still limited to calculating all total expenses, I can't calculate monthly and yearly.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
void input();
void count();
void monthly_expenditure();
int main()
{
int choice;
do
{
printf("====================================MENU====================================\n");
printf("1. Input Data\n");
printf("2. count expenditure\n");
printf("3. monthly expenditure\n");
printf("4. exit\n");
printf("choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
input();
getchar();
break;
case 2:
count();
getchar();
break;
case 3:
monthly_expenditure();
getchar();
break;
case 4:
printf("thank you\n");
getchar();
break;
default:
printf("error\n");
break;
}
} while (choice != 4);
return 0;
}
void input()
{
FILE *fp;
char name[100], year[100], month[100], date[100], money[100];
fp = fopen("data.txt", "a");
printf("Input name : ");
scanf("%s", name);
printf("Input year : ");
scanf("%s", year);
printf("Input month : ");
scanf("%s", month);
printf("Input date : ");
scanf("%s", date);
printf("Input money : ");
scanf("%s", money);
fprintf(fp, "%s %s %s %s %s\n", name, year, month, date, money);
fclose(fp);
}
void count()
{
FILE *fp;
char name[100], year[100], month[100], date[100], money[100];
int total = 0;
fp = fopen("data.txt", "r");
while (fscanf(fp, "%s %s %s %s %s", name, year, month, date, money) != EOF)
{
total += atoi(money);
}
printf("Total expenditure : %d\n", total);
fclose(fp);
}
void monthly_expenditure(){
}
void year_expenditure(){
}
Create one function that passes in the desired year and month.
Use fgets() to read a line of input, then parse. This has far better error handling.
Do not test against EOF, one of the many return values from *scanf() that you do not want. Test against the return value you want.
Use int for year, month, day.
Use a width with %s to avoid buffer overflow.
Recommend a wider type for money.
Illustrative untested code:
// Filter per year. Use 0 for all years, all months
// Return error code.
int count_filter(int year, int month) {
FILE *fp = fopen("data.txt", "r");
if (fp == NULL) {
return 1;
}
long total = 0;
char buf[200];// Some large buffer
while (fgets(buf, sizeof buf, fp)) {
char name[100];
int y, m, d;
long money;
if (sscanf(buf, "%99s %d %d %d %ld",
name, &y, &m, &d, &money) != 5) {
// Incorrect format
fclose(fp);
return 3;
}
if ((year == 0 || year == y) && (month == 0 || month == m)) {
total += money;
}
}
fclose(fp);
printf("Total expenditure: %ld\n", total);
return 0;
}
Now call as needed:
count_filter(0,0); All expenditures
count_filter(2022,12); Just this month
count_filter(2021,0); Last year
count_filter(0,12); All Decembers
Related
Try as I might to solve this riddle, I think I'm losing my mind! Can anyone help understand why this program crashes when I run through the loop a 2nd time?
I can run through the interactive loop one time and have the values entered written to a file. However, when I attempt to pass through the a loop a 2nd time, the program chokes.
// C Libraries Used
#include <stdio.h>
#include <math.h>
#include <string.h>
// Constant definitions
const float OTPAYFACTOR = 1.5;
const float REGWORKWKHRS = 40;
FILE *payfile; // report file (for output)
// Variable declerations
char deptname [21];
char firstname [10];
char lastname [10];
char fullname [47];
float hrsworked;
float hrwage;
float reghrsworked;
float othrsworked;
float otwage;
float grosswage;
int count;
char again;
// Function Prototypes
//**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~
// M A I N F U N C T I O N
//**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~
int main (void){
payfile = fopen("c:\\class\\mod6\\ethan-pay.txt","w"); // Open disk file
printf("Mountain Pacific Corporation\nDepartment Salary Program\n\n");
printf("Please enter the name of the department: ");
scanf("%s", deptname);
count = 0; // Initialize this "counting" variable to zero to start
printf("%s", deptname);
printf("\n");
do {
printf("Enter employee #%d: ", count+1);
scanf("%s %s", firstname, lastname);
fscanf(payfile,"%s %s", firstname, lastname);
strcpy(fullname, firstname);
strcat(fullname, " ");
strcat(fullname, lastname);
printf("Enter the hourly wage of %s: ", fullname);
scanf("%f", &hrwage);
fscanf(payfile,"%f", &hrwage);
printf("Enter total number of hours: ");
scanf("%f", &hrsworked);
fscanf(payfile,"%f", &hrsworked);
if (hrsworked <= REGWORKWKHRS){ //
reghrsworked = hrsworked;
othrsworked = 0;
otwage = hrwage * OTPAYFACTOR;
grosswage = hrwage*reghrsworked;
}
else{
reghrsworked = REGWORKWKHRS;
othrsworked = hrsworked - REGWORKWKHRS;
otwage = hrwage * OTPAYFACTOR;
grosswage = (reghrsworked * hrwage) + (othrsworked * otwage);
}
fprintf(payfile,"%-22s%0.1f ($%0.2f) %6.1f ($%0.2f) $%-4.2f\n", fullname, reghrsworked, hrwage, othrsworked, otwage, grosswage);
printf("\nThank you. Process another employee? ");
scanf ("%s", &again);
printf("\n");
count++; // Increment the counting variable
} while (again == 'Y'|| again == 'y' || again != 'N' && again != 'n');
printf("End of processing.\n");
fclose(payfile);
return 0;
}
I don't understand why are you scanning your file that you use for save the result:
scanf("%s %s", firstname, lastname);
fscanf(payfile,"%s %s", firstname, lastname);
// ...
scanf("%f", &hrwage);
fscanf(payfile,"%f", &hrwage);
// ...
scanf("%f", &hrsworked);
fscanf(payfile,"%f", &hrsworked);
What you do erase previous value. Just remove all call to fscanf().
I strongly advice you to change your code practice, global should be avoid, only declare variable when you need them, don't ignore error code from function, provide to scanf() the max size that it can use, etc...
example probably not perfect:
#include <stdio.h>
#include <string.h>
#define OTPAYFACTOR 1.5
#define REGWORKWKHRS 40
int main(void) {
printf("Mountain Pacific Corporation\nDepartment Salary Program\n\n"
"Please enter the name of the department: ");
char deptname[21];
if (scanf("%20s", deptname) != 1) {
return 1;
}
printf("%s\n", deptname);
FILE *payfile = fopen("c:\\class\\mod6\\ethan-pay.txt", "w");
if (!payfile) {
return 1;
}
for (int count = 0;; count++) {
printf("Enter employee #%d: ", count + 1);
char firstname[10];
char lastname[10];
if (scanf("%9s %9s", firstname, lastname) != 2) {
return 1;
}
char fullname[19];
sprintf(fullname, "%s %s", firstname, lastname);
printf("Enter the hourly wage of %s: ", fullname);
float hrwage;
if (scanf("%f", &hrwage) != 1) {
return 1;
}
printf("Enter total number of hours: ");
float hrsworked;
if (scanf("%f", &hrsworked) != 1) {
return 1;
}
if (hrsworked <= REGWORKWKHRS) {
float reghrsworked = hrsworked;
float othrsworked = 0;
float otwage = hrwage * OTPAYFACTOR;
float grosswage = hrwage * reghrsworked;
fprintf(stdout, "%-22s%0.1f ($%0.2f) %6.1f ($%0.2f) $%-4.2f\n", fullname,
reghrsworked, hrwage, othrsworked, otwage, grosswage);
} else {
float reghrsworked = REGWORKWKHRS;
float othrsworked = hrsworked - REGWORKWKHRS;
float otwage = hrwage * OTPAYFACTOR;
float grosswage = (reghrsworked * hrwage) + (othrsworked * otwage);
fprintf(stdout, "%-22s%0.1f ($%0.2f) %6.1f ($%0.2f) $%-4.2f\n", fullname,
reghrsworked, hrwage, othrsworked, otwage, grosswage);
}
printf("\nThank you. Process another employee? ");
char again;
if (scanf(" %c", &again) != 1) {
return 1;
}
if (again != 'Y' && again != 'y') {
break;
}
printf("\n");
}
fclose(payfile);
printf("End of processing.\n");
}
example input:
jurasickpark
sarah connor 5 42
y
bob lennon 9 12
n
output file:
sarah connor 40.0 ($5.00) 2.0 ($7.50) $215.00
bob lennon 12.0 ($9.00) 0.0 ($13.50) $108.00
The input is retrieve from text file. Which contain information of
product ID
product Name
Product Quantity
product Price
1 RAYBAN 1 450.000000
900 KEYBOARD 100 290.000000
78 MINERALWATER 123 345.000000
2 RAYBAN 2 450.000000
After printing the output through command prompt. It was not align with the 1st item. How to make it align with the title of table. As you can see the input of line 1 and 4 almost the same.
Here is the output.
Here is the full code. With gotoxy function. The display function is on
int displayProduct()
There is a line of code for table titles and also the printf from TXT File.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
void gotoxy(int column, int line);
int main();
int addProduct();
int displayProduct(); //prototype
struct product {
int quantity, reorder, i, id;
char name[20];
float price;
};
COORD coord = { 0, 0 };
void gotoxy(int x, int y) {
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main() {
int choice;
gotoxy(17, 5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17, 20);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(22, 8);
printf("1. Add Product\n\n");
gotoxy(22, 10);
printf("2. Display Product\n\n");
gotoxy(22, 12);
printf("3. Search Product\n\n");
gotoxy(22, 14);
printf("4. Reorder Level of Product\n\n");
gotoxy(22, 16);
printf("5. Update Product\n\n");
gotoxy(22, 18);
printf("6. Exit\n\n");
gotoxy(22, 22);
printf("Please Enter Your Choice : ");
scanf(" %d", &choice);
switch (choice) {
case 1:
addProduct();
break;
case 2:
displayProduct();
break;
case 3:
searchProduct();
break;
case 4:
reorderProduct();
break;
case 5:
updateProduct();
break;
case 6:
break;
default:
system("cls");
main();
}
return (0);
}
/*MENU CODE ENDS !*/
int addProduct() {
FILE *fp;
int i = 0;
struct product a;
system("cls");
fp = fopen("inventory.txt", "a+t");
char checker;
do {
system("cls");
gotoxy(17, 5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17, 20);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(22, 8);
printf("Enter product ID : ");
scanf(" %d", &a.id);
gotoxy(22, 10);
printf("Enter product name : ");
scanf(" %s", a.name);
gotoxy(22, 12);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
gotoxy(22, 14);
printf("Enter product price : ");
scanf(" %f", &a.price);
gotoxy(22, 17);
fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price); //SAVE TO TXT FILE LINE !
printf("Record saved!\n\n");
fclose(fp);
gotoxy(22, 22);
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
i++;
system("cls");
} while (checker=='Y');
if (checker == 'N') {
main();
} else {
do {
system("cls");
gotoxy(17, 5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17, 20);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(18, 8);
printf(">>> Wrong Input! Please Enter Y Or N Only! <<<");
gotoxy(19, 12);
printf("Do You Want To Enter New Product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
} while (checker != 'Y' && checker != 'N');
if (checker == 'Y'){
addProduct();
}
if (checker == 'N') {
system("cls");
main();
}
}
return(0);
}
/*ADD PRODUCT LINE ENDS !*/
int displayProduct() {
FILE *fp;
struct product a;
char true;
system("cls");
fp = fopen("inventory.txt", "r");
gotoxy(17, 5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(5, 6);
printf("======================================================================");
gotoxy(5, 7);
printf("Product ID\t\t Product Name\t\t Quantity\t Unit Price\n"); //TABLE TITLES !
gotoxy(5, 8);
printf("======================================================================");
gotoxy(5,10);
while (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4) {
printf("%d\t\t\t %s\t\t\t %d\t\t %.2f\n\n", a.id, a.name, a.quantity, a.price); //PRINT FROM TXT FILE TO COMMAND PROMPT.
}
fclose(fp);
printf("\t\t \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
printf("\nPress any key to return to Main Menu.");
getch();
int main();
return (0);
}
Updated one, changes made :
gotoxy(5,10);
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
printf("%-10d\t\t %-12s\t\t %8d\t %8.2f\n\n", a.id, a.name, a.quantity, a.price);
}
fclose(fp);
The code to print the inventory should use the length specifier in printf format like this:
gotoxy(0, 10);
while (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4) {
printf(" %-10d\t\t %-12s\t\t %8d\t %8.2f\n\n", a.id, a.name, a.quantity, a.price);
}
Some notes regarding the code:
it is very bad style to call main() recursively. Use a loop instead.
write a function that prints the header instead of duplicating the code multiple times.
the statement int main(); at the end of displayProduct() is a local declaration for function main, it does not generate a call.
your menu doesn't go back to the main menu after pressing any key,
there you have to use main(); instead ogf int main();
i made this program and its working just how i want but it should stop when i type x but it doesn't
can any one tell me why?
and if there is any shortcut or smaller way to type this code?
thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int meat[6];
int i;
int total =0;
int avg;
char what[20];
char end;
int days = 0;
char food[20];
char drink[20];
printf("what thing do u want to calculate the average of?(food/drink)\n");
scanf(" %s", &what);
if(strcmp(what, "food")==0)
{
printf("what sort of food did u ate ?\n");
scanf(" %s", food);
}
if(strcmp(what, "drink")==0)
{
printf("what sort of drink did u drank ?\n");
scanf(" %s", drink);
}
for(i=0; i<6; i++)
{
days++;
if(strcmp(what, "food")==0)
{
printf("how many %s did u ate in day %d\n", food, i+1);
}
else if(strcmp(what, "drink")==0)
{
printf("how many liters of %s did u drank in day %d\n", drink, i+1);
}
scanf(" %d", &meat[i]);
total += meat[i];
printf("if u want to continue type y\n");
printf("type x if u want to finish\n");
scanf(" %c", &end);
if((end = 'y')&&(i<5))
{
continue;
}
else if(end = 'x' && strcmp(what, "drink")==0)
{
avg = total / days;
printf("\nyour average amount of liters of %s you had %d\t the total is %d\n", drink, avg, total);
}
else if(end = 'x' && strcmp(what, "food")==0)
{
avg = total / days;
printf("\nyour average amount of %s you had %d\t the total is %d\n", food, avg, total);
}
break;
}
if(strcmp(what, food)==0)
{
printf("\nyour average amount of %s you had is %d\t the total is %d\n", food, avg, total);
}
else if(strcmp(what, drink)==0)
{
printf("\nyour average amount of liters of %s you had is %d\t the total is %d\n", drink, avg, total);
}
return 0;
}
else if(end = 'x' ...
should be:
else if(end == 'x' ...
You use == to test equality in if statements. You've got this in a couple places in your code, which is inadvertently performing an assignment, not what you want to achieve by comparing if user input is equal to a particular character.
Replace the = with == here:
else if(end = 'x' && strcmp(what, "drink")==0)
here:
else if(end = 'x' && strcmp(what, "food")==0)
and here:
if((end = 'y')&&(i<5))
The illegal character error is coming from my print function in my else statement. And when I try and run the file, I receive a segmentation fault:11. The goal of this program is to enter a lowerbound temperature from 0-80 and a upper bound temperature greater than the lowerbound but less than 90. As well as a percentage of days needed in the range.
#include <stdio.h>
int main(){
// Enter information in regards to your city
int percentage_range;
double low_bound, upper_bound, temperature;
printf("Please enter the lower temperature bound in Fahrenheit.\n");
scanf("%lf", &low_bound);
printf("Please enter the upper temperature bound in Fahrenheit.\n");
scanf("%lf", &upper_bound);
printf("Please enter the percentage of days needed in the range.\n");
scanf("%d", &percentage_range);
// Read in the file name
char filename[20];
printf("Please enter the name of your weather data file for your city.\n");
scanf("%s", filename);
// Open the file
FILE* ifp = fopen(filename, "r");
int month, day, year;
fscanf(ifp, "%d", &month);
// looping totals
double sum = 0;
double sum_range = 0;
int numdays = 0;
int num_totaldays = 0;
double percentage;
while (month != -1){
num_totaldays++;
// Read in the rest of the file line
fscanf(ifp, "%d,%d,%lf",&day, &year, &temperature);
while (low_bound <= upper_bound){
sum = sum + temperature;
numdays++;
percentage = sum/numdays;
printf("Days in between %lf and %lf degrees:%d", low_bound, upper_bound, numdays);
printf("Total number of days:%d",num_totaldays );
printf("Percentage of days in range:%lf",percentage);
// if the percentage is in range
if (percentage >= percentage_range){
printf("Great, I recommend opening a Pizza Shack at your location!\n");
}
// if not print this
else
printf("Sorry, your weather isn’t temperate enough for Pizza Shack.");
}
}
//close file
fclose(ifp);
return 0;
}
Replace the "isn’t" with "isn't". The apostrophe character you are using is weird.
Okay so I'm building a program for work to track volume each day and also split the volume up to how much each different location receives. There are 8 lines in the building. The Text file consists of the day's date, the total volume for the building and the volume for each location (PEN).
Im running into two problems right now both are segregated to my readDay function. where result = fscanf(pi->Date, 40, fp);
issues are
1. Incompatible integer to pointer conversion passing 'int' to parameter of type 'const char*'
2. Incompatible pointer types passing 'char[40]' to parameter of type 'FILE*'(aka 'struct_sFILE*')
Can anyone explain to me what these issues mean. Or if you need any more info in order to help me feel free to ask.
#include <stdio.h>
#include <stdlib.h>
struct Day {
char Date[40];
int Total;
int Pen1;
int Pen2;
int Pen3;
int Pen4;
int Pen5;
int Pen6;
int Pen7;
int Pen8;
};
const char fileName [] = "UPS.txt";
void StartRecord(void);
void printDay(struct Day* pi);
void writeDay(FILE *fp, struct Day *pi);
int readDay(FILE *fp, struct Day *pi);
void readFile(void);
void addDay(void);
int main(int argc, const char * argv[]) {
StartRecord();
int choice;
printf("Enter 1 to read this file or enter 2 to add to it.");
scanf("%d", &choice);
switch (choice) {
case 1: readFile();
break;
default: printf("Not a valid choice\n");
break;
}
getchar();
getchar();
}
void StartRecord(void) {
int count;
int i;
struct Day aDay;
FILE *fp;
fp = fopen(fileName, "w");
if (fp != NULL) {
printf("Enter volume for how many days? ");
scanf("%d", &count);
for (i = 0; i < count; ++i) {
printf("What is today's date? ");
gets(aDay.Date);
printf("What was the total volume for the building for the day?");
scanf("%d", &aDay.Total);
printf("What was the volume for Pen 1? ");
scanf("%d", &aDay.Pen1);
printf("What was the volume for Pen 2? ");
scanf("%d", &aDay.Pen2);
printf("What was the volume for Pen 3? ");
scanf("%d", &aDay.Pen3);
printf("What was the volume for Pen 4? ");
scanf("%d", &aDay.Pen4);
printf("What was the volume for Pen 5? ");
scanf("%d", &aDay.Pen5);
printf("What was the volume for Pen 6? ");
scanf("%d", &aDay.Pen6);
printf("What was the volume for Pen 7? ");
scanf("%d", &aDay.Pen7);
printf("What was the volume for Pen 8? ");
scanf("%d", &aDay.Pen8);
getchar();
writeDay(fp, &aDay);
}
fclose(fp);
}else {
printf("\nError opening file.\n");
}
}
void readFile(void) {
FILE *fp;
struct Day aDay;
fp = fopen(fileName, "r");
if (fp != NULL) {
while (readDay(fp, &aDay)) {
printDay(&aDay);
}
fclose(fp);
} else {
printf("\nError opening File!\n");
}
}
void writeDay(FILE *fp, struct Day *pi) {
fprintf(fp, "%s\n", pi->Date);
fprintf(fp, "%d\n", pi->Total);
fprintf(fp, "%d\n", pi->Pen1);
fprintf(fp, "%d\n", pi->Pen2);
fprintf(fp, "%d\n", pi->Pen3);
fprintf(fp, "%d\n", pi->Pen4);
fprintf(fp, "%d\n", pi->Pen5);
fprintf(fp, "%d\n", pi->Pen6);
fprintf(fp, "%d\n", pi->Pen7);
fprintf(fp, "%d\n", pi->Pen8);
}
int readDay(FILE *fp, struct Day *pi) {
int result;
fgetc(fp); // read the endline
result = fscanf(pi->Date, 40, fp);
if (result == EOF) {
return 0;
}
fscanf(fp, "%d", &pi->Total);
fscanf(fp, "%d", &pi->Pen1);
return 1;
}
void PrintDay(struct Day *pi) {
printf("Records for the day of: %s\n", pi->Date);
printf("Total Volume: %d", pi->Total);
printf("Volume for Pen 1: %d", pi->Pen1);
printf("Volume for Pen 2: %d", pi->Pen2);
printf("Volume for Pen 3: %d", pi->Pen3);
printf("Volume for Pen 4: %d", pi->Pen4);
printf("Volume for Pen 5: %d", pi->Pen5);
printf("Volume for Pen 6: %d", pi->Pen6);
printf("Volume for Pen 7: %d", pi->Pen7);
printf("Volume for Pen 8: %d", pi->Pen8);
printf("\n\n");
}
In one sense, your arguments are the wrong way round. This:
fscanf(pi->Date, 40, fp);
should be:
fscanf(fp, pi->Date, 40);
just like you already have it a few lines further down.
In another sense, what you're trying to do is very strange - you can't read anything into a literal integer. You probably want something more like:
fscanf(fp, "%s", pi->Date);
or possibly:
fscanf(fp, "%39s", pi->Date);
or similar.