470,862 Members | 1,712 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,862 developers. It's quick & easy.

Reading columns in a text file

Suppose I have a text file with the input:
1 2 3 4 5 6 7 8 9 10 ! Comment: Integers 1 - 10

How do I write a C++ program that reads in this line into a 10-element
vector and ignores the comment?

Thanks.
Jun 27 '08 #1
4 3533
C++ Newbie wrote:
Suppose I have a text file with the input:
1 2 3 4 5 6 7 8 9 10 ! Comment: Integers 1 - 10

How do I write a C++ program that reads in this line into a 10-element
vector and ignores the comment?
How do you know there are 10 elements?

Basically, you read integers and stuff them into a vector until you get
an error or the end of the line. If you get an error, ignore the rest
of the line. I strongly suggest two step processing: first, read a line
from your file, then, second, process the line you just read to extract
the individual integers (until the end of the line or an error which
should mean the end of the vector).

To read a line use 'std::geline' function. Then define a istringstream
from the line you just read into a string, and loop while it's "good".
Read an individual int, and if successful, stuff it into your vector.
Once your istringstream is no good, proceed to reading the next line
from the file. Do that until the file has no more lines.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
On 16 mai, 16:54, Victor Bazarov <v.Abaza...@comAcast.netwrote:
C++ Newbie wrote:
Suppose I have a text file with the input:
1 2 3 4 5 6 7 8 9 10 ! Comment: Integers 1 - 10
How do I write a C++ program that reads in this line into a
10-element vector and ignores the comment?
How do you know there are 10 elements?
Or more to the point, does he know? And what determines what is
a comment, and what isn't?
Basically, you read integers and stuff them into a vector
until you get an error or the end of the line. If you get an
error, ignore the rest of the line.
Maybe. Until we know what the actual specification is, any
suggestions are just guesswork. If the specification says that
the '!' character starts a comment, then the simplest solution
might be to use a filtering streambuf, so that characters from
the '!' to the line end simply don't show up in the input.
Although if the syntax is otherwise line oriented, this might be
overkill, since you can use getline, as you propose later. If,
on the other hand, the syntax is 10 elements, and anything else
is a comment, you need some other approach.
I strongly suggest two step processing: first, read a line
from your file, then, second, process the line you just read
to extract the individual integers (until the end of the line
or an error which should mean the end of the vector).
That's generally a good solution if the syntax is line oriented.
If the syntax says that anything following a '!' is a comment,
then it is trivial to trim anything after the first '!' from the
input line. It can be made to work in more complicated cases as
well.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #3
Victor Bazarov wrote:
C++ Newbie wrote:
>Suppose I have a text file with the input:
1 2 3 4 5 6 7 8 9 10 ! Comment: Integers 1 - 10

How do I write a C++ program that reads in this line into a
10-element vector and ignores the comment?

How do you know there are 10 elements?

Basically, you read integers and stuff them into a vector until you
get an error or the end of the line. If you get an error, ignore the
rest of the line. I strongly suggest two step processing: first,
read a line from your file, then, second, process the line you just
read to extract the individual integers (until the end of the line or
an error which should mean the end of the vector).

To read a line use 'std::geline' function. Then define a
istringstream from the line you just read into a string, and loop
while it's "good". Read an individual int, and if successful, stuff
it into your vector. Once your istringstream is no good, proceed to
reading the next line from the file. Do that until the file has no
more lines.
One addition I would make is to peek to see if the next character to read is
a ! or not. If it wasn't, and you get an error, I would produce some
diagnosis stating I was expecting a number or a !, but I received something
else.

--
Jim Langston
ta*******@rocketmail.com
Jun 27 '08 #4
Martin York wrote:
On May 16, 8:00 am, Christian Hackl <ha...@sbox.tugraz.atwrote:
Now that you've got a std::string that contains the line, use
std::string's member functions to get the substring up to the start of
the comment. Hint: find() and substr() will probably be useful.

That sounds like an awful lot of work when streams will do all that
for you automatically.
Hi everyone, thanks for the replies. How does this look?

inputfile.txt
3 ! Rows
5 ! Column entries
1 2 3 4 5
6 7 8 9 0
1 2 3 4 5

fstream myfile;
myfile.open("inputfile.txt");
string inputline;
string comment_starts("!"); // Comments flagged by "!"
unsigned int offset;
getline(myfile, inputline);
offset = inputline.find(comment_starts); // Find location of comment
inputline = inputline.substr(0,offset); // Trim string
unsigned int rows = atoi(inputline.c_str());

[Repeated for columns. Sorry about using atoi; I thought it would be
OK given that there should be only 1 integer in the first two lines of
the file.]

// Read in the 2D data
int i, j;
int x[columns][rows];
for (j = 0; j < rows; j++)
{for (i = 0; i < columns; i++)
{myfile >x[i][j];} // Line #
}

How is it that the line # correctly reads in the columns and advances
to the next row of the array when the inputfile.txt's line hits a
carriage return? By analogy if we were writing the contents of x[i][j]
out to myfile, we would have to explicitly specify a carriage return,
i.e.:
// Write out the 2D data
for (j = 0; j < rows; j++)
{myfile << "\n";
for (i = 0; i < columns; i++)
{myfile << x[i][j];} // Line #
}

Why is it a bad idea to use arrays? I need to store the 2D data in a
2D array for later manipulation.
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by jamait | last post: by
4 posts views Thread by Amit Maheshwari | last post: by
9 posts views Thread by dba123 | last post: by
6 posts views Thread by Nick | last post: by
2 posts views Thread by rwiegel | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.