0
#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。

4

3 回答 3

1

在这里打开后:

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
}
于 2013-01-26T15:08:56.363 回答
0

即使您确实说cl = fopen("C:\\CLASSLIST.txt", "w");并且没有访问 C 驱动器的权限,也不会创建该文件。

为此,请确保您有权访问 C 盘(检查是否禁用了 UAC),或以管理员身份运行可执行文件(对于 windows vista、7 和 8)。

或者,您可以将文件保存在您有权写入的文件夹中,例如您的Documents文件夹中。

高温高压

于 2013-01-26T15:13:00.950 回答
0

我设法编译并运行了您的程序(在进行了一些更改以使其更符合 C99 之后),然后CLASSLIST.txt在当前目录中成功创建。

尝试为您的文件指定不同的位置或确保您具有写入C:\根目录的权限。

于 2013-01-26T15:15:44.890 回答