文件i / o写入和读取文件c

时间:2015-04-13 13:25:20

标签: c file-io

我是c编程的新手。我刚开始学习文件i / o。所以我的问题是在输入写入文件的所有数据后,我的程序说有问题然后退出。我程序的哪一部分是错的?

  //THIS PROGRAM WRITE RECORDS IN A TXT.FILE AND THEN READ AND DISPLAY THE RECORDS IN A TXT.FILE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>

struct student
{
    char MatricNo[20];
    int matric[20];
    char CourseNo[20];
    int course[20];
    char Grade[20];
    float Value[20];
 }s[10];

int count=0;

int i,j, no,nu;

int write();
 int read(int i, int j);

int main()
{
int choice;
printf("To record data to txt type <1>\nTo read data from txt type <2>\nTo end the program type <3>\n\n");
    printf("Your Choice: ");
    scanf("%i",&choice);
while(choice!=3)
{


    switch(choice)
    {
        case 1:
            write();
            break;
        case 2:
            read(i, j);
            break;
        case 3:
            return 0;
            break;
        default:
            break;
    }
    printf("To record data to txt type <1>\nTo read data from txt type <2>\nTo end the program type <3>\n\n");
    printf("Your Choice: ");
    scanf("%i",&choice);
}

fflush(stdin);
getchar();

return 0;
}

// for write data to txt file

int write()
{
    FILE *fw;



   fw=fopen("Register Table.txt","w");



      printf("\nEnter number of student :");
      scanf("%d",&no);

    for(i=0;i<no;i++)
{
    printf("\nEnter student MatricNo :");
    scanf("%s",&s[i].MatricNo);
    printf("\nEnter number of courses :");
    scanf("%d",&nu);
    for(j=0; j < nu; j++)
    {
    printf("\nEnter student CourseNo :");
    scanf("%s",&s[j].CourseNo);
    printf("\nEnter student Grade :");
    scanf("%s",&s[j].Grade);
    printf("\nEnter student value :");
    scanf("%.2f",&s[j].Value);
    fflush(stdin);
fprintf(fw,"%5s %5s %5s %.2f\n",s[i].MatricNo,s[j].CourseNo,s[j].Grade,s[j].Value); 
        fclose(fw); 
   }
   }


   }

int read(int i, int j)
{
FILE *fr;


//read data from txt file
fr=fopen("Register Table.txt","r");

if(!fr){
    printf("not file");
    return 0;
}
else
return 1;

printf("\n\n\t **Register Table  **\n");

    while(fscanf(fr,"%5s %5s %5s %.2f",&s[i].MatricNo,&s[j].CourseNo,&s[j].Grade,&s[j].Value)==1)
    {
            //display data from txt file
printf("\n\n MatricNo\t CourseNo\t Grade\t\t Value");
for(i=0;i<no;i++)
{
    for(j=0; j < nu; j++)
    {
        printf("\n\n %5s %5s %5s %.2f",s[i].MatricNo,s[j].CourseNo,s[j].Grade,s[j].Value);

    }
}

printf("\n\n");
    }

fclose(fr);


}
好吧,我编辑了我的程序,就是这样。但现在问题是在我写完文件后,无法读取。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>

    struct student
    {
        char MatricNo[20];
        int matric[20];
        char CourseNo[20];
        int course[20];
        char Grade[20];
        float Value[20];
    }s[10];

    int count=0;

    int i,j, no,nu;

    int write();
    int read(int no, int nu);
    FILE *fw;
    int main()
    {
        int choice;
        printf("To record data to txt type <1>\nTo read data from txt type <2>\nTo end the program type <3>\n\n");
            printf("Your Choice: ");
            scanf("%i",&choice);
        while(choice!=3)
        {


            switch(choice)
            {
                case 1:
                    write();
                    break;
                case 2:
                    read(no, nu);
                    break;
                case 3:
                    return 0;
                    break;
                default:
                    break;
            }
            printf("To record data to txt type <1>\nTo read data from txt type <2>\nTo end the program type <3>\n\n");
            printf("Your Choice: ");
            scanf("%i", &choice);
        }

        fflush(stdin);
        getchar();
        return 0;

    }

    // for write data to txt file

    int write()
    {




        fw=fopen("Register Table.txt","w");



            printf("\nEnter number of student :");
            scanf("%d", &no);

            for(i=0;i<no;i++)
        {
            printf("\nEnter student MatricNo :");
            scanf("%s", &s[i].MatricNo);
            printf("\nEnter number of course :");
            scanf("%d", &nu);
            for(j=0; j < nu; j++)
            {
            printf("\nEnter student CourseNo :");
            scanf("%s", &s[i].CourseNo[j]);
            printf("\nEnter student Grade :");
            scanf("%s", &s[i].Grade[j]);
            printf("\nEnter student value :");
            scanf("%f", &s[i].Value[j]);



        }


        }


                fprintf(fw,"%s %s %s %.2f\n", s[i].MatricNo, s[i].CourseNo[j], s[i].Grade[j], s[i].Value[j]);   

                fclose(fw);

    }

    int read(int no, int nu)
    {
        FILE *fr;


        //read data from txt file
        fr=fopen("Register Table.txt","r");


        printf("\n\n\t **Register Table  **\n");


                for(i=0;i<no;i++)
                    {
                         for(j=0; j < nu; j++)
                            {
                                fscanf(fr,"%s %s %s %.2f", &s[i].MatricNo, &s[i].CourseNo[j], &s[i].Grade[j], &s[i].Value[j]);
                            }
                    }
                    //display data from txt file
        printf("\n\n MatricNo\t CourseNo\t Grade\t\t Value");



            for(i=0;i<no;i++)
                {
                         for(j=0; j < nu; j++)
                        {

                            printf("%s %s %s %.2f", s[i].MatricNo, s[i].CourseNo[j], s[i].Grade[j], s[i].Value[j]);
                             }
                }




        printf("\n");






        fclose(fr);

    }

