gpt4 book ai didi

c - 访问成员结构时出现无效数字

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:07 25 4
gpt4 key购买 nike

我是c语言新手。我将结构成员 (int dd) 添加到文件 (file.txt),现在我试图读取该文件以访问结构成员 (Day.dd)。但是当我通过 scanf() 函数将 5 添加到一个变量并编译该文件并运行时,我没有得到 (Day = 5)我得到 Day = 1325715504

My code is:
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
struct DailyCost
{
int dd;

} Day;
FILE *fp;
void add_dailycost();
void display();
void mainmenue();
int main()
{

int j;
fp = fopen("/home/mohit/projects/c/gedit_Dailyost/TextFiles/fileb.txt", "w+");
rewind(fp);
mainmenue();
return 0;
}
void mainmenue()
{
struct DailyCost day1;
int j;
printf("Enter 1 to add dailycost and enter 2 display dailycost.");
scanf("%d", &j);
switch (j)
{
case 1:
add_dailycost(day1);
break;
case 2:
display(day1);
break;
}
}
void add_dailycost(struct DailyCost Day)
{
if (fp != NULL)
{
fseek(fp, 0L, SEEK_END);
}
int j;
printf("Enter Day = ");
scanf("%d", &Day.dd);
fwrite(&Day, sizeof(struct DailyCost), 1, fp);
printf("If menmenue press 1 and exit press 0 = ");
scanf("%d", &j);
if (j == 1)
{
mainmenue();
}
else
{
printf("\nThank you");
fclose(fp);
exit(0);
}
}
void display(struct DailyCost Day)
{
fread(&Day, sizeof(struct DailyCost), 1, fp);
rewind(fp);
printf("Day = %d\n", Day.dd);
fclose(fp);
}

请指出我在编写程序时做错了什么。以及如何通过使用我的代码来解决这个问题。

最佳答案

根据 Klaus Gütter 的建议使用引用调用,对原始对象进行更改。

#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
struct DailyCost
{
int dd;

} Day;
FILE *fp;
void add_dailycost();
void display();
void mainmenue();
int main()
{

int j;
fp = fopen("/home/mohit/projects/c/gedit_Dailyost/TextFiles/fileb.txt", "w+");
rewind(fp);
mainmenue();
return 0;
}
void mainmenue()
{
struct DailyCost day1;
int j;
printf("Enter 1 to add dailycost and enter 2 display dailycost.");
scanf("%d", &j);
switch (j)
{
case 1:
add_dailycost(&day1);
break;
case 2:
display(&day1);
break;
}
}
void add_dailycost(struct DailyCost* Day) /*if you pass simply
struct DailyCost , all the contents of original objects will be copied
into a new memory area, to be used in this function and will be
destroyed when this function exits. */
/* if you pass just the address of your object,you are working on
the same memory area as you passed from,in this case from mainmenue. */
{
if (fp != NULL)
{
fseek(fp, 0L, SEEK_END);
}
int j;
printf("Enter Day = ");
scanf("%d", &Day->dd);
fwrite(Day, sizeof(struct DailyCost), 1, fp);
printf("If menmenue press 1 and exit press 0 = ");
scanf("%d", &j);
if (j == 1)
{
mainmenue();
}
else
{
printf("\nThank you");
fclose(fp);
exit(0);
}
}
void display(struct DailyCost* Day) /* passing the object instead
of reference would also work here. But just for the display
purpose, you need not to unnecessarily create a new memory. */
{
fread(Day, sizeof(struct DailyCost), 1, fp);
rewind(fp);
printf("Day = %d\n", Day->dd);
fclose(fp);
}

如果您还不清楚,请阅读互联网上的按值调用按引用调用,以及将结构作为参数传递

关于c - 访问成员结构时出现无效数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53722435/

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