473,287 Members | 3,295 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 3723
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jamait | last post by:
Hi all, I m trying to read in a text file into a datatable... Not sure on how to split up the information though, regex or substrings...? sample: Col1 Col2 ...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
9
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
6
by: Nick | last post by:
Hiya, Am having a few problems with StreamReader. I'm trying to open a text file, and read 5 columns, and put them into an array, and then do a calculation and read the next line of data, and...
2
by: rwiegel | last post by:
I'm trying to read rows from an Excel file and display them in an ASP.NET DataGridview. I am using C# for the code file. I am using OleDb to read from the Excel file. The columns that contain...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
6
by: =?Utf-8?B?UmljaA==?= | last post by:
'--this code works but only reads text into one column when contains multiple cols Dim ds1x As New DataSet Dim ConStr As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;Data...
3
by: jasvinder singh | last post by:
Respected Sir/madam, Can you help in providing code in 'C' for Reading text file with n number of rows and columns and putting the result in arrays.The sample file is as follows: rim_label =...
4
by: Miner Jeff | last post by:
I've written some code that reads a text file. I have a requirement that the text file also be in a format that's easy to read when it's printed. In a prior project, I had used some spreadsheet...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.