how to extract data in a textfile

H

hope

Dear all,

I have a txt file with list of numbers delimited by spaces. (123 456 34
33......) How can I extract these numbers in C++? Thanks.
 
G

Gwar

Dear all,

I have a txt file with list of numbers delimited by spaces. (123 456 34
33......) How can I extract these numbers in C++? Thanks.

// One way:

// data.txt:
// ---------
// 123
// 456
// 34
// 33
// ---------


#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
ifstream ifs("data.txt");
if(!ifs)
cout << "File not opened" << endl;
vector<double> dv;
string s;
double d;
char *pend;
while((getline(ifs, s, '\n')))
{
d = strtod(s.c_str(), &pend);
dv.push_back(d);
}
copy(dv.begin(), dv.end(), ostream_iterator<double>(cout, "\t"));
cout << endl;
}
 
D

David Harmon

On Sun, 4 Jul 2004 09:40:33 +0800 in comp.lang.c++, "hope"
I have a txt file with list of numbers delimited by spaces. (123 456 34
33......) How can I extract these numbers in C++? Thanks.

That question is answered in great length in any reasonable beginners
C++ textbook. What do you wish to do with the extracted values? Could
you be more explicit?

int total=0; item=0;
std::ifstream data("filename.txt");
while (filename >> item)
total += item;

In addition, This issue is covered in Marshall Cline's C++ FAQ. See the
section "[15] Input/output via <iostream> and <cstdio>". It is always
good to check the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 
C

Chris Theis

hope said:
Dear all,

I have a txt file with list of numbers delimited by spaces. (123 456 34
33......) How can I extract these numbers in C++? Thanks.

You can easily use input stream iterators for this task:

#include <list>
#include <iostream>
#include <fstream>

int main()
{
using namespace std;

ifstream DataFile("ints.dat");
list<int> Data( (istream_iterator<int>( DataFile) ),
istream_iterator<int>() );
}

HTH
Chris
 
G

Gwar

You can easily use input stream iterators for this task:

#include <list>
#include <iostream>
#include <fstream>

int main()
{
using namespace std;

ifstream DataFile("ints.dat");
list<int> Data( (istream_iterator<int>( DataFile) ),
istream_iterator<int>() );
}

// What about for dealing with matrices?

#include <vector>
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <sstream>


using namespace std;

void parse(string& s, int& n, vector<int>& nv);
void display(vector <int>& v);

int main()
{

ifstream ifs("ints.dat");
string s;
vector<int> v;
vector< vector<int> > vm;
int n;
int i = 1;
while(getline(ifs, s, '\n'))
{
parse(s, n, v);
vm.push_back(v);
v.erase(v.begin(), v.end());
}
for_each(vm.begin(), vm.end(), display);


}

void parse(string& s, int& n, vector<int>& v)
{
istringstream ss(s);
while(ss >> n)
v.push_back(n);

}

void display(vector <int>& v)
{
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
 
C

Chris Theis

Gwar said:
// What about for dealing with matrices?

#include <vector>
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <sstream>


using namespace std;

void parse(string& s, int& n, vector<int>& nv);
void display(vector <int>& v);

int main()
{

ifstream ifs("ints.dat");
string s;
vector<int> v;
vector< vector<int> > vm;
int n;
int i = 1;
while(getline(ifs, s, '\n'))
{
parse(s, n, v);
vm.push_back(v);
v.erase(v.begin(), v.end());
}
for_each(vm.begin(), vm.end(), display);


}

void parse(string& s, int& n, vector<int>& v)
{
istringstream ss(s);
while(ss >> n)
v.push_back(n);

}

void display(vector <int>& v)
{
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}

That works just fine. My solution for this problem is very similar, only
that my parse function looks like the following:

////////////////////////////////////////////////////////////////////////////
//
template <class T>
std::vector<T> StringToVec( const std::string& Str )
// Tokenize a passed line/string into a vector
// e.g:
// std::string str = "1 2 3 4";
// std::vector<int> vec = StringToVector<int>(str);
////////////////////////////////////////////////////////////////////////////
//
{
std::vector<T> MyVec;
copy( std::istream_iterator<T>( std::istringstream( Str ) ),
std::istream_iterator<T>(), back_inserter(MyVec) );
return MyVec;
}

Of course one could pass the vector as a parameter & hence save unnecessary
copies (which might be optimized by the compiler anyway). If your compiler
supports it (MSVC 6 for example doesn't!) you can save yourself the copy
statement and pass the istream_iterators to the ctor of the vector.

Cheers
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top