使用 fopen 创建文件,但文件未出现在驱动器上

标签 c io

#include<stdio.h>
#include<alloc.h>

/*What the linked list contains.*/
struct stud
{
    char studNumber[13],lastName[21],firstName[21];
    float finalGrade;
    struct stud *sp;
};

/*This subroutine will SHOW the MENU to the user.*/
showMenu()
{
    int choice;
    clrscr();

    do
    {
        printf("MENU:\n\n1. CREATE\n2. READ\n3. UPDATE\n4. DELETE\n5. SAVE\n6. QUIT\n\nEnter your choice : ");
        scanf("%d",&choice);
    }while( choice<1 || choice>6 );

    return choice;
}

/*This subroutine will insert a new record at the end of the list.*/
struct stud* createRec( struct stud *head,int recordCounter,int firstInputCheck )
{
    struct stud *trail;
    char ans;

    clrscr();

    /* To see if there was already a first input. */
    if( firstInputCheck==0 )
    {
        ans='y';

        printf("Enter the student's student number : ");
        scanf("%s",head->studNumber);
        printf("\nEnter the student's last name : ");
        scanf("%s",head->lastName);
        printf("\nEnter the student's first name : ");
        scanf("%s",head->firstName);
        printf("\nEnter the student's final grade : ");
        scanf("%f",&head->finalGrade);

        trail=head;
        trail->sp=NULL;
        recordCounter++; /* A counter to see if it already exceeded 100 records. */
        firstInputCheck++; 

        printf("\n\nWould you like to enter another record? [y/n]");
        ans=getche();
    }

    while( recordCounter!=100 && ( (ans=='y') || (ans=='Y') ) )
    {
        /* Input more records. */
        while( (ans=='y') || (ans=='Y') )
        {
            struct stud *t=malloc( sizeof(struct stud) );

            clrscr();

            trail->sp=t;

            printf("Enter the student's student number : ");
            scanf("%s",t->studNumber);
            printf("\nEnter the student's last name : ");
            scanf("%s",t->lastName);
            printf("\nEnter the student's first name : ");
            scanf("%s",t->firstName);
            printf("\nEnter the student's final grade : ");
            scanf("%f",&t->finalGrade);

            trail=t;
            recordCounter++;

            printf("\n\nWould you like to enter another record? [y/n]");
            ans=getche();
        }
    }

    trail->sp=NULL;

    return head;
}

/* This subroutine will let the user input the student number and will display the desired record. */   
void showRec( struct stud *head )
{
    int foundIt=0;
    struct stud *trail;
    char stdNum[12];

    clrscr();

    printf("Input the student number of the student : ");
    scanf("%s",stdNum);

    trail=head;

    /* This do-while will find the desired record - TO BE CREATED IN A NEW SUBROUTINE. */
    do
    {
        if( strcmp( trail->studNumber,stdNum )==0 )
        {
            foundIt++;
        }else{
            trail=trail->sp;
        }
    }while( foundIt==0 && trail!=NULL );

    if( foundIt!=0 )
    {
        printf("\n\nStudent %s's records are as follow:\n\n%s , %s , %s , %.1f",stdNum,trail->studNumber,trail->lastName,trail->firstName,trail->finalGrade);
    }else{
        printf("\n\nSuch student number does not exist.");
    }
}

/* This subroutine will let the user input the student number and the user will update the existing records EXCEPT student number. */
struct stud* updateRec( struct stud *head )
{
    int foundIt=0;
    struct stud *trail;
    char stdNum[12];

    clrscr();

    printf("Input the student number of the student : ");
    scanf("%s",stdNum);

    trail=head;

    /* This do-while will find the desired record - TO BE CREATED IN A NEW SUBROUTINE. */
    do
    {
        if( strcmp( trail->studNumber,stdNum )==0 )
        {
            foundIt++;
        }else{
                trail=trail->sp;
        }
    }while( foundIt==0 && trail!=NULL );

