文件处理中的EOF标记问题 [英] EOF Marker Problem in Files Handling

查看:89
本文介绍了文件处理中的EOF标记问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的下面的代码中,我展示了fseek& ftell函数。

说我输入ABCD然后输入EOF !!

输出应为4;相反它显示6.

此外,EOF不会被接受与ABCD在同一行(行)中,如果我移动到下一行,它只被接受。



例如。

说,

我输入,

ABCDE

EOF

它应该将字符数读为5.但它正在读为7.

5 + 1(下一行) = 6

但是编译器返回7作为结果。



//无法理解为什么!!

//如果你能告诉我为什么EOF与INPUT(在这种情况下是ABCDE)在同一行中被接受,我真的很感激。



****************************** *



<前郎=cs> / * 使用ftell& fseek * /

#include< stdio.h>
#include< conio.h>

int main()
{
FILE * fp;
long n;
char c;

fp = fopen( RANDOM w);

while ((c = getchar())!= EOF)
putc(c,fp);

printf( 输入字符数=%ld,ftell( FP));

fclose(fp);

fp = fopen( RANDOM r);

n = 0L;

while (feof(fp)== 0
{
fseek(fp,n, 0 ); / * 第(n + 1)个字符的位置* /
printf( \ n%c的位置是%ld,getc(fp),ftell(fp));
n = n + 5L;
}

putchar(' \ n');

fseek(fp,-1L, 2 ); / * 最后一个角色的位置* /

do
{
putchar(getc(fp));
}
while (fseek(fp,-2L, 1 ));

fclose(fp);

return 0 ;
}

解决方案

如果按字符串末尾的ENTER键,系统将添加 \\ n (回车和换行)。并且EOF始终是键盘输入的单独行,因为只有在自己输入时才能识别。


In my following code I am demonstrating the fseek & the ftell functions.
Say I input ABCD and then the EOF !!
The output should be 4; instead it is showing 6.
Moreover the EOF is NOT being accepted in the same line(row) as ABCD, it is ONLY being accepted if I move to the next line(s).

For eg.
Say,
I input,
ABCDE
"EOF"
It should read the number of characters as 5. But it is reading it as 7.
5 + 1(for the next line) = 6
But the compiler is returning 7 as the result.

// Unable to Understand Why !!
// Also I would really appreciate if you could tell me why the EOF is not being accepted in the same line as the INPUT(in this case ABCDE).

******************************

/* WAP that uses ftell & fseek */

#include<stdio.h>
#include<conio.h>

int main()
{
    FILE *fp;
    long n;
    char c;

    fp=fopen("RANDOM","w");

    while((c=getchar())!=EOF)
        putc(c,fp);

    printf("No of Characters Entered = %ld",ftell(fp));

    fclose(fp);

    fp=fopen("RANDOM","r");

    n=0L;

    while(feof(fp)==0)
    {
        fseek(fp,n,0);          /* Position to (n+1)th character */
        printf("\nPosition of %c is %ld",getc(fp),ftell(fp));
        n=n+5L;
    }

    putchar('\n');

    fseek(fp,-1L,2);            /* Position to the Last Character */

    do
    {
        putchar(getc(fp));
    }
    while(fseek(fp,-2L,1));

    fclose(fp);

return 0;
}

解决方案

If you press the ENTER key at the end of your string then the system will add \r\n (Carriage return and Line feed). And the EOF is always a separate line on input from the keyboard, as it is only recognised when entered on its own.


这篇关于文件处理中的EOF标记问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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