0

This code is supposed to skip a line of file and write everything else in a different file, delete the original one, and rename the different one to the one deleted. Whats wrong with this code is its not working after the first file ,i.e., the second file it is niether deleted nor a new file is created with the skipped line of file. what is the issue? Does it has to do something with the rename remove function?

FILE *lname
FILE *id
FILE *rep

lname = fopen("lname.txt", "r");
id = fopen("id.txt", "r");
rep = fopen("rep.txt", "w+");

char ch1,ch2;
int temp=1,delete_line=3; /*(delete_line is supposed to be taken as an input)*/

ch1 = getc(lname);

while (ch1 != EOF)
{
    if (ch1 == '\n')
        temp++;

    if(delete_line==1) {
        if (temp == 2 && ch1 == '\n')
            ch1 = getc(lname);
    }

    if (temp != delete_line)
        putc(ch1, rep);
​
    ch1 = getc(lname);
}

fclose(lname);
fclose(rep);

remove("lname.txt");
rename("rep.txt","lname.txt");

rep = fopen("rep.txt", "w+");
​
ch2 = getc(id);

while (ch2 != EOF)
{
    if (ch2 == '\n')
        temp++;
    //except the line to be deleted
    if (temp == 2 && ch2 == '\n') //making sure to skip a blank line if delete_line=1
        ch2 = getc(id);
​
    if (temp != delete_line)
        putc(ch2, rep);
​
    ch2 = getc(id);
}

fclose(id);
fclose(rep);

remove("id.txt");
rename("rep.txt","id.txt");

data in id.txt

asd123
xcv1323
rijr123
eieir2334

data in lname.txt

Bipul Das
Star Lord
Tony Stark
Vin Diesel
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • 2
    You probably want to reset `temp=1` after reading the first file. – 001 Jun 08 '20 at 15:22
  • 1
    https://stackoverflow.com/questions/62256572/why-is-this-code-skipping-the-first-character-and-printing-a-special-character-a concentrate on pmg comment. Also you deleted [this question](https://stackoverflow.com/questions/62261730/this-code-snippet-is-dtrgffffffffffffffffffffffff) which is almost exactly the same - please edit your question and undelete it next time. – KamilCuk Jun 08 '20 at 15:23
  • 2
    What type are `ch1` and `ch2`? They should be type `int`. – Ian Abbott Jun 08 '20 at 15:25
  • OT: The code for the `while` loops can be simplified a bit, e.g. `ch1 = getc(lname);` `while (ch1 != EOF)` `{` `if (temp != delete_line)` `putc(ch1, rep);` `if (ch1 == '\n')` `temp++;` `ch1 = getc(lname);` `}`. – Ian Abbott Jun 08 '20 at 15:30
  • @Ian Abbott, that leaves a blank line in the file if delete line is initiated as 1 –  Jun 08 '20 at 17:02
  • @aaks-ctrl No it doesn't. – Ian Abbott Jun 08 '20 at 17:11
  • regarding: ` rep = fopen("rep.txt", "w+");` 1) The `w` in the mode results in the file contents being truncated to nothing during the process of opening the file. 2) when calling `fopen()`, always check (!=NULL) the returned value to assure the operation was successful. – user3629249 Jun 09 '20 at 14:09
  • 1
    OT: regarding: `ch2 = getc(id); while (ch2 != EOF)` much better to combine those two lines into: `while( (ch2 = getc(id) ) != EOF ) ` and remove the call to `getc()` at the end of the loop – user3629249 Jun 09 '20 at 14:13

2 Answers2

1

The line

ch1 = getc(lname);

truncates the return value of getc from int to char. Therefore, the while loop condition

while (ch1 != EOF)

will always be true, because EOF cannot be represented in a char.

To fix this, you must declare ch1 to have the type int instead of char.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
0

regarding: Whats wrong with this code is its not working after the first file ,i.e., the second file it is niether deleted nor a new file is created with the skipped line of file. what is the issue? Does it has to do something with the rename remove function?

the original file: rep.txt is actually being deleted.

However, this call:

rep = fopen("rep.txt", "w+");

creates an empty file of the same name.

user3629249
  • 16,402
  • 1
  • 16
  • 17