Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中向文件写入/读取结构_C_Struct - Fatal编程技术网

在C中向文件写入/读取结构

在C中向文件写入/读取结构,c,struct,C,Struct,我对如何使用file和struct写入/读取数据感到非常困惑。我已经在网上搜索过了,但仍然很困惑 当我想保存结构时,它将以中文保存。当我想从文件中读取时,它会读取其他内容 你能帮我吗 这是我的结构 struct yogurt { char name[50]; char topping[50]; int size; int harga; }*record; 这是处理文件读写操作的代码部分 void menu1(void) { FILE *in; i

我对如何使用file和struct写入/读取数据感到非常困惑。我已经在网上搜索过了,但仍然很困惑


当我想保存结构时,它将以中文保存。当我想从文件中读取时,它会读取其他内容

你能帮我吗

这是我的结构

struct yogurt
{
    char name[50];
    char topping[50];
    int size;
    int harga;
}*record;
这是处理文件读写操作的代码部分

void menu1(void)
{
    FILE *in;
    in = fopen("data.txt","a");
    int nm,tp,sz;
    record = (struct yogurt *)malloc(sizeof(struct yogurt));
    bool loop;
    char sure[10];
    while (loop = true)
    {
        printf("Input your yogurt [chocolate/vanilla/strawberry] : ");
        scanf("%s",record->name);
        if (strcmp(record->name,"chocolate") == 0)
        {
            nm = 9;
            break;
        }
        else if (strcmp(record->name,"vanilla") == 0)
        {
            nm = 7;
            break;
        }
        else if (strcmp(record->name,"strawberry") == 0)
        {
            nm = 10;
            break;
        }
    }
    while (loop = true)
    {
        printf("Input your topping [kitkat/jelly/kiwi/mango] : ");
        scanf("%s",record->topping);
        if (strcmp(record->topping,"kitkat") == 0)
        {
            tp = 6;
            break;
        }
        else if (strcmp(record->topping,"jelly") == 0)
        {
            tp = 5;
            break;
        }
        else if (strcmp(record->topping,"kiwi") == 0)
        {
            tp = 4;
            break;
        }
        else if (strcmp(record->topping,"mango") == 0)
        {
            tp = 5;
            break;
        }
    }
        printf("Input your size [100..500] : ");
        scanf("%d",&record->size);
        sz = record->size;
        record->harga = nm * tp * sz * 10;
        printf("\n\nDetail Purchase:\n");
        printf("Name : %s\n", record->name);
        printf("Topping : %s\n", record->topping);
        printf("Size : %d\n", record->size);
        printf("Price : %d\n", record->harga);
        while (loop = true)
        {
        printf("Are you sure [y/n] : ");
        scanf("%s", sure);
        if (strcmp(sure, "y") == 0)
        {
        fwrite(record,sizeof(struct yogurt),1,in);
        fclose(in);
        free(record);
        printf("Data succesfully added....");
        count++;
        _getch();
        system("cls");
        main();
        }
        else if (strcmp(sure, "n") == 0)
        {
            system("cls");
            main();
        }
        }

}

void menu2(void)
{
    FILE *in;
    in = fopen("data.txt", "r");
    record = (struct yogurt*)malloc(sizeof(struct yogurt));
    int i = 1;
    while(i<=count)
    {
        fread(record,sizeof(struct yogurt),i,in);
        printf("Name : %s\n", record->name);
        printf("Topping : %s\n", record->topping);
        printf("Size : %d\n", record->size);
        printf("Price : %d\n", record->harga);
        i++;
    }
    fclose(in);
    free(record);
    _getch();
    system("cls");
    main();
}
void菜单1(void)
{
文件*in;
in=fopen(“data.txt”,“a”);
int-nm,tp,sz;
记录=(struct酸奶*)malloc(sizeof(struct酸奶));
布尔环;
char-sure[10];
while(loop=true)
{
printf(“输入你的酸奶[巧克力/香草/草莓]:”;
scanf(“%s”,记录->名称);
如果(strcmp(记录->名称,“巧克力”)==0)
{
nm=9;
打破
}
else if(strcmp(记录->名称,“香草”)==0)
{
nm=7;
打破
}
else if(strcmp(记录->名称,“草莓”)==0)
{
nm=10;
打破
}
}
while(loop=true)
{
printf(“输入你的配料[kitkat/jelly/kiwi/mango]:”;
扫描(“%s”,记录->顶部);
如果(strcmp(记录->顶部,“kitkat”)==0)
{
tp=6;
打破
}
否则如果(strcmp(记录->浇头,“果冻”)==0)
{
tp=5;
打破
}
否则如果(strcmp(记录->顶部,“猕猴桃”)==0)
{
tp=4;
打破
}
否则如果(strcmp(记录->浇头,“芒果”)==0)
{
tp=5;
打破
}
}
printf(“输入您的尺寸[100..500]:”;
scanf(“%d”,&记录->大小);
sz=记录->大小;
记录->充电=nm*tp*sz*10;
printf(“\n\n详细购买:\n”);
printf(“名称:%s\n”,记录->名称);
printf(“打顶:%s\n”,记录->打顶);
printf(“大小:%d\n”,记录->大小);
printf(“价格:%d\n”,记录->价格);
while(loop=true)
{
printf(“您确定[y/n]:”);
scanf(“%s”,当然);
如果(strcmp(当然,“y”)==0)
{
fwrite(记录,sizeof(struct酸奶),1英寸);
fclose(in);
免费(记录);
printf(“数据成功添加…”);
计数++;
_getch();
系统(“cls”);
main();
}
else if(strcmp(当然,“n”)==0)
{
系统(“cls”);
main();
}
}
}
void菜单2(void)
{
文件*in;
in=fopen(“data.txt”,“r”);
记录=(struct酸奶*)malloc(sizeof(struct酸奶));
int i=1;
while(在南部);
printf(“打顶:%s\n”,记录->打顶);
printf(“大小:%d\n”,记录->大小);
printf(“价格:%d\n”,记录->价格);
i++;
}
fclose(in);
免费(记录);
_getch();
系统(“cls”);
main();
}

如果要将其保存在文本文件中,则需要将其序列化(转换为文本格式):

而不是
fwrite(记录,sizeof(struct酸奶),1,in);fclose(in)

您需要这样的内容(创建逗号分隔的文件)


要以二进制格式保存(当然也要读取),在模式字符串中添加字母“b”更安全。因为我们不知道是什么平台-有些平台可以以不同的方式处理文本和二进制文件。它在任何符合POSIX标准的系统上都是不需要的,并且将被忽略-因此它是无害的。请尝试使用
“ab”
“rb”
模式字符串。

确保以二进制模式打开文件

使用文本模式可能会损坏
int size,int harga
成员的二进制数据

如果在不同的
*ninx
类编译器上,这将不会改善情况,因为文本模式和二进制模式没有这种差异

// in = fopen("data.txt","a");
in = fopen("data.txt","ab");

// in = fopen("data.txt", "r");
in = fopen("data.txt", "rb");

下面的代码是作业。通常这里的代码需要比较。由于
循环
未在后面读取,因此OP的意图尚不清楚

// while (loop = true)
while (loop == true)
// or even better
while (loop)

fread的返回值(记录,sizeof(struct酸奶),i,in)记录
成员之前,应检查code>

else if(strcmp(当然,“n”)==0)
缺少前面的
if()

未声明计数

该文件不是真正的文本文件,因此建议不要使用
.txt
后缀

可能还存在其他问题


最好发布真正的可编译代码。

一个观察结果,While==true not While=true,您的赋值假设不是这样做的。您需要初始化变量(例如,
bool loop
),使用比较(
=
),而不是在
While
循环中赋值,而不是从函数调用
main()
,可能还有其他一些东西。另外,我假设您正在查看记事本或其他文本编辑器中保存的文件,并看到“中文”。数据以二进制形式保存,通常不能作为文本查看;为什么我?
while(loop=true)
应该是
while(loop==true)
或者
while(loop)
当然可以,但是您永远不会初始化
loop
,也不会在循环中的任何地方更改其值。所以不清楚您想做什么。“我想保存结构,它将以中文保存。”-->您是如何确定它是以中文保存的?这很重要。从同一程序读取/写入原始结构是安全的。这不是问题的根源,这不是我的全部answer@dbush文件名:
data.txt
说明了我的操作意图。我已经尝试了你的代码并添加了“ab”,但当我打开data.txt时,它仍然是中文。如何使其可读?感谢您的回答。我已经用添加的“ab”和使用==with循环更正了我的代码。但是使用.txt后缀意味着什么呢?因为我想在打开数据时使文件可读。txt@Ivan乌辛
// while (loop = true)
while (loop == true)
// or even better
while (loop)