C++中如何导入TXT文件到二维数组

txt中是4乘800的数组,每一列都是不同变量,一行4个数互相关联

//需求有点不太明确,大概写了一些代码:
//=================================================
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define DESFILENAME    "d:\\test.txt"   // 文本文件名称
#define MAXROW 800             // 行数
#define MAXCOL 4               // 列数
#define MAXLENGTH 256             // 单行的最长字符数
#define DELIN '\n'            // 换行
#define SEPSTR " ,\t"          // 列与列之间的分隔符

// 把数字字符串转换成整形数保存
bool parseNum( char * strLine, int nResult[])
{
bool bResult = false;
// 函数调用出错返回
if(strLine == NULL || nResult == NULL)
{
return bResult;
}
char *strToken;
int nCount = 0;

// 把每行的字符串分割开,并转换成整形
strToken = strtok( strLine, SEPSTR );
while( strToken != NULL )
{
// 每行的列数超过指定范围,出错返回
if ( nCount >= MAXCOL)
{
return bResult;
}
// 字符串转换整形
nResult[nCount] = atoi( strToken );
nCount++;
// 取下一串数字
strToken = strtok( NULL, SEPSTR );
}
bResult = true;
return bResult;

}

void main()
{
char strTmp[MAXLENGTH];
        int nBuf[MAXROW][MAXCOL] = {0};
int nRow = 0, nCol = 0;
bool bRes;

// 打开文件
ifstream inStream( DESFILENAME,
ios::in||ios::nocreate,
filebuf::sh_read);
if( inStream.fail() )
{
printf( "文件打开失败!\n");
return;
}

do{
// 读取的行数越过了上限(800?),则跳出循环
if ( nRow >= MAXROW )
{
printf( "文件错误! 超出指定行数! 忽略后面的记录!\n");
break;
}
// 把buffer清0,准备下一次读取
memset(strTmp, 0, MAXLENGTH );
// 从文件中读一行文本
inStream.getline( strTmp, MAXLENGTH, DELIN );
// 解析每行文本并转换字符串为整形
bRes = parseNum( strTmp, nBuf[nRow] );
if ( bRes == false )
{
printf( "列项解析错误!\n");
inStream.close();
return;
}
nRow++;

}while( !inStream.eof() );    // 文件没到结尾继续读
// 关闭文件
inStream.close();

// 把读取并转换后的数组内容打印到屏幕
nRow = 0;
while( nRow < MAXROW)
{
for( nCol = 0; nCol < MAXCOL; nCol++ )
{
printf( "%d ", nBuf[nRow][nCol] );
}
printf( "\n" );
nRow++;
}

}

首先,不知道你指定的文件的具体格式,是用什么分隔的,每个数字的位数,这里都是假定的,可以根据你的实际需要修改代码
===============================
d:\\test.txt文件格式类似如下
123213 234324        234    38909   <------空格或TAB分隔的四个数字串
23 4 8 9
324        88 99         88        <----分隔符可以是一个或连续多个
34,,8,,,9    10                        <----可以是','逗号分隔
3    8    7                        <----可以只有三项
34        78                       <----可以只有两项
===============================
输出的效果,大概是:
123213 234324 234 38909
23 4 8 9
324 88 99 88
34 8 9 10
3 8 7 0
34 78 0 0
0 0 0 0
............
================================
修改MAXCOL为8,可以读取福采双色球的历史开奖记录(改MAXROW为20,测试用)
2011124                                   9  18 19 26 31 32 16 
2011125                                   3  10 15 24 27 32 8  
2011126                                   3  7  13 18 23 26 16 
2011127                                   16 19 22 23 27 29 11 
2011128                                   9  11 14 17 19 23 12 
2011129                                   7  10 11 21 23 26 6  
2011130                                   7  14 18 23 25 32 15
2011131                                   2  7  9  17 21 25 1  
2011132                                   2  5  12 13 25 33 7  
2011133                                   12 14 20 21 25 31 16
2011134                                   1  2  6  7  30 31 10
2011135                                   12 13 17 20 25 26 12
2011136                                   2  4  6  20 22 31 7  
2011137                                   2  11 18 23 30 33 13
2011138                                   1  5  15 24 28 32 7  
2011139                                   8  20 24 27 30 31 3

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-04-09

数据之间是什么分隔符?最好能有类似","或者空格之类的分割符,那么就很容易把数据转化为数组。

首先把txt读入到字符串中

char str[3600];
FILE *fp = fopen("xxx.txt","r");
if(fp == NULL) break;
int i=0;
char ch;
// 循环将文本读入到字符串中
while(ch = fgetc(fp))
{
    str[i] = ch;
    i++;
}
// 字符串插入结束符
str[i] = '\0';
fclose(fp);

本回答被网友采纳
第2个回答  2015-11-24
这种情况如果拘泥用c++语句的话会造成不必要的麻烦,用c更简洁:
#include<stdio.h>
#include<stdlib.h>
#define DATASIZE 1024
void main()
{ char buffer[256]; //每行最多255个字符
float a[DATASIZE][4]; //本程序只处理DATASIZE行数据,每行4个数据
int n;
FILE *fp;
if ( fp=fopen("data.txt","r") )
{ n=0;
while ( !feof(fp)&&n<DATASIZE )
{ fgets(buffer,255,fp);
sscanf(buffer,"%f,%f,%f,%f",&a[n][0],&a[n][1],&a[n][2],&a[n][3]);
n++;
}
fclose(fp);
}
else printf("无法打开文件读取数据\n");
//至此n行数据已经读入数组a
}
第3个回答  2014-04-09
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int a[800][4]={0};
fstream f("test.txt",ios::in);
for(int i=0;i<800;i++)
{
for(int j=0;j<4;j++)
{
f>>a[i][j];
cout<<a[i][j]<<' ';
}
cout<<endl;
}
f.close();
    return 0; 
}

相似回答