Read data into c++

A

axcytz

Hi all,

I have a data that looks like this:

1,2,3
1,3,4
2,4
1
2,4,5,6

I have these data in an excel file but can also copy into a txt file. I have declared a vector in c++ and want to read these data into my c++ code. For small problems, I typed it manually in my c++ code as:

int init1[] = {1,2,3};
std::vector<int> row1( init1, init1+sizeof(init1)/sizeof(init1[0]));
P.push_back(row1);

int init2[] = {1, 3, 4};
std::vector<int> row2( init2, init2+sizeof(init2)/sizeof(init2[0]));
P.push_back(row2);

and so on.

How can I import these data from txt or excel file?

Thanks
 
B

Barry Schwarz

Hi all,

I have a data that looks like this:

1,2,3
1,3,4
2,4
1
2,4,5,6

I have these data in an excel file but can also copy into a txt file. I have declared a vector in c++ and want to read these data into my c++ code. For small problems, I typed it manually in my c++ code as:

int init1[] = {1,2,3};
std::vector<int> row1( init1, init1+sizeof(init1)/sizeof(init1[0]));
P.push_back(row1);

int init2[] = {1, 3, 4};
std::vector<int> row2( init2, init2+sizeof(init2)/sizeof(init2[0]));
P.push_back(row2);

and so on.

How can I import these data from txt or excel file?

Consider the following:

Define V as a vector<vector<int>>, a two dimensional vector.

Loop through each line using getline to read one line into a
buffer.
Append a \n to buffer
Add vector element V to V with zero int elements.
Loop through buffer using find_first_of to note end of
current value (at comma or \n) .
Extract value, convert to int, and store value in added
int element V[j]
Repeat until buffer exhausted (\n reached).
Repeat until end of file.
 
S

Stuart

Hi all,

I have a data that looks like this:

1,2,3
1,3,4
2,4
1
2,4,5,6

I have these data in an excel file but can also copy into a txt file. I have declared a vector in c++ and want to read these data into my c++ code. For small problems, I typed it manually in my c++ code as:

int init1[] = {1,2,3};
std::vector<int> row1( init1, init1+sizeof(init1)/sizeof(init1[0]));
P.push_back(row1);

int init2[] = {1, 3, 4};
std::vector<int> row2( init2, init2+sizeof(init2)/sizeof(init2[0]));
P.push_back(row2);

and so on.

How can I import these data from txt or excel file?

Reading from a text file can be achieved with standard C++ library
classes like ifstream. If you have problems with that, you can get more
help in this newsgroup.

On the other hand, reading from Excel files is considered as off-topic
by many people in this newsgroup. You can do this through the COM
interface of Microsoft Office (COM = Component Object Model). However,
this requires a lot more knowledge. So you should only do this, if the
export of the Excel file into a text file must be automated as well.

Questions about Excel automation should be asked in one of the MSDN
forums (MS "discontinued" their own newsgroups microsoft.public.* some
years ago in favour of their new web-based forums). I don't like MSDN,
so if you want to get help from people like me, I'd suggest to use
something like stackoverflow.

Regards,
Stuart
 
J

Jorgen Grahn

Hi all,

I have a data that looks like this:

1,2,3
1,3,4
2,4
1
2,4,5,6

I have these data in an excel file but can also copy into a txt file. I have declared a vector in c++ and want to read these data into my c++ code. For small problems, I typed it manually in my c++ code as:

int init1[] = {1,2,3};
std::vector<int> row1( init1, init1+sizeof(init1)/sizeof(init1[0]));
P.push_back(row1);

int init2[] = {1, 3, 4};
std::vector<int> row2( init2, init2+sizeof(init2)/sizeof(init2[0]));
P.push_back(row2);

and so on.

How can I import these data from txt or excel file?

Reading from a text file can be achieved with standard C++ library
classes like ifstream. If you have problems with that, you can get more
help in this newsgroup.

On the other hand, reading from Excel files is considered as off-topic
by many people in this newsgroup.

I was assuming his "excel file" was in fact a CSV file -- a text file
representation of a simple two-dimensional Excel sheet. That's
something we could discuss.
You can do this through the COM
interface of Microsoft Office (COM = Component Object Model).

