0

Case 2 is the part that has a problem. At the end of it, it's supposed to delete the file "List.txt" and rename the file "newlist.txt" to "List.txt" but neither of these happen and both functions return -1, so both files remain in the directory after the execution.

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

int main() 
{
    char menu, firstName[20], lastName[30], stdNum[10], avgInt[3], avgDec[3];
    do
    {
        FILE *listPtr;
        printf("Menu: \n1 : Add a new student\n2 : Remove a student\n3 : Edit a student\n4 : Search a student\n5 : Sort the students\n6 : Exit\n\n");   
        menu = getch();
        if(menu != 6)
            switch(menu)
            {
                case '1':
                {
                    listPtr = fopen("List.txt", "a");

                    //first name
                    printf("Enter the first name:\n");
                    scanf("%19s", firstName);
                    fprintf(listPtr, firstName);
                    fputc(' ', listPtr);

                    //last name
                    printf("\nEnter the last name:\n");
                    scanf("%29s", lastName);
                    fprintf(listPtr, lastName);
                    fputc(' ', listPtr);

                    //student number
                    printf("\nEnter the student number:\n");
                    scanf("%9s", stdNum);
                    fprintf(listPtr, stdNum);
                    fputc(' ', listPtr);

                    //average
                    printf("\nEnter the student's average score:\n");
                    printf("Integer part:");
                    scanf("%s", avgInt);
                    fprintf(listPtr, avgInt);
                    fputc(' ', listPtr);
                    printf("Decimal part:");
                    scanf("%s", avgDec);
                    fprintf(listPtr, avgDec);
                    fputc('\n', listPtr);
                    fclose(listPtr);
                    break;
                }
                case '2':
                {
                    listPtr = fopen ("List.txt", "r");
                    if(feof(listPtr))
                        printf("You haven't saved any students yet.");
                    else
                    {
                        int found = 0;
                        listPtr = fopen("list.txt", "r");
                        printf("Enter the student's student number:");
                        char tempNum[10];
                        scanf("%s", tempNum);
                        do
                        {
                            fscanf(listPtr, "%s %s %s %s %s", firstName, lastName, stdNum,     avgInt,avgDec);
                        if(strcmp(tempNum, stdNum) == 0)
                        {
                            found = 1;
                            break;  
                        }
                        } while(strcmp(tempNum, stdNum) != 0 && !feof(listPtr));
                        if(found == 0)
                            printf("This student doesn't exist in the list.");
                        else
                        {
                            rewind(listPtr);
                            FILE *tempList;
                            tempList = fopen("newlist.txt", "w");
                            while(!feof(listPtr))
                            {
                                fscanf(listPtr, "%s %s %s %s %s", firstName, lastName, stdNum,     avgInt, avgDec); 
                                if(strcmp(tempNum, stdNum) != 0 && !feof(listPtr))
                                    fprintf(tempList, "%s %s %s %s %s\n", firstName, lastName, stdNum, avgInt, avgDec);
                            }
                            fclose(listPtr);
                            fclose(tempList);
                            remove("List.txt");
                            rename("newlist.txt", "List.txt");
                        }
                    }
                    break;
                }
                case '3':

                    break;
                case '4':

                    break;
                case '5':   

                    break;
            }
    } while(menu != '6');
}    
  • My remove and rename won't work too, use system commands or syscall instead. – Zach P Jan 04 '15 at 11:20
  • 1
    Try checking the return values from remove and rename and if they are -1 then check errno to see what the error is. – John Sheridan Jan 04 '15 at 11:20
  • Why shouldn't they work? Everything seems fine to me. – a.sadeghi Jan 04 '15 at 11:21
  • @JohnSheridan How do I use that errno? – a.sadeghi Jan 04 '15 at 11:21
  • Use the manual. Try man -S2 rename and man remove. – John Sheridan Jan 04 '15 at 11:25
  • 1) you are opening `listPtr = fopen ("List.txt", "r");` twice. 2) you should avoid `feof()` 3) You can put code into separate functions, instead of cramming everything in main. 4) `fscanf()` has a return value: us it! (just like rename and remove) – wildplasser Jan 04 '15 at 11:31
  • It says "Permission denied" and "File exists" for remove and rename respectively. The second one is actually expected due to the first one. But why does the first one happen? Is it because I'm on drive C? – a.sadeghi Jan 04 '15 at 11:32
  • @wildplasser I'm not openning it twice! There are in different cases. And about fscanf, I'll learn it later... But for now this problem is the priority. – a.sadeghi Jan 04 '15 at 11:35
  • There are only 5 lines between them, but they are still both in `case '2'` . – wildplasser Jan 04 '15 at 11:42
  • IIRC, DOS/Windows refuses to move/rename/unlink a file that is still open. Your program opened it twice (and leaked one file descriptor/file handle) – wildplasser Jan 04 '15 at 11:51
  • `if(feof(listPtr))` is used wrong, check return value of `fscanf(listPtr, ...` instead. At it stands now, code after that is UB. – chux - Reinstate Monica Jan 04 '15 at 23:19

0 Answers0