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
  • 6,646
  • 15
  • 58
  • 94
TazE
  • 1
  • 3
    "utter failure": Please explain what this means. – Scott Hunter Dec 19 '16 at 18:12
  • 1
    Can the database only have one word per line? `%s` will only give you all the characters up until a space or a new line. For example, if you wanted to delete 'this is a whole line', your code would only act on 'this'. – AndyW Dec 19 '16 at 18:19
  • 3
    show definition of A, plus sample data (first few lines of the file) – pm100 Dec 19 '16 at 18:41
  • 1
    Of topic: Don't use `scanf("%s", Tname);`. Use `fgets` or at least `scanf("%39s", Tname);` – Support Ukraine Dec 19 '16 at 18:50
  • `putc(A.firstname,Temp);` writes one character. Was this intended? – chux - Reinstate Monica Dec 19 '16 at 19:18
  • Thanks for answers! I have tried so many ways, but "putc" was the last attempt. – TazE Dec 19 '16 at 21:45
  • #include #include #include #include #define Size 100 #define FILENAME "TEST.txt" struct { char carbrand[Size]; char cartype[Size]; char firstname[Size]; char lastname[Size]; int age; }A; – TazE Dec 19 '16 at 21:48

0 Answers0