为什么此代码跳过第一个字符并在文件末尾打印一个特殊字符 [英] Why is this code skipping the first character and printing a special character at the end of the file

查看:240
本文介绍了为什么此代码跳过第一个字符并在文件末尾打印一个特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

上面的代码基本上通过将行值放入delete_line来跳过了我想要的数据行. 在这里,temp初始化为1.现在的问题是,此代码正在跳过第一个字符,即本例中的"A",并将特殊字符"putting"放在文件末尾. 例如,delete_line = 3

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
ÿ

此外,如果delete_line设置为1,则会跳过文件中的整行,例如:

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


Vinayak Sharma
Dev Deol
Ameesh Deol
ÿ

即使delete_line初始化为1,也请告诉我是否有一种方法可以从文件的第一行进行写操作.

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.

推荐答案

您的代码正在跳过第一个字符,因为您已经在调用getc()来读取第一个字母之后再次调用它.除了使用第一个字符来决定是否进入循环外,您没有对第一个字符进行任何操作,也没有在打印它.

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.

您需要将对getc()的第二个调用移到循环主体的底部,而不是顶部:

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
}

对于打印出ÿ的代码,这也是由于您第二次调用getc()的位置错误.

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

ÿ的数值为0xFF,当将其视为char时,该值与EOF相同.在已经打印了ch而不管它的值之后,直到下一次循环迭代时,才检查第二次调用getc()的返回值.

ÿ 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.

您的循环应更像这样:

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);
    }
}

至于额外的换行符,这是因为您正在打印属于已删除"行的'\n'字符.当遇到'\n'字符时,首先递增temp,然后求值if (temp != delete_line)以调用putc().当temp等于delete_line时,跳过putc(),但是当到达delete_line'\n'字符时,首先递增temp,使if (temp != delete_line)的值为true,所以 '\n'字符.您需要颠倒这种逻辑.

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++;
}

这篇关于为什么此代码跳过第一个字符并在文件末尾打印一个特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