vc 读取txt文件问题??

icu 2004-04-06 02:37:41
vc中读取一个txt文件,每次读取一段末尾为tab键的字符串,该如何实现???
...全文
92 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
at_yuan2 2004-04-06
  • 打赏
  • 举报
回复
////////////////////////////////////////////////////////////////

char s[255];
FILE *stream;
stream = fopen("c:\\YDQ.txt", "r");
if (stream != NULL)
{
while (!feof(stream))
{
fscanf( stream, "%s\t", s );
AfxMessageBox(s);
}
fclose(stream);
}
ymbymb 2004-04-06
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
using namespace std;

ifstream ifile("temp.txt");
char linebuf[512];
while(!ifile.eof())
{
ifile.get(linebuf, sizeof(linebuf), '\t');
ifile.get(); //跳过'\t';
}
ifile.close();
zhangnanonnet 2004-04-06
  • 打赏
  • 举报
回复
读一个字符判断一次最好
byry 2004-04-06
  • 打赏
  • 举报
回复
CFile file;
CFileException e;

if(!file.Open(fileName, CFile::modeRead, &e ) )
{
AfxMessageBox("Can't find index.ini!");
return;
}


char buf = '\0';
CString str;
str.Empty();

file.Read(&buf, 1);
for(int i=0; i<file.GetLength(); i++)
{
if(buf != '\t')
{
CString temp;
temp = buf;
str =str + temp;
temp.Empty();
}
else
{
int j = 0;
str = str + '\t';
//此时已经得到末尾为TAB键的一个字符串
}
file.Read(&buf, 1);
}


file.Close();

这个方法有点笨,而且没有考虑换行
fzd999 2004-04-06
  • 打赏
  • 举报
回复
最好对缓冲区进行一个限制,或者用'\0'来替换'\t'
fzd999 2004-04-06
  • 打赏
  • 举报
回复
楼上的,如果文件很大怎么办?要知道CString的上限是65536个字符。

简单的办法就是双缓冲,用两个char Buf[??]配合,寻找'\t',然后用指针进行截取
快乐鹦鹉 2004-04-06
  • 打赏
  • 举报
回复
CFile file;
file.Open("C:\\aa.txt",CFile::modeRead);
CString str;
file.ReadHuge(str.GetBuffer(),file.GetLength());
int index = str.Find('\t');
while(index != -1)
{
CString subStr = str.Left(index);//subStr就是截取的一段字符串
str = str.Mid(index+1);
index = str.Find('t');
}
if(str.GetLength() > 0)
{
//剩余的str是最后一段字符串
}
str.ReleaseBuffer();
快乐鹦鹉 2004-04-06
  • 打赏
  • 举报
回复
先将文件的所有内容读到CString中。然后用Find函数查找\t后,用Left函数进行截取。

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