怎样使用VC++读取文本文件数据?解决思路
怎样使用VC++读取文本文件数据?
文本文件的数据格式如下:
50.5374633 -2.4570890 1166.9398546,50.5369380 -2.0802710 1166.9278145,
50.5346507 -1.3266250 1166.8752432,50.5343117 -1.7033891 1166.8672562,
50.5328551 -13.7597295 1166.8159962,......
数据使用逗号分隔,三个数据为一组,每行有多条数据,请高手指点一下:在VC++中怎样读取这个文本数据呢?
------解决方案--------------------
呵呵,这个我今天刚写过。。。不过读的是obj file
先写一个大while loop,每次getline一次,直到读完文件为止
先定义一个vector,然后设一个bool变量(就叫它located吧)
然后用一个while loop, 如果非空格,非逗号,located为false,就进行循环
这个地方比较难解释,看我的代码吧。差不多是这个意思
for(int xyz_idx=0; xyz_idx <3; xyz_idx++)
{
string acc_str;
string_located = false;
while(mystr[string_idx] != ' ' || !string_located)
{
if(mystr[string_idx] != ' ') string_located = true;
if(string_located)
{
acc_str+=mystr[string_idx];
}
string_idx++;
}
float xyz_val = atof(acc_str.data());
normals.push_back(xyz_val);
cout << "Located coordinate "<< (xyz_idx == 0?"x":(xyz_idx == 1?"y":"z")) << " = " << xyz_val << "("<<acc_str.data()<<")"<<endl;
}
------解决方案--------------------
我的方法,可能比较笨:
1、创建一个istringstream对象,读入文本对象。
2、将istringstream对象的字符串中的全部逗号改为空格。
3、从istringstream对象的字符串中读入数据。
------解决方案--------------------
其实题道题有楼上的做法很简单,最直观了,一点都不笨.
二楼的做法有点小题大做了,而且还有漏洞,呵呵,逗号判断哪去了?
具体做法:先用getline读取一行文本字符串,保存到str,
然后用replace将逗号全部替换为空格,然后再用str构造一个
istringstream,从里面每读3个分一组,OK了啊,c++标准的
字符串流和函数效率是很高的,不必从字符的角度对待.
------解决方案--------------------
void Replace(std::string& str, char former, char latter)
{
for(int i = 0; str[i] != '\0'; i++)
{
if(str[i] == former)
{
str[i] = latter;
}
}
}
int main()
{
std::ifstream infile("ReadFile.txt");
std::ofstream outfile("ReadFile_.txt");
if(infile)
{
std::cout << "Reading in from ReadFile.txt.\n";
std::string line, str;
std::getline(infile, line, '\n');
Replace(line, ',', ' ');
std::istringstream stream(line);
int count = 1;
while(stream >> str)
{
outfile << str.data() << ( (count == 3) ? '\n' : ' ' );
count = (count++) % 4;
}
}
outfile << std::endl;
return 0;
}
路过, 学习...
------解决方案--------------------
发现你们可能都想复杂了,其实输入数字的时候,C++可以自动略过逗号的。空格和逗号都是数字分隔符
参考:
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
double x, y, z;
ifstream ifile("text1.txt", ios::in);
ifile >> x >> y >> z;
cout << x << endl;
cout << y << endl;
cout << z << endl;
ifile >> x >> y >> z;
cout << x << endl;
cout << y << endl;
cout << z << endl;
ifile >> x >> y >> z;
cout << x << endl;
cout << y << endl;
cout << z << endl;
ifile >> x >> y >> z;
cout << x << endl;
cout << y << endl;
cout << z << endl;
}
文本文件的数据格式如下:
50.5374633 -2.4570890 1166.9398546,50.5369380 -2.0802710 1166.9278145,
50.5346507 -1.3266250 1166.8752432,50.5343117 -1.7033891 1166.8672562,
50.5328551 -13.7597295 1166.8159962,......
数据使用逗号分隔,三个数据为一组,每行有多条数据,请高手指点一下:在VC++中怎样读取这个文本数据呢?
------解决方案--------------------
呵呵,这个我今天刚写过。。。不过读的是obj file
先写一个大while loop,每次getline一次,直到读完文件为止
先定义一个vector,然后设一个bool变量(就叫它located吧)
然后用一个while loop, 如果非空格,非逗号,located为false,就进行循环
这个地方比较难解释,看我的代码吧。差不多是这个意思
for(int xyz_idx=0; xyz_idx <3; xyz_idx++)
{
string acc_str;
string_located = false;
while(mystr[string_idx] != ' ' || !string_located)
{
if(mystr[string_idx] != ' ') string_located = true;
if(string_located)
{
acc_str+=mystr[string_idx];
}
string_idx++;
}
float xyz_val = atof(acc_str.data());
normals.push_back(xyz_val);
cout << "Located coordinate "<< (xyz_idx == 0?"x":(xyz_idx == 1?"y":"z")) << " = " << xyz_val << "("<<acc_str.data()<<")"<<endl;
}
------解决方案--------------------
我的方法,可能比较笨:
1、创建一个istringstream对象,读入文本对象。
2、将istringstream对象的字符串中的全部逗号改为空格。
3、从istringstream对象的字符串中读入数据。
------解决方案--------------------
其实题道题有楼上的做法很简单,最直观了,一点都不笨.
二楼的做法有点小题大做了,而且还有漏洞,呵呵,逗号判断哪去了?
具体做法:先用getline读取一行文本字符串,保存到str,
然后用replace将逗号全部替换为空格,然后再用str构造一个
istringstream,从里面每读3个分一组,OK了啊,c++标准的
字符串流和函数效率是很高的,不必从字符的角度对待.
------解决方案--------------------
void Replace(std::string& str, char former, char latter)
{
for(int i = 0; str[i] != '\0'; i++)
{
if(str[i] == former)
{
str[i] = latter;
}
}
}
int main()
{
std::ifstream infile("ReadFile.txt");
std::ofstream outfile("ReadFile_.txt");
if(infile)
{
std::cout << "Reading in from ReadFile.txt.\n";
std::string line, str;
std::getline(infile, line, '\n');
Replace(line, ',', ' ');
std::istringstream stream(line);
int count = 1;
while(stream >> str)
{
outfile << str.data() << ( (count == 3) ? '\n' : ' ' );
count = (count++) % 4;
}
}
outfile << std::endl;
return 0;
}
路过, 学习...
------解决方案--------------------
发现你们可能都想复杂了,其实输入数字的时候,C++可以自动略过逗号的。空格和逗号都是数字分隔符
参考:
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
double x, y, z;
ifstream ifile("text1.txt", ios::in);
ifile >> x >> y >> z;
cout << x << endl;
cout << y << endl;
cout << z << endl;
ifile >> x >> y >> z;
cout << x << endl;
cout << y << endl;
cout << z << endl;
ifile >> x >> y >> z;
cout << x << endl;
cout << y << endl;
cout << z << endl;
ifile >> x >> y >> z;
cout << x << endl;
cout << y << endl;
cout << z << endl;
}