C++的win32里面如何从文件中读取换行符?

来源:百度知道 编辑:UC知道 时间:2024/05/22 15:33:04
我想读取一串字符串,中间换个行。直接添加“\n”是不行的,或者回车换行也是不行的,请问合理的方法。
比如:
文档:abc.txt
文档内容:“今天上山打老虎”
目的:在WIN32程序中读取文档并能输出“今天上山
打老虎”

按照你问题补充写了一个,在vc上调试通常。
#include <stdio.h>
#include <malloc.h>
#include <process.h>
main()
{
int i,flen;
char temp[100];
FILE *fp;
if((fp=fopen("d:\\kc1.txt","r"))==NULL)
{
printf("can not open\n");
exit(0);
}
fseek(fp,0,SEEK_END);
flen=ftell(fp); //文件长度
fseek(fp,0,SEEK_SET);
printf("%d\n",flen);
for(i=0;i<flen;i++)
{
fscanf(fp,"%c",&temp[i]);
printf("%c",temp[i]);
if((i+1)%8==0) //每显示4个中文换行
printf("\n");
}
fclose(fp);
}

完全不理解你要做什么。 你是要读一个两行的字符串,还是要在读进来的字符串中间插个换行?

直接添加\n为什么不行?

istream& get ( char& c );
istream& get ( char* s, streamsize n );
istream& get ( char* s, streamsize n, char delim );
istream& get ( streambuf& sb);
istream& get ( streambuf&