0

I am trying to figure out how to remove a specific line from my simple Database. Result of running this will just leave the Origin file empty instead of one line less After endless of failure i now ask for help.

My code.

void Remove_name(FILE *Array, FILE *PointTemp) {
char Tname[40]; /// string to store name
long int recsize; /// size of each record
recsize = sizeof(A);
Array = fopen ( "Orig.txt", "r" );
                printf("\nEnter name to delete: ");
                scanf("%s", Tname);
                PointTemp = fopen("TEMP.txt","w");  /// create a intermediate file for temporary storage
                rewind(Array); /// move record to starting of file
                while(fread(&A,recsize,1,Array) == 1){ /// read all records from file
                    if(strcmp(A.firstname, Tname) != 0){ /// if the entered record match
                        fwrite(&A,recsize,1,PointTemp); /// move all records except the one that is to be deleted to temp file
                    }
                }
                fclose(PointTemp);
                fclose(Array);
                remove("Orig.txt"); /// remove the orginal file
                rename("TEMP.txt", "Orig.txt"); /// rename the temp file to original file name
            }

I made a attempt before but ended up in utter failure aswell. Will post code of pre attemp.

void Remove_name(FILE Array[Size], FILE Temp[Size]) {
char temp;
char delete_string[Size];
Array = fopen ("Orig.txt", "r" ); //Open real file for READ
temp = getc(Array);
while (temp != EOF){
    printf ("%c", temp);
    temp = getc(Array);
}
printf("\nEnter name to delete: ");
    Temp = fopen("Taco.txt","w");  /// create a intermediate file for temporary storage
    //temp = getc(Array);
    scanf("%s", delete_string);
    //temp = getc(Array);
    while (temp != EOF) {
    temp = getc(Array);
    if(strcmp(A.firstname,delete_string) != 0){ /// if the entered record match
    putc(A.firstname,Temp); /// move all records except the one that is to be deleted to temp file
    }
}
    fclose(Array);
    fclose(Temp);
    remove("Orig.txt"); /// remove the orginal file
    rename("Taco.txt","Orig.txt"); /// rename the temp file to original file name
    printf("\nThe contents of file after being modified are as follows\n");
Array = fopen("Orig.txt", "r");
temp = fgetc(Array);
while (temp != EOF){
    printf ("%c", temp);
    temp = fgetc(Array);
}
fclose(Array);
maazza
  • 7,016
  • 15
  • 63
  • 96
TazE
  • 1

0 Answers0