Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我的Fwrite无法工作,或者我的文件似乎无法在devc++;_C++_C - Fatal编程技术网

C++ 我的Fwrite无法工作,或者我的文件似乎无法在devc++;

C++ 我的Fwrite无法工作,或者我的文件似乎无法在devc++;,c++,c,C++,C,这是我代码中的代码,当我在文件指针中添加一些记录时,我看不到列表结果,似乎写得不太好,或者代码中有某种错误,而且当我添加记录时,我尝试列出它,但它不会显示任何记录,或者我认为列表工作得不太好 #include<stdio.h> #include<string.h> #include<conio.h> #include<ctype.h> #include<windows.h> struct { char studno

这是我代码中的代码,当我在文件指针中添加一些记录时,我看不到列表结果,似乎写得不太好,或者代码中有某种错误,而且当我添加记录时,我尝试列出它,但它不会显示任何记录,或者我认为列表工作得不太好

   #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();
                     }
#包括
#包括
#包括
#包括
#包括
结构{
char-studno[10];
字符名[80];
字符段[20];
智力年龄;
}学生[20];
文件*文件\指针;
int t;
无效添加();
作废清单();
void gotoxy(整数x,整数y)
{ 
合作社;
坐标X=X;
坐标Y=Y;
设置控制台或位置(GetStdHandle(标准输出句柄),坐标);
}      
int main()
{
智力选择;
对于(;;)
{             
哥特氧(35,5);
printf(“[1]添加学生记录\n”);
gotoxy(28,10);
printf(“[2]列出学生记录\n”);
gotoxy(28,12);
printf(“[3]退出\n\n”);
gotoxy(35,15);
printf(“选择:”);
scanf(“%d”,选择(&C);
如果(选项==1){
添加();
系统(“cls”);
}
else if(选项==2){
list();
系统(“cls”);
}
else if(选项==3){
出口(0);
}                
}
}
void add()
{
查尔安斯;
int i;
对于
add()
中的(i=0;i)


for(i=0;i请修复缩进。什么是
student
?它是如何声明/定义的?结构是如何定义的?student是我把记录放在上面的人这是它的结构{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)应该删除if?但当我按数字2显示所有注册学生的列表时,它仍然不会显示。当我在文件指针上放置记录时,它似乎也在打破列表
for(i=0;i<20;i++){
   ...
   fwrite(student[i+1].studno,sizeof(student[i+1].studno),i,file_pointer);
   ...
}