访问成员结构时出现无效数字
Invalid number comes when accessing member structures
我是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语言新手。我将结构成员 (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);
}
了解按值调用和按引用调用,以及将结构作为参数传递在网上,如果你还不清楚。