(12.03,515.04,117.9,52.79,13.58)
(18.03,415.04,217.9,42.79,23.58)
(11.03,315.04,317.9,32.79,33.58)
(1.03,215.04,417.9,22.79,43.58)
(32.03,115.04,517.9,12.79,53.58)
.
.
.
.
数据格式是固定的就是括号里面5个float,具体有多少行不固定
怎么从这个txt文件中一行一行的读取,并将数据保存到数组中 比如: char cStr[5];
cStr[0] =(12.03,515.04,117.9,52.79,13.58);
cStr[1] =(18.03,415.04,217.9,42.79,23.58);
cStr[2] = (11.03,315.04,317.9,32.79,33.58);
cStr[3] =(1.03,215.04,417.9,22.79,43.58);
cStr[4] =(32.03,115.04,517.9,12.79,53.58);
求大神帮忙指导下,求详细点的C++代码,小弟万分感谢!!!!
12 个解决方案
#1
fin.getline();
#2
求大神能指导的详细点的
#3
干这种活,C更擅长。
没实际编译链接调试,不保证对,仅供参考:
没实际编译链接调试,不保证对,仅供参考:
#include <stdio.h>
#define MAXDATA 1000
double D[MAXDATA][5];
char ln[80];
FILE *f;
int i,n;
int main() {
f=fopen("Data.txt","r");
if (NULL==f) {
printf("Can not open file Data.txt!\n");
return 1;
}
i=0;
while (1) {
if (NULL==fgets(ln,80,f)) break;
if (5==sscanf(ln,"(%lf,%lf,%lf,%lf,%lf",&D[i][0],&D[i][1],&D[i][2],&D[i][3],&D[i][4])
|| 5==sscanf(ln,"(%lf,%lf,%lf,%lf,%lf",&D[i][0],&D[i][1],&D[i][2],&D[i][3],&D[i][4])) {
} else {
printf("Ignore format error at line %d:%s",i+1,ln);
}
i++;
if (i>=MAXDATA) {
printf("Too many data(>%d lines), ignore it.\n",MAXDATA);
break;
}
}
fclose(f);
n=i;
for (i=0;i<n;i++) {
printf("%04d (%5.2lf,%6.2lf,%5.1lf,%5.2lf,%5.2lf)\n",i+1,D[i][0],D[i][1],D[i][2],D[i][3],D[i][4]);
}
return 0;
}
#4
有哪位C++er不赞同“干这种活,C更擅长。”的,欢迎站出来实现一个和3楼代码功能一样的纯C++代码PK一下!
#5
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
vector<vector<double> > buffer;
vector<vector<double> >::iterator i;
vector<double>::iterator j;
int main()
{
ifstream file("Data.txt");
double tmp_d;
char tmp_c;
string tmp;
stringstream ss;
while (file >> tmp ) {
ss<<tmp;
vector<double> v;
for(int i=0;i<5;++i)
{
ss>>tmp_c>>tmp_d;
v.push_back(tmp_d);
}
ss.get();
buffer.push_back(v);
}
for(i=buffer.begin();i!=buffer.end();++i)
{
for(j=i->begin();j!=i->end();++j)
{
cout<<setfill('0')<<setw(5)<<*j<<"\t";
}
cout<<endl;
}
return 0;
}
#6
fstream,getline。
#7
谢谢,学习了
#8
@zhao4zhong1 ,我用fgets函数,是将f文件夹里面的每一行都付给了数组In,可是:后面一行都会覆盖之前一行,付给了In.
结果为:读第一行In=“(12.03,515.04,117.9,52.79,13.58)”;第二行的时候 In=“(18.03,415.04,217.9,42.79,23.58)”
我想实现的是:比如一共4行
In[0]=(12.03,515.04,117.9,52.79,13.58);
In[1]=(18.03,415.04,217.9,42.79,23.58);
In[2]=(11.03,315.04,317.9,32.79,33.58);
In[3]=(1.03,215.04,417.9,22.79,43.58);
结果为:读第一行In=“(12.03,515.04,117.9,52.79,13.58)”;第二行的时候 In=“(18.03,415.04,217.9,42.79,23.58)”
我想实现的是:比如一共4行
In[0]=(12.03,515.04,117.9,52.79,13.58);
In[1]=(18.03,415.04,217.9,42.79,23.58);
In[2]=(11.03,315.04,317.9,32.79,33.58);
In[3]=(1.03,215.04,417.9,22.79,43.58);
#9
5楼代码的功能比3楼代码的功能少很多。
#10
@zhao4zhong1 , 我贴出那段代码不是回复给你看的.
#11
扫了一眼, 你的代码毫无新意.
你的代码预设了太多经验性的参数, 楼主的数据只需稍作改动, 你的代码 改动起来便苦不堪言.
你的代码预设了太多经验性的参数, 楼主的数据只需稍作改动, 你的代码 改动起来便苦不堪言.
#12
这么多的代码怎么样才能写出来呢
#1
fin.getline();
#2
求大神能指导的详细点的
#3
干这种活,C更擅长。
没实际编译链接调试,不保证对,仅供参考:
没实际编译链接调试,不保证对,仅供参考:
#include <stdio.h>
#define MAXDATA 1000
double D[MAXDATA][5];
char ln[80];
FILE *f;
int i,n;
int main() {
f=fopen("Data.txt","r");
if (NULL==f) {
printf("Can not open file Data.txt!\n");
return 1;
}
i=0;
while (1) {
if (NULL==fgets(ln,80,f)) break;
if (5==sscanf(ln,"(%lf,%lf,%lf,%lf,%lf",&D[i][0],&D[i][1],&D[i][2],&D[i][3],&D[i][4])
|| 5==sscanf(ln,"(%lf,%lf,%lf,%lf,%lf",&D[i][0],&D[i][1],&D[i][2],&D[i][3],&D[i][4])) {
} else {
printf("Ignore format error at line %d:%s",i+1,ln);
}
i++;
if (i>=MAXDATA) {
printf("Too many data(>%d lines), ignore it.\n",MAXDATA);
break;
}
}
fclose(f);
n=i;
for (i=0;i<n;i++) {
printf("%04d (%5.2lf,%6.2lf,%5.1lf,%5.2lf,%5.2lf)\n",i+1,D[i][0],D[i][1],D[i][2],D[i][3],D[i][4]);
}
return 0;
}
#4
有哪位C++er不赞同“干这种活,C更擅长。”的,欢迎站出来实现一个和3楼代码功能一样的纯C++代码PK一下!
#5
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
vector<vector<double> > buffer;
vector<vector<double> >::iterator i;
vector<double>::iterator j;
int main()
{
ifstream file("Data.txt");
double tmp_d;
char tmp_c;
string tmp;
stringstream ss;
while (file >> tmp ) {
ss<<tmp;
vector<double> v;
for(int i=0;i<5;++i)
{
ss>>tmp_c>>tmp_d;
v.push_back(tmp_d);
}
ss.get();
buffer.push_back(v);
}
for(i=buffer.begin();i!=buffer.end();++i)
{
for(j=i->begin();j!=i->end();++j)
{
cout<<setfill('0')<<setw(5)<<*j<<"\t";
}
cout<<endl;
}
return 0;
}
#6
fstream,getline。
#7
谢谢,学习了
#8
@zhao4zhong1 ,我用fgets函数,是将f文件夹里面的每一行都付给了数组In,可是:后面一行都会覆盖之前一行,付给了In.
结果为:读第一行In=“(12.03,515.04,117.9,52.79,13.58)”;第二行的时候 In=“(18.03,415.04,217.9,42.79,23.58)”
我想实现的是:比如一共4行
In[0]=(12.03,515.04,117.9,52.79,13.58);
In[1]=(18.03,415.04,217.9,42.79,23.58);
In[2]=(11.03,315.04,317.9,32.79,33.58);
In[3]=(1.03,215.04,417.9,22.79,43.58);
结果为:读第一行In=“(12.03,515.04,117.9,52.79,13.58)”;第二行的时候 In=“(18.03,415.04,217.9,42.79,23.58)”
我想实现的是:比如一共4行
In[0]=(12.03,515.04,117.9,52.79,13.58);
In[1]=(18.03,415.04,217.9,42.79,23.58);
In[2]=(11.03,315.04,317.9,32.79,33.58);
In[3]=(1.03,215.04,417.9,22.79,43.58);
#9
5楼代码的功能比3楼代码的功能少很多。
#10
@zhao4zhong1 , 我贴出那段代码不是回复给你看的.
#11
扫了一眼, 你的代码毫无新意.
你的代码预设了太多经验性的参数, 楼主的数据只需稍作改动, 你的代码 改动起来便苦不堪言.
你的代码预设了太多经验性的参数, 楼主的数据只需稍作改动, 你的代码 改动起来便苦不堪言.
#12
这么多的代码怎么样才能写出来呢