关于读取文件的问题

yeyingshu 2002-05-11 12:59:53
有一个文本文件,其中每行包括一个号码和一个名字(名字长短不一),请问我该怎么把文件打开读出,并把前面的号码存到数组中?
...全文
24 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeyingshu 2002-05-12
  • 打赏
  • 举报
回复
to ma2jun() :果然高明,谢谢你,问题得到解决。
mechiland 2002-05-11
  • 打赏
  • 举报
回复
to ma2jun() ,
高手,有所得!
ma2jun 2002-05-11
  • 打赏
  • 举报
回复
//
// MFC style
//
CStdioFile txtFile;
CString szLine = "";
CStringArray arrIndex;
TRY
{
txtFile.Open( "c:\\1.txt", CFile::modeRead|CFile::typeText );
while( txtFile.ReadString( szLine ) )
{
int iFirstBlank = szLine.FindOneOf( " " );
arrIndex.Add( szLine.Left( iFirstBlank + 1 ) );
}

}
CATCH( CFileException, e )
{
e->ReportError();
}
END_CATCH

for( int i=0; i< arrIndex.GetSize(); i++ )
TRACE0( arrIndex[i] );
ma2jun 2002-05-11
  • 打赏
  • 举报
回复
// 假设文件格式如下:
// 1234 abcd
// 2345 bcde
// .... ....

//
// "C"
//
#include <stdio.h>
#include <string.h>
#include <conio.h>


int main(int argc, char* argv[])
{
// open it as text mode
FILE* pFile = fopen( "c:\\1.txt", "rt" );
char c; // every read char buffer
char word[255]; // every temp reading buffer, like "1234",or "abcd"
int i=0;
bool bIndex = true;
while( fread( &c, sizeof( char ), 1, pFile ) )
{
if( c >= '0' && c <= '9' && bIndex ) // the index string
*( word + i++ ) = c;
if( c == ' ' && bIndex ) { // the blank
// do something save the word(index) to an array
*( word + i ) = '\0';
_cputs( word );
_cputs( "\r\n" );

i = 0;
bIndex = false;
}
if( !bIndex ) // the string
*( word + i++ ) = c;
if( c == 10 ) { // carrage return
i = 0;
bIndex = true;
}
}
fclose( pFile );

return 0;
}
qiuanhong 2002-05-11
  • 打赏
  • 举报
回复
你这样做比较难吧

要达到你的这种功能,看看这种方法,2分钟搞定

只要你把号码放一行、名字放一行 。(注意:读、或写都严格按这种格式)

123456
aa
123546
bb

然后用CStdioFile 的ReadString()函数读出(这函数据每次只读一行)

问题不大吧.


这是我昨天写的:

BOOL CHistoryList::FileContentToList()
{//从文本文件中导入数据显示到列表中
CString str;
UINT nRecord;
UINT nRow;
UINT nCol;

nRecord=0;
nRow=0;
nCol=0;
m_pFile.SeekToBegin();
while(m_pFile.ReadString(str))
{
str.TrimLeft();
if (str.Left(1)!='#')
{
if ((nRecord%6==0)&&(nRecord!=0))
{
nRow++;
nCol=0;
}
if (nCol==0)
m_pList->InsertItem(nRow,str,0);
else
m_pList->SetItemText(nRow,nCol,str);
nCol++;
nRecord++;
}
}

CButton* m_btnClear=(CButton*)GetDlgItem(IDC_CLEAR);
if (m_pList->GetItemCount())
{
m_btnClear->EnableWindow(true);
}
else
{
m_btnClear->EnableWindow(false);
}
m_pList->Arrange(LVA_DEFAULT);

return true;
}
yeyingshu 2002-05-11
  • 打赏
  • 举报
回复
TOStephen_Ma(追日):
谢谢你,可是我想把前面的号码也用字符串数组来存取,请问用Cfile类能做到吗?
akiy 2002-05-11
  • 打赏
  • 举报
回复
这样行么,文本文件不是由字符组成的么
是不是需要把每个字符读出来然后识别成数字和名字呢??
Stephen_Ma 2002-05-11
  • 打赏
  • 举报
回复
读出一行--->pBuf

int iNum;
char sName[NameLen];
sscanf(pBuf, "%d %s", &iNum, sName);

将iNum存入数组,后直接在上面把iNum替换掉。

16,548

社区成员

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

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

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