鱼C论坛

 找回密码
 立即注册
查看: 1414|回复: 2

文件处理问题

[复制链接]
发表于 2015-9-23 11:11:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
输入框不能停止结束,不知道原因,代码如下:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 4

struct student
{
        char name[10];
        int phone;
        char addr[15];
}stu[SIZE];

void main()
{
        int i;
       
        printf("Please input the student's name phone and address: \n");
       
        for(i=0; i<SIZE; i++)
        {
                scanf("%s %d %s",&stu[i].name,&stu[i].phone,&stu[i].addr);
        }
       
        save();
        system("pause");
}

void save()
{
        FILE *fp;
        int i;
       
        if(!(fp = fopen("student.txt","wb")))
        {
                printf("Cannot find the file!\n");
                return;
        }
       
        for(i=0; i<SIZE; i++)
        {
               
                if( fwrite(&stu[i],sizeof(struct student),1,fp) != 1)
                {
                        printf("File write error!\n");
                        fclose(fp);
                }
                                               
        }
                       
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-10-8 15:53:43 | 显示全部楼层
怎么没人指点一下?我运行以后输入的是5组数组元素,不明白???
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-10-8 18:14:29 | 显示全部楼层
我输入的是四组啊,没有问题吧
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define SIZE 4

  4. struct student {
  5.   char name[10];
  6.   int phone;
  7.   char addr[15];
  8. } stu[SIZE];

  9. void save()
  10. {
  11.   FILE *fp;
  12.   int i;

  13.   if(!(fp = fopen("student.txt","wb"))) {
  14.     printf("Cannot find the file!\n");
  15.     return;
  16.   }

  17.   for(i=0; i<SIZE; i++) {

  18.     if( fwrite(&stu[i],sizeof(struct student),1,fp) != 1) {
  19.       printf("File write error!\n");
  20.       fclose(fp);
  21.     }

  22.   }

  23. }

  24. void main()
  25. {
  26.   int i;

  27.   printf("Please input the student's name phone and address: \n");

  28.   for(i=0; i<SIZE; i++) {
  29.     scanf("%s %d %s",&stu[i].name,&stu[i].phone,&stu[i].addr);
  30.   }

  31.   save();
  32.   system("pause");
  33. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 08:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表