怎样按行读txt文件 我的txt文件是一行一行的,每一行是一个词条,现在想把txt读进来,按行存储这些词条,应该怎么读?存储的时候用什么最好,字符数组?我是想用已知的词条去和txt文件中的词条一一进行比对,找相符的。这样的话读进来这些词条用什么存储好? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是用CStdioFile类吗?这个怎么用?谁能给我讲讲,我查的书上讲的不是很细,还是不会。 fgetline(fp, buffer, maxsize_buffer);每次从文件往buffer里读入maxsize_buffer个字符。你可以将maxsize_buffer设置成一行的大小。 CArchive CFile ReadString不过上面的方法只支持ANSI编码的txt文件。 不好意思,说错了,用CArchive的方法,如果是MBCS版的,就只支持ANSI编码的txt文件,如果是Unicode版,就只支持Unicode编码的txt文件。 void Read3dDataEachLine(const char* szFileName = "test.txt"){ if(szFileName == NULL) return; char FullPath[100] = {"\0"}; ::GetModuleFileName(NULL,FullPath,100); PathRemoveFileSpec(FullPath); string File = FullPath; File += "\\"; File += szFileName; FILE* pFile = fopen(File.c_str(),"r"); if (NULL == pFile) { cout<<"读文件出错!"<<endl; return; } char szLine[100] = {0}; char cData; int nIndex = 0; while (fread(&cData,1,1,pFile) > 0) { if (cData == '\n') { ProcessEachLine(szLine);//对每行进行处理 strcpy(szLine,"\0"); nIndex = 0; } else { szLine[nIndex] = cData; nIndex++; } }} #include <string>#include <iostream>#include <vector>#include <fstream>using namespace std;//read file vector<string> s; string strtmp; ifstream infile("123.txt"); while ( infile >> strtmp ) { s.push_back( strtmp ); }//findstring sc="11"; vector<string>::iterator it; for(it = s.begin(); it != s.end(); ++it) { string stmp = *it; int ifind = stmp.find(sc, 0); if(0 == ifind) cout << *it << '\n'; } void ReadFromFile(const CString& strFile){ if(strFile.IsEmpty()) { AfxMessageBox(_T("Invaild Path!")); return; } CStdioFile file; if(file.Open(strFile,CFile::modeRead|CFile::typeText ) ) { CString str = _T(""); while (file.ReadString(str)) { m_vector.push_back(str);//m_vector为vector<CString>容器. } }} msdn 的列子 内存中画图,打印的问题,快崩溃了,救命 CList 类如何初始化??? 调试vc时,总报告出现的问题 请教在ODBC中,如果应用SQL进行查询.查询之后,如何将结果显示在列表中? 真彩工具栏........有难题 提过让我一直疑惑的问题,关于WndProc函数 help!!!!OnLine Wait 双缓冲绘图中的一个奇怪问题 COM的简单问题。 我做了个基于MFC的系统,用到了树型控件,现在我在树型控中已经能够实现把对应数据库的用户表名显示在树的分支上,可是现在我想点击表的 记得哪个博客里面有封包与解包的文章
每次从文件往buffer里读入maxsize_buffer个字符。你可以将maxsize_buffer设置成一行的大小。
不过上面的方法只支持ANSI编码的txt文件。
{
if(szFileName == NULL)
return; char FullPath[100] = {"\0"};
::GetModuleFileName(NULL,FullPath,100);
PathRemoveFileSpec(FullPath);
string File = FullPath;
File += "\\";
File += szFileName; FILE* pFile = fopen(File.c_str(),"r");
if (NULL == pFile)
{
cout<<"读文件出错!"<<endl;
return;
} char szLine[100] = {0};
char cData;
int nIndex = 0; while (fread(&cData,1,1,pFile) > 0)
{
if (cData == '\n')
{
ProcessEachLine(szLine);//对每行进行处理 strcpy(szLine,"\0");
nIndex = 0;
}
else
{
szLine[nIndex] = cData;
nIndex++;
}
}
}
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;//read file
vector<string> s;
string strtmp;
ifstream infile("123.txt");
while ( infile >> strtmp )
{
s.push_back( strtmp );
}//find
string sc="11"; vector<string>::iterator it;
for(it = s.begin(); it != s.end(); ++it)
{
string stmp = *it;
int ifind = stmp.find(sc, 0);
if(0 == ifind)
cout << *it << '\n';
}
{
if(strFile.IsEmpty())
{
AfxMessageBox(_T("Invaild Path!"));
return;
}
CStdioFile file; if(file.Open(strFile,CFile::modeRead|CFile::typeText ) )
{
CString str = _T("");
while (file.ReadString(str))
{
m_vector.push_back(str);//m_vector为vector<CString>容器.
}
}
}