声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 7023|回复: 9

[C/C++] 如何在VC++中读取txt文件数据存到多个一维数组中

[复制链接]
发表于 2011-5-7 13:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
在VC++6.0下创建MFC AppWizard[exe] 基于基本对话框的工程TxtArray 。
我用下面的代码能获取txt文件的路径,但是不知把txt中每一列数据存到一个一维数组中,
txt中数据格式为3列数据:
1 1 2
2 1 3
3 1 4
1 2 3
2 2 4
3 2 5
1 3 4
........
怎样才能把这三列数据存到对应的三个一维数组中。
A[]={1,2,3,1,2,3,1......}
B[]={1,1,1,2,2,2,3........}
C[]={2,3,4,3,4,5,4........}

获取txt路径的代码如下:
  1. void CTxtArrayDlg ::OnButton1()
  2. {
  3.         // TODO: Add your control notification handler code here

  4.         char szFilters[] = "MyType Files (*.txt)|*.txt|All Files (*.*)|*.*||";
  5.        
  6.         CFileDialog fileDlg (TRUE, "txt", "*.txt",OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);
  7.        
  8.         // Display the file dialog. When user clicks OK, fileDlg.DoModal()
  9.         // returns IDOK.
  10.         if( fileDlg.DoModal()==IDOK )
  11.         {
  12.                 CString pathName = fileDlg.GetPathName();
  13.                
  14.                 m_FilePath = pathName;
  15.                
  16.                 UpdateData(FALSE);   
  17.         }
  18.        

  19. }
复制代码
回复
分享到:

使用道具 举报

发表于 2011-5-8 08:31 | 显示全部楼层
.
    不好意思,我一直编程是用FORTRAN,你的问题可以到计算板块问问,那里应该有了解的。。。
发表于 2011-5-8 10:14 | 显示全部楼层
本帖最后由 Rainyboy 于 2011-5-8 10:15 编辑
  1. #include <fstream>
  2. #include <string>
  3. #include <sstream>
  4. #include <iomanip>
  5. #include <iostream>
  6. using namespace std;

  7. int ReadDataFromFile(double * DataList[] ,int DataListCount,string &FileName)
  8. {
  9.         ifstream DataFile;
  10.         int CurrentDataIndex = 0;;
  11.         DataFile.open(FileName.c_str(),ios::in);
  12.         if(DataFile.is_open()==true)
  13.         {
  14.                 char buffer[200];
  15.                 while(DataFile.getline(buffer,200))
  16.                 {
  17.                         string strdata;
  18.                         stringstream ss(buffer);
  19.                         for(int i =0;i<DataListCount;++i)
  20.                         {
  21.                                 getline(ss,strdata,' ');
  22.                                 DataList[i][CurrentDataIndex] = strtod(strdata.c_str(),NULL);
  23.                         }
  24.                         ++CurrentDataIndex;
  25.                 }
  26.         }
  27.         return CurrentDataIndex;
  28. }


  29. int _tmain(int argc, _TCHAR* argv[])
  30. {
  31.         double a[100],b[100],c[100];
  32.         double* DataList[] = {a,b,c};
  33.         int DataCount = ReadDataFromFile(DataList,3,string("test.txt"));
  34.         for(int i=0;i<DataCount;++i)
  35.         {
  36.                 cout<<setw(10)<<a[i]<<setw(10)<<b[i]<<setw(10)<<c[i]<<endl;
  37.         }
  38.         system("pause");
  39.         return 0;
  40. }
复制代码
*************************
test.txt的内容:
*************************
1 1.1 1.11
2 2.2 2.22
3 3.3 3.33
4 4.4 4.44
5 5.5 5.55




************************
输出结果:
************************
1.jpg



评分

1

查看全部评分

发表于 2011-5-24 10:26 | 显示全部楼层
谢谢楼主,不过我一直想知道不用MFC应该怎样获取。。
发表于 2011-5-24 12:01 | 显示全部楼层
回复 4 # yu婧 的帖子

本来就没有用MFC。
 楼主| 发表于 2011-5-26 08:53 | 显示全部楼层
回复 5 # Rainyboy 的帖子

那用MFC该怎么获取?
发表于 2011-5-26 09:12 | 显示全部楼层
本帖最后由 ibrave 于 2011-5-26 09:16 编辑

回复 6 # sunminmin 的帖子

你可以先利用read函数把txt里面的字符读到一个buffer里面,
然后做一个循环;
for(i=0;i<size;i=i+3)

    coloum1[i/3]=atoi(buffer);
    coloum2[i/3]=atoi(buffer[i+1]);
    coloum3[i/3]=atoi(buffer[i+2]);


发表于 2011-5-26 13:45 | 显示全部楼层
回复 6 # sunminmin 的帖子

MFC是用C++封装的WINAPI,本质上是C++语言的一个库类,因此,我们在使用MFC时也可以使用标准C++库类进行操作。

所以,不存在“在MFC中怎么获取”这一类问题。
 楼主| 发表于 2011-5-29 22:29 | 显示全部楼层
回复 7 # ibrave 的帖子

能写个简单例子吗?
比如txt中:
1 2 3
4 5 6
7 8 9
把这三列写到三数组中。
发表于 2011-5-30 10:26 | 显示全部楼层
回复 9 # sunminmin 的帖子

CFile cfile;
char buffer[15];
UINT nBytesRead = cfile.Read( buffer, 15 );
for(i=0;i<16;i=i+5)

    coloum1[i/5]=atoi(buffer[i+0]);
    coloum2[i/5]=atoi(buffer[i+2]);
    coloum3[i/5]=atoi(buffer[i+4]);
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-4-30 01:35 , Processed in 0.079707 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表