Offtopic: there are happily also other ways to do this. For example,
there are Python modules which convert to a text representation, on
any OS and without MS Office or COM.

/Jorgen
 
V

Vlad from Moscow

пÑтница, 2 авгуÑта 2013 г., 4:17:44 UTC+4 пользователь (e-mail address removed) напиÑал:
Hi all,



I have a data that looks like this:



1,2,3

1,3,4

2,4

1

2,4,5,6



I have these data in an excel file but can also copy into a txt file. I have declared a vector in c++ and want to read these data into my c++ code. For small problems, I typed it manually in my c++ code as:



int init1[] = {1,2,3};

std::vector<int> row1( init1, init1+sizeof(init1)/sizeof(init1[0]));

P.push_back(row1);



int init2[] = {1, 3, 4};

std::vector<int> row2( init2, init2+sizeof(init2)/sizeof(init2[0]));

P.push_back(row2);



and so on.



How can I import these data from txt or excel file?



Thanks



Here is a code snip that demonstrates how the task can be performed

Code:
std::string buffer = "1,2,3\n1,3,4\n2,4\n1\n2,4,5,6";

std::istringstream simulate_file( buffer );

std::string line;
std::vector<std::vector<int>> v;

while ( std::getline( simulate_file, line ) )
{
std::istringstream is( line );
std::string s;
std::vector<int> v1;

while ( std::getline( is, s, ',' ) ) v1.push_back( std::stoi( s ) );
v.push_back( v1 );
}

for ( const std::vector<int> &v1 : v )
{
for ( int x : v1 ) std::cout << x << ' ';
std::cout << std::endl;
}
 
J

Jorgen Grahn

Hi all,

I have a data that looks like this:

1,2,3
1,3,4
2,4
1
2,4,5,6

(Pedantic mode)

That's an example, not a description of the problem.

I can /guess/ that valid input is a series of lists of 1 or more
integers, with no "holes". But I cannot guess what you want to /do/
with them, and that is important in order to select a good solution.

A vector<vector<int>> might be a good data structure, but it might
also be the wrong one. It gets easier if you can say "an absent
number is equivalent to 0" or something.

When parsing an input language, I always prefer to read straight into
the kind of data structure I will eventually use. If you do half the work
in a postprocessing step, it's hard to give good syntax error messages
because you've forgotten which file and line the problem came from.

/Jorgen
 
S

Stuart

On 08/02/13, Jorgen Grahn wrote:

[snip]
Offtopic: there are happily also other ways to do this. For example,
there are Python modules which convert to a text representation, on
any OS and without MS Office or COM.

I also found http://www.libxl.com. Apparently this provides a thin
wrapper around the COM API of Excel. The example code looks nice:

Book* book = xlCreateBook();
Sheet* sheet = book->addSheet(L"Sheet1");
sheet->writeStr(2, 1, L"Hello, World !");
sheet->writeNum(3, 1, 1000);
book->save(L"example.xls");
book->release();

Pity about the "release" call at the end. A smart pointer would have
been appropriate.

Regards,
Stuart
 
A

axcytz

пÑтница, 2 авгуÑта 2013 г., 4:17:44 UTC+4 пользователь (e-mail address removed) напиÑал:
Hi all,
I have a data that looks like this:

I have these data in an excel file but can also copy into a txt file. Ihave declared a vector in c++ and want to read these data into my c++ code.. For small problems, I typed it manually in my c++ code as:
int init1[] = {1,2,3};
std::vector<int> row1( init1, init1+sizeof(init1)/sizeof(init1[0]));
P.push_back(row1);

int init2[] = {1, 3, 4};
std::vector<int> row2( init2, init2+sizeof(init2)/sizeof(init2[0]));
P.push_back(row2);

and so on.
How can I import these data from txt or excel file?
Thanks







Here is a code snip that demonstrates how the task can be performed



Code:
std::string buffer = "1,2,3\n1,3,4\n2,4\n1\n2,4,5,6";



std::istringstream simulate_file( buffer );



std::string line;

std::vector<std::vector<int>> v;



while ( std::getline( simulate_file, line ) )

{

	std::istringstream is( line );

	std::string s;

	std::vector<int> v1;



	while ( std::getline( is, s, ',' ) ) v1.push_back( std::stoi( s ) );

	v.push_back( v1 );

}