    if( foundIt!=0 )
    {
        printf("\nEnter the new student's last name : ");
        scanf("%s",trail->lastName);
        printf("\nEnter the new student's first name : ");
        scanf("%s",trail->firstName);
        printf("\nEnter the new student's final grade : ");
        scanf("%f",&trail->finalGrade);
        printf("\n\nThe new student %s's records are as follow:\n\n%s , %s , %s , %.2f",stdNum,trail->studNumber,trail->lastName,trail->firstName,trail->finalGrade);
        printf("\n\nRecords updated!");
    }else{
        printf("\n\nSuch student number does not exist.");
    }

    return head;
}

/* This subroutine will mark a record for deletion. */
struct stud* deleteRec( struct stud *head )
{
    int foundIt=0;
    struct stud *trail;
    char stdNum[12];

    clrscr();

    printf("Input the student number of the student's record for deletion : ");
    scanf("%s",stdNum);

    trail=head;

    /* This do-while will find the desired record - TO BE CREATED IN A NEW SUBROUTINE. */
    do
    {
        if(strcmp(trail->studNumber,stdNum)==0)
        {
            foundIt++;
        }else{
            trail=trail->sp;
        }
    }while( foundIt==0 && trail!=NULL );

    if( foundIt!=0 )
    {
        trail->studNumber[0]='*';
        printf("\n\nDeletion mark placed! %s",trail->studNumber);
    }else{
        printf("\n\nSuch student number does not exist.");
    }   

    return head;
}

/* All the records EXCEPT the ones with deletion marks will be saved in a text file named CLASSLIST.TXT in this subroutine. */
struct stud* saveRecords( struct stud *head )
{
    FILE *cl;
    struct stud *trail;

    trail=head;

    clrscr();

    /* CLASS = file name, wt = write only text file */
    cl=fopen( "C:\\CLASSLIST.txt" , "w" );

    do
    {
        if(trail->studNumber[0]!='*')
        {
            fprintf( cl , "%s , %s , %s , %.1f\n" , trail->studNumber, trail->lastName, trail->firstName, trail->finalGrade );
            printf("Student %s's records are now written on CLASSLIST.txt\n",trail->studNumber);
        }

        trail=trail->sp;
    }while( trail!=NULL );

    fclose(cl);
}

/* The main subroutine */
main()
{
    struct stud *head=malloc( sizeof(struct stud) );
    int choice,recordCounter=1,firstInputCheck=0;

    do
    {
        choice=showMenu();

        switch( choice )
        {
            case 1: head=createRec( head,recordCounter,firstInputCheck );
                    break;
            case 2: showRec( head );
                    break;      
            case 3: head=updateRec( head );
                    break;
            case 4: head=deleteRec( head );
                    break;
            case 5: saveRecords( head );
                    break;

            default: printf("\n\nProgram terminated.");
        }

        printf("\n\nPress any key to continue.");
        getch();

    }while( choice!=6 );
}       

这是我的代码,它工作正常,只是我在 C: 中看不到新创建的 CLASSLIST.txt。

最佳答案

打开此处后:

cl=fopen( "C:\\CLASSLIST.txt" , "w" );

确保您的指针不为空(这意味着文件实际上已打开用于写入):

if (cl == NULL) {
    // some error has occurred
    // i.e. you don't have WRITE permission on C:\
} else {
    // do write into file
}

关于使用 fopen 创建文件,但文件未出现在驱动器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14538027/

相关文章:

haskell - 是否建议以尾递归形式使用递归IO操作?

java - 无法在新行中写入字符串

C:用常量子结构初始化结构

c - 在链表中插入节点

在不同的体系结构上转换指针类型

c++ - 在 fscanf 中直接将 char* 转换为 std::string

c - 用于缩短 C 中结构成员引用的宏

c - malloc 之前的 (int *) 是什么意思?

mysql - 如何更改mysql表的物理顺序?

java - 如何将文本文件中加载的值与 Jtextfield 中新输入的值相加