前提・実現したいこと
どうすれば textfile に保存できるかを教えてもらいたい。
または、間違えているところを教えてもらいたい
発生している問題・エラーメッセージ
text fileに読み込めない Unhandled exception at 0x7AB8F2F6 (ucrtbased.dll) in CS36 Project10.exe: An invalid parameter was passed to a function that considers invalid parameters fatal
該当のソースコード
void savetext(struct Grocery g[], float totalcost, float totaldisc, float totalcwd, int n) { FILE* f; f = fopen("d:\grocery.txt", "w"); for (int i = 0; i < n; i++) { fprintf(f, "%s\n", g[i].item); fprintf(f, "%0.2f %0.2f %0.2f\n", totalcost, totaldisc, totalcwd); } fclose(f); }
### ソースコード
#define _CRT_SECURE_NO_WARNINGS #define SIZE 4 #include<stdio.h> #include<stdlib.h> #include<string.h> struct Grocery { char item[20], brand[20]; int qty; float price, disc, cost, discamt, cwd; }; void load(struct Grocery g[], float* totalcost, float* totaldisc, float* totalcwd, int n) { for (int i = 0; i < n; i++) { printf("Enter an item name "); gets_s(g[i].item); printf("Enter a Brand name "); gets_s(g[i].brand); printf("Enter the priece "); scanf("%f", &g[i].price); printf("Enter the quantity "); scanf("%d", &g[i].qty); printf("Enter the discount percent "); scanf("%f", &g[i].disc); g[i].cost = g[i].price * g[i].qty; g[i].discamt = g[i].cost * g[i].disc / (float)100; g[i].cwd = g[i].cost - g[i].discamt; *totalcost += (g[i].cost); *totaldisc += (g[i].disc); *totalcwd += (g[i].cwd); } } void sort(struct Grocery g[], int n) { int i, j; Grocery t; for (i = 0; i < n - 1; i++) for (j = 0; j < n - 1; j++) if (strcmp(g[j].item, g[j + 1].item) > 0) { t = g[j]; g[j] = g[j + 1]; g[j + 1] = t; } } void print(struct Grocery g[], float totalcost, float totaldisc, float totalcwd, int n) { for (int i = 0; i < n; i++) { printf("Item:%s\n", g[i].item); } printf("\nTotal cost is $%0.2f", totalcost); printf("\nTotal discount is $%0.2f", totaldisc); printf("\nTotal cost with discount is $%0.2f\n", totalcwd); } void savetext(struct Grocery g[], float totalcost, float totaldisc, float totalcwd, int n) { FILE* f; f = fopen("d:\grocery.txt", "w"); for (int i = 0; i < n; i++) { fprintf(f, "%s\n", g[i].item); fprintf(f, "%0.2f %0.2f %0.2f\n", totalcost, totaldisc, totalcwd); } fclose(f); } void retrievetext(struct Grocery g[], float totalcost, float totaldisc, float totalcwd, int n) { FILE* f; int length; f = fopen("d:\grocery.txt", "r"); for (int i = 0; i < n; i++) { fgets(g[i].item, sizeof(g[i].item), f); length = strlen(g[i].item); g[i].item[length - 1] = '\0'; fscanf(f, "%0.2f %0.2f %0.2f\n", totalcost, totaldisc, totalcwd); } fclose(f); } void savebin(struct Grocery g[], float totalcost, float totaldisc, float totalcwd, int n) { FILE* f; f = fopen("d:\grocery.bin", "wb"); fwrite(&g, sizeof(g[0]), n, f); fclose(f); } void retrievebin(struct Grocery g[], float totalcost, float totaldisc, float totalcwd, int n) { FILE* f; f = fopen("d:\grocery.bin", "rb"); fread(&g, sizeof(g[0]), n, f); fclose(f); } void main() { Grocery g[SIZE]; float totalcost = 0, totaldisc = 0, totalcwd = 0; load(g, &totalcost, &totaldisc, &totalcwd, SIZE); sort(g, SIZE); print(g, totalcost, totaldisc, totalcwd, SIZE); savetext(g, totalcost, totaldisc, totalcwd, SIZE); retrievetext(g, totalcost, totaldisc, totalcwd, SIZE); printf("\n\nAfter the text file is retrieved\n\n"); print(g, totalcost, totaldisc, totalcwd, SIZE); savebin(g, totalcost, totaldisc, totalcwd, SIZE); retrievebin(g, totalcost, totaldisc, totalcwd, SIZE); printf("\n\nAfter the text file is retrieved\n\n"); print(g, totalcost, totaldisc, totalcwd, SIZE); system("PAUSE"); }
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
Visual studio 2019
回答2件
あなたの回答
tips
プレビュー