C语言文件写入问题

qq_41320307 2017-12-18 06:00:43
如果文件不存在时就可以重复调用函数向文件写入数据,当文件存在时第二次运行程序就无法继续向里面写入数据,在fwrite后用fflush也没有解决问题,求指点
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#include<string.h>
#define MAX 40
struct time
{
int hour;
int min;
};
struct order
{
char name[30];
int no;
struct time ortime;
int number;
};
struct order order1[MAX];
int input(void)
{
FILE *or;
int count = 0;
int filecount, index, test, a, test1, test2;
char judge;
int size = sizeof(struct order);
errno_t err;
system("cls");
while ((err=fopen_s(&or,"order.dat", "a+b")) != 0)
{
fputs("Can't creat the file\n", stderr);
system("pause");
exit(1);
}
rewind(or);
while (fread(&order1[count], size, 1, or ) == 1 && order1[count].no != 0)
{
count++;
}
for (filecount = count; filecount < MAX; filecount++)
{
if (count == MAX)
{
printf("The order.dat file is full!\n");
system("pause");
break;
}
system("cls");
do
{
printf("Please enter guest's No.:\n");
a = scanf_s("%d", &order1[filecount].no);
getchar();
test = order1[filecount].no;
if ((test < 1e-6 &&test>40)|| a == 0)
printf("Unexpectable ,please enter a number in range 1-40\n");
} while ((test < 1e-6 &&test>40) || a == 0);
printf("Please enter guest's name:\n");
gets(order1[filecount].name);
do
{
printf("Please enter the number of people:\n");
a = scanf_s("%d", &order1[filecount].number);
getchar();
test = order1[filecount].number;
if ((test < 1e-6 &&test>40) || a == 0)
printf("Unexceptable ,please enter a number in range 1-40\n");
} while ((test < 1e-6 &&test>40) || a == 0);

do
{
printf("Please enter order time(XX,XX):\n");
a=scanf_s("%d,%d", &order1[filecount].ortime.hour, &order1[filecount].ortime.min);
getchar();
test1 = order1[filecount].ortime.hour;
test2 = order1[filecount].ortime.min;
if ((test1 <= 0 || test1>24) || (test2 < 0 || test2>59) || a < 2)
{
printf("Unexceptable ,please enter right time\n");
}
} while ((test1 <= 0 || test1>24) || (test2 < 0 || test2>59) || a < 2);
fwrite(&order1[filecount], size, 1, or );
do
{
printf("Do you want continue?(y/n)\n");
a = scanf_s("%c", &judge);
while (getchar() != '\n')
continue;
if (judge != 'y' && judge != 'n' && judge != 'Y' && judge != 'N')
printf("please try again!\n");
} while (judge != 'y'&&judge != 'n' &&judge != 'Y'&& judge != 'N');
if (judge == 'N' || judge == 'n')
break;
}
fclose(or );
}
int main(void)
{
int a, b, count;
struct order guest[20];
struct order *p;
p = guest;
for (count = 0; count < MAX; count++)
{
order1[count].no = 0;
order1[count].name[40] = '\0';
order1[count].number = 0;
order1[count].ortime.hour = 0;
order1[count].ortime.min = 0;
}
for (;;)
{
system("cls");
printf("----------Welcome To Guest Manager System----------\n");
printf(" MENU \n");
printf("---------------------------------------------------\n");
printf("| 1.Input guests information |\n");
printf("| 2.Sort guests information |\n");
printf("| 3.Read guests information |\n");
printf("| 4.Find guest information |\n");
printf("| 5.Edit guest information |\n");
printf("| 6.Delete guest information |\n");
printf("| 7.Quit |\n");
printf("---------------------------------------------------\n");
for (;;)
{
printf("Please enter the function number: ");
b = scanf_s("%d", &a);
getchar();
printf("\n");
if (b == 0)
printf("Wrong quest!Try again!\n");
else break;
}
switch (a)
{
case 1:input(); break;
case 2:sort(); break;
case 3:read(); break;
case 4:find(); break;
case 5:edit(); break;
case 6:del(); break;
case 7:exit(1); break;
}
}
return 0;
}
...全文
263 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wodexiaojidan 2017-12-19
  • 打赏
  • 举报
回复
引用 4 楼 qq_41320307 的回复:
[quote=引用 3 楼 wodexiaojidan的回复:]文件大小都被你搞死了,第一次就写满了40个单位大小,后面还怎么写
多谢指点,问题已解决,想再问一下如果想初始化结构体数组除了逐个赋值还有什么办法呢[/quote] 定义的时候就赋上初值
wodexiaojidan 2017-12-19
  • 打赏
  • 举报
回复
引用 4 楼 qq_41320307 的回复:
[quote=引用 3 楼 wodexiaojidan的回复:]文件大小都被你搞死了,第一次就写满了40个单位大小,后面还怎么写
多谢指点,问题已解决,想再问一下如果想初始化结构体数组除了逐个赋值还有什么办法呢[/quote]像普通数组一样
qq_41320307 2017-12-19
  • 打赏
  • 举报
回复
引用 3 楼 wodexiaojidan的回复:
文件大小都被你搞死了,第一次就写满了40个单位大小,后面还怎么写
多谢指点,问题已解决,想再问一下如果想初始化结构体数组除了逐个赋值还有什么办法呢
wodexiaojidan 2017-12-19
  • 打赏
  • 举报
回复
文件大小都被你搞死了,第一次就写满了40个单位大小,后面还怎么写
qq_41320307 2017-12-19
  • 打赏
  • 举报
回复
引用 1 楼 自信男孩的回复:
while ((err=fopen_s(&or,"order.dat", "a+b")) != 0)
打开文件的方式,有"a+b",我记得有"ab+",没见过"a+b";
order1[count].name[40] = '\0';
这句是不是数组越界访问了,数组长度是30,下标应该从0~29;这儿用40,已经越界。
a = scanf_s("%c", &judge);
scanf_s缺少参数,是不是应该是这样:
a = scanf_s("%c", &judge, 1);
多谢指点,但数据还是没有办法写入文件,a+b的写法是从C Primer plus上看来的,和ab+似乎是等价的,但似乎不是那个问题
自信男孩 2017-12-19
  • 打赏
  • 举报
回复
while ((err=fopen_s(&or,"order.dat", "a+b")) != 0)
打开文件的方式,有"a+b",我记得有"ab+",没见过"a+b";
order1[count].name[40] = '\0';
这句是不是数组越界访问了,数组长度是30,下标应该从0~29;这儿用40,已经越界。
a = scanf_s("%c", &judge);
scanf_s缺少参数,是不是应该是这样:
a = scanf_s("%c", &judge, 1);

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