473,386 Members | 1,842 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,386 software developers and data experts.

skipping line while reading data file

I would like know how I can skip a line while reading
a set of input data (from a text file) if the first character
of the line is "#".

My original code reads:

ifstream Infile("data.dat");

for( int i=0; i<N; i++){
Infile >x[i] >y[i];
}

Sep 4 '06 #1
3 5506
In article <11**********************@74g2000cwt.googlegroups. com>,
da*******@gmail.com says...
I would like know how I can skip a line while reading
a set of input data (from a text file) if the first character
of the line is "#".

My original code reads:

ifstream Infile("data.dat");

for( int i=0; i<N; i++){
Infile >x[i] >y[i];
}
One obvious possibility would be something like this:

std::istream &my_getline(std::istream &is, std::string &s) {
while (std::getline(is, s) && s[0] == '#')
;
return is;
}

for (int i=0; i<N; i++) {
std::string line;
if (!my_getline(Infile, line))
break;
std::stringstream temp(line);
temp >x[i] >y[i];
}

That does raise one minor question: you're starting with a count (N) of
lines to read. Is that count supposed to include the commented lines or
not? The code above attempts to read N lines of actual data -- if you
want a total of N lines, whether they contain data or not, you'd have to
rewrite it a bit.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 4 '06 #2
Thanks. I have a further question..
Jerry Coffin wrote:
One obvious possibility would be something like this:

std::istream &my_getline(std::istream &is, std::string &s) {
while (std::getline(is, s) && s[0] == '#')
;
return is;
}

for (int i=0; i<N; i++) {
std::string line;
if (!my_getline(Infile, line))
break;
Is the if-statement for checking if the Infile does exit?
std::stringstream temp(line);
temp >x[i] >y[i];
}
Sep 5 '06 #3
In article <11**********************@h48g2000cwc.googlegroups .com>,
da*******@gmail.com says...

[ ... ]
for (int i=0; i<N; i++) {
std::string line;
if (!my_getline(Infile, line))
break;

Is the if-statement for checking if the Infile does exit?
It's mostly to check whether we've reached the end of the file (for
example, if the file got corrupted and only contained one line).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: JackM | last post by:
I'm having a brain freeze on this. I have a script where I am reading the contents of a file into an array. I have that up and working with no problem. Then I'm exploding the array using while...
1
by: Rick | last post by:
After being frustrated with this issue on several occasions I think I found the secret sauce for solving the issue after reading a few different messages about what others thought and trying a...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
6
by: lisa.engblom | last post by:
Hi, I've just started programming in python, and have run into an unexpected problem. I am using python to pull text data from some csv files. I have one file that has the important...
10
blazedaces
by: blazedaces | last post by:
Alright guys, so the title explains exactly my goal. The truth is I'm going to be reading in a lot of data from an xml file. The file is too large and there's too much data to store in arraylists...
11
by: cindyfaith | last post by:
I have one question regarding a code I wrote to parse a data file My code looks like this open($FILE, "C:\\data\\NCI sample.txt") or die "oh oh can't open"; open(OUT,...
7
by: Gustaf | last post by:
Hi all, Just for fun, I'm working on a script to count the number of lines in source files. Some lines are auto-generated (by the IDE) and shouldn't be counted. The auto-generated part of files...
0
by: Jerry Coffin | last post by:
In article <4fae62b0-6858-4e9e-830e-9eecf6691d4a@ 59g2000hsb.googlegroups.com>, friend.blah@googlemail.com says... Each time you read from the file, keep track of the file position after...
4
by: BibI | last post by:
Hi there, I just started programming with PERL and am trying to put together my first little data manipulation program. I am working on a MAC with OSX. I have a data file with the following...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.