質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Q&A

解決済

2回答

660閲覧

C言語 text fileについて

yumaishii

総合スコア3

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

0グッド

1クリップ

投稿2020/12/11 01:44

編集2020/12/11 02:50

前提・実現したいこと

どうすれば 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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dodox86

2020/12/11 01:53

タグが"C#"になっています。質問がC言語に関することなのであればタグは "C"に変更してください。タグが違うと閲覧者の目にとまりづらくなります。 あと、細かいですが 正: Visual Studio 2019 誤: visual stadio2019 です。"stadio" ではありません。
pepperleaf

2020/12/12 09:22

コンパイルの設定はどうなってるのでしょうか? 上記のコードを c ソースとしてコンパイル(cl test.c)すると、エラーでコンパイル失敗。 c++としてコンパイル(cl test.cpp)すると、d: にファイルが作られました。
guest

回答2

0

妹のパソコンでやったら出来ました

投稿2020/12/14 02:53

編集2020/12/14 02:55
yumaishii

総合スコア3

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

まず、

f = fopen("d:\grocery.txt", "w");

ここで、ファイルのオープンが失敗していないかを見てみよう。
失敗してればファイルは作られません

投稿2020/12/11 01:57

y_waiwai

総合スコア87800

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yumaishii

2020/12/11 02:53

どうやって見れば良いんでしょうか? ローカルには何も保存されていません
y_waiwai

2020/12/11 02:57 編集

VisualStudio を使っているなら、ブレークポイントを設定すれば、そこで実行を止めて変数の中身を見ることができます。 また、1行づつ実行することもできます。 こうやって、その行の直後で実行を止めてfの値を見てみよう
otn

2020/12/11 03:14

> どうやって見れば良いんでしょうか? if((f = fopen("d:\grocery.txt", "w"))==NULL){ perror("fopen"); exit(1); } これはfopenの使い方の基本です。
yumaishii

2020/12/12 01:22

ファイルのオープンはできるていると思いますが、 fprintのところで ❌triggered a breakpointというのが出てきてしまいます
y_waiwai

2020/12/12 03:10

オープンができているとはどうやって判断したんでしょうか。 んで、fprintfのところでブレークポイント設定してませんか。 そこで止まるのでそのまま実行押せばまた走り出します
yumaishii

2020/12/12 05:26 編集

txtfileが作れていませんでした、
y_waiwai

2020/12/12 05:40

少なくとも、オープンが成功していればファイルはできるはず、です できていないなら、オープンは失敗しています
yumaishii

2020/12/12 06:12

オープンが失敗ていると思います。 これはプログラミングの問題でしょうか。 もしくは、セキリュティソフトの問題などもあるのでしょうか?
y_waiwai

2020/12/12 06:15

Dドライブになにかファイルを作ってみてください。できますか?
otn

2020/12/12 08:31

おそらくオープンに失敗しているのだから、失敗の原因を調べれば良いと思うのですが。
yumaishii

2020/12/12 09:09

すいません、どのように失敗の原因を調べれば良いんでしょうか?
otn

2020/12/12 09:15

既に書きました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問