C语言中,如何从TXT文件中读出每列的数据,存放到一个数组中!

每行数据为33个,都为double数据,如何取出每列的数据,每列数据各存放一个数组中,每个txt文档大小不一样,有几千行的,也有几百行的,要取出的数据都为double 小数后6位!给出完整代码给高分!
比如数据为:
0 1.123456 2.454646 2.1213142 3.223132321 2.1213131
0.001 1.133456 2.254646 1.121314 1.223131 1.1313131
0.002 2.133456 1.25464623 3.12131423 1.2311312 3.1313131
0.003 3.133456 2.234646 -1.1213142 10.22313123 -11.1313131
0.004 4.233456 3.234646 -11.121314 10.523131 -1.1313131
0.005 13.133456 -2.234646 -1.521314 -1.223131 10.3413131
0.006 -3.133456 -1.234646 -11.121314 -10.223131 -2.3313131
0.007 3.8396266 2.1358943 6.12584331 4.223131 -1.3524564
0.008 13.13342356 12.234646 -11.423514 8.423131 -5.4313131
0.009 2.234456 -2.235323646 -10.23132314 11.22453231 -10.2323131
0.010 4.256456 -2.344646 -1.368314 8.232255131 -2.1313131
。。。。。。。。。

这道题很简单呀,我就给你说说思路吧.只有自己动手做做才有收获.
先open(文件);
char *buf = NULL;
while(!不是文件末尾){
buf = gets(获取一行内容);
然后根据空格来分别提取存入数组中;
没一个数据;}追问

C语言中 如何取出字符串中俩空格之间的字符

追答

有现成的函数可以用呀.
char * strtok(char *s, " ");

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-11-10
每列数据各存放一个数组中
是不是要 33个数组来存了
行数未知,最好用 链表来实现了
第2个回答  2015-10-02
程序代码:
#include <stdio.h>
#include <string.h> //包含memset函数
#include <stdlib.h> //包含double atof(const char *nptr);

#define MAX_LEN 260

#ifndef _MSC_VER
#define bool int
#define false 0
#define true 1
#endif

int main()
{
float num[100]={0};
int num_index = 0;
FILE * lpFile = NULL;
lpFile = fopen("2.txt","r");
if(lpFile)
{
char ch;
char szBuf[MAX_LEN]={0};
bool bHasSpace=false;
int i=0;
while((ch=fgetc(lpFile)) != EOF)
{
//过滤多余空格,如果有\r或\t ,顺便过滤掉
if(ch == '\r')continue;
while( ch == ' ' || ch == '\t')
{
ch=fgetc(lpFile);
bHasSpace = true;
}

if(bHasSpace)//多个空格使用一个空格代替
{
bHasSpace = false;
szBuf[i++]=' ';
}
szBuf[i++]=ch;

if( ch == '\n' || ch == EOF)//读完一行
{
//开始解析字符串
if(num_index<100)
{

int count=0;

for(char * p = szBuf;*p;p++)
{
if(*p == ' ')
count ++;
if(count == 5)
{
char szNum[MAX_LEN]={0};
int tmp=0;
while(*(++p) && *p != ' ')
{
szNum[tmp++]=*p;

}
if(tmp)
{
num[num_index++] = atof(szNum);
}
break;
}
}
}

i=0;
memset(szBuf,0,sizeof(szBuf));
if(ch == EOF)
break;
}
}

fclose(lpFile);
}
for(int i=0;i<num_index;i++)
{
printf("%f\n",num[i]);
}
return 0;
第3个回答  2012-11-11
用buffer提取后想怎么存,就随意拉
相似回答