文件编排问题 [英] file progamming problem

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

问题描述

在我的程序中,我需要存储与某个ASCII值相对应的字符,然后从文件中检索该字符并打印其ASCII值.

该程序的代码在下面给出

In my program I need to store the character corresponding to some ASCII value and then retrieved the character from the file and print its ASCII value.

The code for this program is given below

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

void main()
{
int ch;
FILE *fptr;
clrscr();
fptr=fopen("newtest1.txt","w++");
for(ch=0;ch<256;ch++)
fprintf(fptr,"%c",ch);
fclose(fptr);
fptr=fopen("newtest1.txt","r++");
for(;;)
{
ch=fgetc(fptr);
if(ch==EOF)
break;
printf("%d\t",ch);
}
getch();
}





但是问题是它只给出输出1到25.在那之后,它退出循环并且不打印其余部分.我使用TurboC.
该程序在Linux环境中运行.但是我必须在Turbo C中执行该程序.如何读取26值?请帮忙.





but the problem is that it only gives output 1 to 25. after that it gets out of the loop and does not print the rest.I ''m using turbo C.
this program run goo in Linux environment.But I have to do the program in Turbo C.how can I read the value 26? please help.

推荐答案

为什么不以二进制模式打开文件?
:)
Why don''t you open the file in binary mode?
:)


在文本模式下,值26(0x1A或ASCII CTRL-Z)被解释为文件结尾,而fgetc()返回EOF代码.
在二进制模式下,fgetc()不会解释读取的值,您可以读取到文件的实际结尾.

最好使用if( 0 != feof(fptr))作为中断条件来结束读取循环.
In text mode, the value 26 (0x1A or ASCII CTRL-Z) is interpreted as end of file and fgetc() returns the EOF code.
In binary mode fgetc() doesn''t interpret the readed value and you can read to the real end of file.

You better use if( 0 != feof(fptr)) as break condition to end the reading loop.


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

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