for ( const std::vector<int> &v1 : v )

{

	for ( int x : v1 ) std::cout << x << ' ';

	std::cout << std::endl;

}

Thanks Vlad for your help. It seems like in the first line, I should type my data in your code. However, I have at least 200 of those, and it is not efficient to do so. I want to read them from my excel spreadsheet, so I willprobably need to state my file name, etc, right?
 
A

axcytz

(Pedantic mode)



That's an example, not a description of the problem.



I can /guess/ that valid input is a series of lists of 1 or more

integers, with no "holes". But I cannot guess what you want to /do/

with them, and that is important in order to select a good solution.



A vector<vector<int>> might be a good data structure, but it might

also be the wrong one. It gets easier if you can say "an absent

number is equivalent to 0" or something.



When parsing an input language, I always prefer to read straight into

the kind of data structure I will eventually use. If you do half the work

in a postprocessing step, it's hard to give good syntax error messages

because you've forgotten which file and line the problem came from.



/Jorgen



--

// Jorgen Grahn <grahn@ Oo o. . .

\X/ snipabacken.se> O o .

Hi Mr. Jorgen, I have some integers like 15, 16,17, etc. so I should have holes to differentiate them.
 
J

Jorgen Grahn

[Google Groups messed up the formatting, so I cut heavily]
....
....

Hi Mr. Jorgen, I have some integers like 15, 16,17, etc. so I should
have holes to differentiate them.

With "holes" I meant Excel cells with no value in them, but with
filled-in cells around it. You already showed that different rows can
contain a different number of values, but if

1, 2, 3
4, , 5

can exist and is not the same thing as

1, 2, 3
4, 5

or

1, 2, 3
4, 0, 5

then you can't just use a vector<int> to store a row.

/Jorgen
 
A

axcytz

[Google Groups messed up the formatting, so I cut heavily]


...



...



Hi Mr. Jorgen, I have some integers like 15, 16,17, etc. so I should
have holes to differentiate them.



With "holes" I meant Excel cells with no value in them, but with

filled-in cells around it. You already showed that different rows can

contain a different number of values, but if



1, 2, 3

4, , 5



can exist and is not the same thing as



1, 2, 3

4, 5



or



1, 2, 3

4, 0, 5



then you can't just use a vector<int> to store a row.



/Jorgen



--

// Jorgen Grahn <grahn@ Oo o. . .

\X/ snipabacken.se> O o .

I'm sorry I got it wrong. I don't have something like

1, 2, 3
4, , 5

but I have

1, 2, 3
4, 5

type of data
 
L

Luca Risolia

I have a data that looks like this:

1,2,3
1,3,4
2,4
1
2,4,5,6

I have these data in an excel file but can also copy into a txt file. I have
declared a vector in c++ and want to read these data into my c++ code.
How can I import these data from txt or excel file?

For those kind of things, use a standard input stream and tell it that "," has
to be considered as a "white space". The key point is std::istream::imbue.

Below is an example:

#include <iostream>
#include <fstream>
#include <sstream>
#include <iterator>
#include <string>
#include <vector>
#include <cctype>
#include <locale>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> get_vector(istream& is) {
istream_iterator<int> ii(is), eof;
return vector<int>(ii, eof);
}

void print_vector(const vector<int>& v) {
ostream_iterator<int> oo(cout, "\n");
copy(begin(v), end(v), oo);
}

struct Facet : ctype<char> {
Facet() : ctype<char>(get_table()) {}

static ctype_base::mask const* get_table() {
static vector<ctype_base::mask>
rc(classic_table(), classic_table() + table_size);

for (size_t i = 0; i < table_size; i++)
if (i == ',')
rc = ctype_base::space;

return &rc[0];
}
};

int main(int argc, char** argv) {
istream* in = &cin;

if (argc == 2)
in = new ifstream(argv[1]);

istringstream istr;
istr.imbue(locale(locale(), new Facet())); // the facet will be destroyed
for (string line; getline(*in, line); istr.clear()) {
istr.str(line);
vector<int> v = get_vector(istr);
print_vector(v);
}

if (in != &cin)
delete in;

return 0;
}
 

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

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top