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" );

ポインタがnullでないことを確認してください(ファイルが実際に書き込み用に開かれたことを意味します)。

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ます。

HTH

于 2013-01-26T15:13:00.950 に答える
0

私はなんとかあなたのプログラムをコンパイルして実行し(それをもう少しC99準拠にするためにいくつかの変更を行った後)、その後CLASSLIST.txt、現在のディレクトリに正常に作成されました。

C:\ファイルに別の場所を指定するか、 rootへの書き込み権限があることを確認してください。

于 2013-01-26T15:15:44.890 に答える