1

It creates the temp file containing the required result. but its not removing the previous file "records.txt" and not renaming temp.txt. im using dev c++.

void delete()
{
    FILE *fp, *ft;
    int no;
    char ch;
    student stud;

    fp = fopen("Records.txt","r");
    printf("\nEnter student's RollNo to delete:");

    scanf("%d",&no);
    ft = fopen("temp.txt","w+");

    while (!feof(fp))
    {
        fscanf(fp, "\n%d\t%s\t", &stud.roll_no, stud.name);
        if (stud.roll_no != no)
        {
            fprintf(ft, "\n\t%d \t%s", stud.roll_no, stud.name);
        }
    }
    fclose(fp);
    fclose(ft);

    remove("Records.txt");
    rename("temp.txt","Records.txt");
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 3
    First problem [`while(!feof(fp))` is always wrong](http://stackoverflow.com/a/26557243/1983495). And you do absolutely no error check. Check every single possible error, and you will know why things didn't work, for example `remove()` returns `-1` on error, and sets `errno` so you can use `strerror()` and check what actually didn't work. – Iharob Al Asimi Apr 28 '15 at 14:05
  • Looks like a school assignment... please do also use the "debugger", so that you can step through your code line by line. Your current code is also somewhat "risky", in the worst case, you could lose your records.txt file. – Tilman Hausherr Apr 28 '15 at 14:16
  • Thank you. yeah ill try doing what you told me to. :) – Punya Nayak Apr 28 '15 at 14:32
  • 1
    @iharob To be picky, those functions return a _non-zero_ value if they fail. So don't check `if(remove("...") == -1)` but rather something like `int failed = remove("..."); if(failed) { ...` – Lundin Apr 28 '15 at 15:08

0 Answers0