我的Fwrite不会工作,或者看起来我的文件在Dev c ++中不起作用

时间:2014-03-08 08:48:34

标签: c++ c

这是我的代码中的代码当我在file_pointer中添加一些记录时,我没有看到列表结果似乎fwrite不能很好地工作或者如果我的代码中出现错误,那么就会出现错误我把添加记录然后我试图列出它不会显示任何记录或我认为列表不能很好地工作

   #include<stdio.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<windows.h>

struct {
       char studno[10];
       char name[80];
       char section[20];
       int age;
           }student[20];
       FILE *file_pointer;
       int t; 
       void add();
       void list();
       void gotoxy(int x, int y) 
{ 
    COORD coord;
    coord.X = x; 
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}      
      int main()
      {
         int choice;
         for(;;)
     {             

                gotoxy(35,5);
                printf("[1]Add Student Record\n");
                gotoxy(28,10);
                printf("[2]List Student Record\n");
                gotoxy(28,12);
                printf("[3]Exit\n\n");
                gotoxy(35,15);
                printf("Choice: ");
                scanf("%d",&choice);
                if(choice==1){
                add();
                system("cls");
                }
                else if(choice==2){
                list();
                system("cls");
                }
                else if(choice==3){
                exit(0);
}                
                }
                }
     void add()
     {
          char ans;
          int i;
              for(i=0;i<20;i++){
                               system("cls");
                                gotoxy(31,2);
                                printf("ADD STUDENT RECORDS\n\n");
                                gotoxy(31,5);
                                printf("\t\t\t\tRecord Number: %d\n\n",i+1);
                                gotoxy(3,7);
                                printf("Student Number: ");
                                scanf("%s",&student[i].studno);
                                 gotoxy(3,9);
                                printf("Name: ");
                                scanf("%s",&student[i].name);
                                gotoxy(3,11);
                                printf("Section: ");
                                scanf("%s",&student[i].section);
                                 gotoxy(3,13);
                                printf("Age: ");
                                scanf("%d",&student[i].age);
                                t++;

                                if((file_pointer=fopen("student.dat","w"))!=NULL)
                                {
                                                fwrite(student[i+1].studno,sizeof(student[i+1].studno),i,file_pointer);
                                                            gotoxy(31,18);
                                                printf("\nAdd another? [Y/N]: ");
                                                scanf("%s",&ans);
                                                if(toupper(ans)=='N')
                                                {
                                                                    fclose(file_pointer);
                                                                     i=20;
                                                                     break;
                                                system("cls");
                                                }
                                                }
                                                else
                                                {
                                                printf("Error writing student.dat\n");
                                                fclose(file_pointer);
                                                               gotoxy(31,18);
                                                printf("\nAdd another? [Y/N]: ");
                                                scanf("%s",&ans);
                                                if(toupper(ans)=='N')
                                                {
                                                                     i=20;
                                                                     break;
                                                system("cls");
                                                }
                                                }

              }
              }
           void list()
              {
                      system("cls");
                    int i,x=0,y=8;
                    gotoxy(31,2);
                    printf("LIST STUDENT RECORD\n\n");
                     gotoxy(3,5);
                    printf("Record No.");
                     gotoxy(3,6);
                    printf("-------------");
                     gotoxy(19,5);
                    printf("Student No.");
                     gotoxy(19,6);
                    printf("-------------");
                     gotoxy(35,5);
                    printf("Name");
                     gotoxy(35,6);
                    printf("-------------");
                     gotoxy(59,5);
                    printf("Section");
                     gotoxy(59,6);
                    printf("-------------");
                     gotoxy(73,5);
                    printf("Age");
                     gotoxy(73,6);
                    printf("-------------");

                    for(i = 0;i<t;)
                    {
                    if((file_pointer=fopen("student.dat","r"))!=NULL)
                    {
                     fread(student[i+1].studno,sizeof(student[i+1].studno),i,file_pointer);
                     }
                     else
                     {
                     printf("Error writing student.dat\n");
                     fclose(file_pointer);
                      gotoxy(x+3,y);
                     printf("      -%d-",i+1);
                    gotoxy(x+23,y);
                     printf("%s",student[i].studno);
                      gotoxy(x+35,y);
                     printf("%s",student[i].name);
                      gotoxy(x+59,y);
                     printf("%s",student[i].section);
                      gotoxy(x+73,y);
                     printf("%d",student[i].age);
                     y++;
                     }
                    fclose(file_pointer);
                    i=t;
                     }
                     getch();
                     }

1 个答案:

答案 0 :(得分:0)

add()

for(i=0;i<20;i++){
   ...
   fwrite(student[i+1].studno,sizeof(student[i+1].studno),i,file_pointer);
   ...
}

对于i = 0fwrite0 * sizeof(student[i+1].studno)个字节的数组写入file_pointer指向的内存块,最后一次迭代i = 19写入19 * sizeof(student[i+1].studno)的数组字节不是你想要的。