1 个答案:

答案 0 :(得分:0)

1 - 您无法写入文件,因为您在for循环中关闭了write()函数中的FILE指针fw。

2 - 您无法阅读,因为退出read()功能文件无效或使用此代码无效。

if(!fr){
    printf("not file");
    return 0;
}
else
return 1;

3-只有在read()之后调用write()函数时,no函数才能正常工作。如果您未首先调用nu,则代码不知道write()#include <stdio.h> #include <string.h> #include <stdlib.h> struct student { char MatricNo[20]; int matric[20]; char CourseNo[20]; int course[20]; char Grade[20]; float Value[20]; } s[10]; int count=0; int i,j, no,nu; int write(); int read(int no, int nu); FILE *fw; int main() { int choice; printf("To record data to txt type <1>\nTo read data from txt type <2>\nTo end the program type <3>\n\n"); printf("Your Choice: "); scanf("%i",&choice); while(choice!=3) { switch(choice) { case 1: write(); break; case 2: read(no, nu); break; case 3: return 0; break; default: break; } printf("To record data to txt type <1>\nTo read data from txt type <2>\nTo end the program type <3>\n\n"); printf("Your Choice: "); scanf("%i", &choice); } fflush(stdin); getchar(); return 0; } // for write data to txt file int write() { fw=fopen("Register Table.txt","w"); printf("\nEnter number of student :"); scanf("%d", &no); for(i=0;i<no;i++) { printf("\nEnter student MatricNo :"); scanf("%s", &s[i].MatricNo); printf("\nEnter number of course :"); scanf("%d", &nu); for(j=0; j < nu; j++) { printf("\nEnter student CourseNo :"); scanf("%s", &s[i].CourseNo[j]); printf("\nEnter student Grade :"); scanf("%s", &s[i].Grade[j]); printf("\nEnter student value :"); scanf("%f", &s[i].Value[j]); fprintf(fw,"%s %s %s %.2f\n", &s[i].MatricNo, &s[i].CourseNo[j], &s[i].Grade[j], &s[i].Value[j]); } } fclose(fw); } int read(int no, int nu) { FILE *fr; //read data from txt file fr=fopen("Register Table.txt","r"); printf("\n\n\t **Register Table **\n"); for(i=0;i<no;i++) { for(j=0; j < nu; j++) { fscanf(fr,"%s %s %s %.2f", &s[i].MatricNo, &s[i].CourseNo[j], &s[i].Grade[j], &s[i].Value[j]); } } //display data from txt file printf("\n\n MatricNot CourseNot Gradett Value"); for(i=0;i<no;i++) { for(j=0; j < nu; j++) { printf("\n%s %s %s %.2f", &s[i].MatricNo, &s[i].CourseNo[j], &s[i].Grade[j], &s[i].Value[j]); } } printf("\n"); fclose(fr); } 的值。


我认为此代码适合您

{{1}}