gpt4 book ai didi

c - 为什么我的 File.dat 不会被读取和运行,我如何将数据分配给结构? - C

转载 作者:太空宇宙 更新时间:2023-11-04 07:51:19 30 4
gpt4 key购买 nike

问题:我是 C 的新手,甚至是 structs 的新手,当我尝试简单地打印出 beer.dat 文件中的数据时,我我出现了一个错误,我知道我没有正确读取它,但是一旦文件正确流式传输,我如何才能将我的数据存储在我的结构中?

为了清楚起见,我将发布作业问题...任务是:

  1. 搜索啤酒时应提示用户输入 ID 号,结果应显示其名称数量和价格,如果它在您的库存中。
  2. 整个库存 View 将显示所有啤酒及其 ID 号、价格和数量按价格升序排列。这种排序应该使用递归来完成冒泡或递归选择排序。
  3. 下订单时,应将订单发票打印到屏幕上。

beer.dat 文件内容:

7 // total number of beers 
Coors //beer name
1234567 // beer id
72 // beer quantity
7.40 //beer price
Miller
7777777
44
9.70
Bud
7654321
345
9.90
Wachusett
7799435
4
14.70
Corona
9999999
112
9.99
Zima
0000000
1
0.01
Mikes
0890398
12
10.99

代码:

#include "beer.dat" //loading required file... in other news - making groundbreaking comments like this because I'm forced to, stay tuned!
#include <stdio.h> // including the thing
#include <stdlib.h>
#include <string.h>

struct beerData {
char *beer[7]; // number of brands

char *beer_name; //names
int beer_id[7]; //ID number given to beer
int beer_quantity; //stock
float beer_price; // pricing
} beer_data;

int main() {
int user_choice;
printf("Enter 1 to search for a beer, 2 to view the entire catalogue,"
" and 3 to place an order, press 4 to exit.\n");
scanf("%d", &user_choice);

while (user_choice != 4) {
switch (user_choice) {
case 1:
printf("Searching for a beer\n");
search();
break;
case 2:
printf("Viewing Inventory\n");
//viewInv();
break;
case 3:

printf("Placing an order...\n");
//placeOrder();
break;
case 4:
printf("Goodbye!\n");
exit(0);
default:
printf("Incorrect entry, try again.\n");
continue;
}
}


}

void search() {
//look through beer.dat for a specific beer by ID number.

FILE *fp = fopen("beer.dat", "r");

if (fp == NULL) {
printf("Error: can't open file to read\n");
} else {
while (!feof(fp)) {
fscanf(fp, "%s ", fp);
//printStr(fp);
}


}

fclose(fp);
}

void printStr(char *line) {
printf("%s\n", line);
}

初始错误:编译器给我,

In file included from C:\Users\me\CLionProjects\work\main.c:32:0:
C:\Users\me\CLionProjects\work\beer.dat:1:1: error: expected identifier or '(' before numeric constant
7
^
C:\Users\me\CLionProjects\work\beer.dat:27:1: error: invalid digit "9" in octal constant
0890398
^~~~~~~

非常感谢您提供的任何帮助。我正处于严重的抑郁症发作期间,因此我很难理解这个新概念。这是本学期的期末作业。再次感谢您关注这个问题。当然不要求完成作业!!

最佳答案

您希望您的程序在执行时从 beer.dat 中读取内容。该文件不包含属于您的程序的代码。编译器告诉您它不知道如何将文件中的内容解释为 C 程序的一部分。

因此删除行 #include "beer.dat"

稍后使用 fopenfscanfsearch 中打开并读取文件。

您还缺少 search 的前向声明。要么将 search 函数放在 main 之前,要么添加像

这样的声明
void search();

main 之前,这样当您在 main 中调用它时,编译器就知道 search 需要哪些参数以及它返回什么。

使用 -Wall -Wextra 启用一些额外的警告也告诉我们

<source>: In function 'search':
<source>:58:26: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'FILE *' {aka 'struct _IO_FILE *'} [-Wformat=]
fscanf(fp, "%s ", fp);
~^ ~~
<source>:58:13: warning: passing argument 1 to restrict-qualified parameter aliases with argument 3 [-Wrestrict]
fscanf(fp, "%s ", fp);
^~~~~~

因为您正在尝试从处理程序 fp(第一个参数)引用的文件中读取,并且您正在指示 fscanf 将读取的字符串保存到文件处理程序 fp 正在指向(第三个参数)。当然这没有任何意义,你想把它保存在一些 char 数组中。

关于c - 为什么我的 File.dat 不会被读取和运行,我如何将数据分配给结构? - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53688045/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com