0
ch = getc(lname);
while (ch != EOF)
{
    ch = getc(lname);
    if (ch == '\n')
        temp++;
    //except the line to be deleted
    if (temp != delete_line)
    {
        //copy all lines in file replica.c
        putc(ch, rep);
    }
}

I have a file in which I have the following data

Aryan Verma
Vinayak Sharma
Dev Deol
Ameesh Deol

the above code basically skips the line of data that I want to by putting the line value in delete_line. Here, temp is initiated to be 1. Now the problem is, this code is skipping the first char, i.e. "A" in this case and putting a special char "ÿ" at the end of the file. for eg, delete_line=3

ryan Verma
Vinayak Sharma
Ameesh Deol
ÿ

Also, if the delete_line is intialised to 1, it skips the whole line in file, like:


Vinayak Sharma
Dev Deol
Ameesh Deol
ÿ

Please let me know if there is a way to write from the first line of file even though delete_line is initialized to 1.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 3
    You call `getc` outside the loop and then immediately throw that away with a `getc` inside the loop. That is the cause of the missing first character. – kaylum Jun 08 '20 at 07:00
  • 2
    You check for `EOF` **after** writing the character to file. That is the cause of the invalid last character. – kaylum Jun 08 '20 at 07:00
  • 2
    is `ch` an `int`? If it isn't, the comparison `ch != EOF` does not make sense. – pmg Jun 08 '20 at 07:01
  • Also `EOF` is not a character: it's a signal. You're not supposed to read more characters after you've been sent the `EOF` signal – pmg Jun 08 '20 at 07:04
  • Please provide a [mcve] as foundation for solution code proposals. – Yunnosch Jun 08 '20 at 07:04
  • @kaylum oh yes! I didn't notice it. Thank You! But what about the first line being skipped if I initialize delete_line=1. It creates a blank line in the file at the top. –  Jun 08 '20 at 07:05
  • Does this answer your question? [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Fantastic Mr Fox Jun 08 '20 at 07:05
  • Also c/c++ is not a language, pick one of them, because the answer is different for the 2 languages. – Fantastic Mr Fox Jun 08 '20 at 07:06
  • @FantasticMrFox that other question has nothing to do with this issue – Remy Lebeau Jun 08 '20 at 07:10

1 Answers1

2

Your code is skipping the first character because you are calling getc() again after it has already been called to read the 1st letter. You are not doing anything with the first character other than using it to decide whether to enter the loop or not, you are not printing it.

You need to move that 2nd call to getc() down to the bottom of the loop body, rather than be at the top:

ch = getc(lname);
while (ch != EOF)
{
    // ch = getc(lname); <-- move this...
    if (ch == '\n')
    ... 
    ch = getc(lname); // <-- ... down here instead
}

As for the code printing out ÿ, that is also due to your 2nd call to getc() being in the wrong place.

ÿ has a numeric value of 0xFF, which is the same value as EOF when it is treated as a char. You are not checking the return value of the 2nd call to getc() until the next loop iteration, after you have already printed ch regardless of its value.

Your loop should look more like this:

ch = getc(lname);
while (ch != EOF)
{
    if (ch == '\n')
        temp++;
    //except the line to be deleted
    if (temp != delete_line)
    {
        //copy all lines in file replica.c
        putc(ch, rep);
    }
    ch = getc(lname);
}

Alternatively, it can be rewritten like this:

while ((ch = getc(lname)) != EOF)
{
    if (ch == '\n')
        temp++;
    //except the line to be deleted
    if (temp != delete_line)
    {
        //copy all lines in file replica.c
        putc(ch, rep);
    }
}

As for the extra line break, that is because you are printing the '\n' character that belongs to the "deleted" line. When you encounter a '\n' character, you increment temp first, and then evaluate if (temp != delete_line) to call putc(). When temp is equal to delete_line, you skip putc(), but when you reach the '\n' character of delete_line, you increment temp first, making if (temp != delete_line) evaluate as true, so you putc() the '\n' character. You need to reverse this logic.

Your final loop code should look more like this instead:

while ((ch = getc(lname)) != EOF)
{
    // copy all lines in file replica.c
    // except the line to be deleted
    if (temp != delete_line)
    {
        putc(ch, rep);
    }
    if (ch == '\n')
        temp++;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • This does fix the skipping first char and the special character, but it doesnt fix the blank line in the file after delete_line is initialized to 1 –  Jun 08 '20 at 07:09
  • @aaks-ctrl that is because you are printing the `'\n'` character of the "deleted" line instead of skipping it. I have updated my answer – Remy Lebeau Jun 08 '20 at 07:20