By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,948 Members | 1,215 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,948 IT Pros & Developers. It's quick & easy.

Help, read input from file ?

P: n/a
Please help,
basicly, I just want to skip "#" when read line from input file.
Below is my code:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

struct Record_info {
string name;
double midterm;
double quiz;
double final;
}student;
int main (void){

double grade;

ifstream in ("test2.txt");
string line,word;
while (getline(in,line)){
if (line.find_first_of("#")) continue; //Just want to skip "#" when
getline
istringstream anyname(line);

anyname>>student.name>>student.midterm>>student.qu iz>>student.final;
cout << student.name <<endl;
cout << student.midterm <<endl;
cout << student.quiz <<endl;
cout << student.final <<endl;
}
return 0;
}

//////////// input file: test2.txt /////////
##############
Tony 90.0 -15.2 98.2
Michael 95.0 17.2 92.2

////////////// output from program ////
##############
0
0
0
////////////////// I would like to have the output as follow ???????
Tony
90.0
-15.2
98.2
Michael
95.0
17.2
92.2

Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
tv****@hotmail.com wrote:
Please help,
basicly, I just want to skip "#" when read line from input file.
Below is my code:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

struct Record_info {
string name;
double midterm;
double quiz;
double final;
}student;
int main (void){

double grade;

ifstream in ("test2.txt");
string line,word;
while (getline(in,line)){
if (line.find_first_of("#")) continue; //Just want to skip "#" when
getline


find_first_of() will return 0 if, and only if the first character of your
string (i.e. the one with index 0) is a '#'. This is only the case for the
first line, so it's the only one that will not be ignored.
And you actaully don't need find_first_of, which looks for one of a list of
characters. Since your "list" only consists of one character, you can
simply use find(). I'm not sure what exactly you want. Do you want to
ignore lines that begin with a '#' or those that contain '#' at any place?
In the former case, try:

if (line.size >= 1 && line[0] == '#') continue;

For the latter, try:

if (line.find('#') != string::npos) continune;

Nov 22 '05 #2

P: n/a
<tv****@hotmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Please help,
basicly, I just want to skip "#" when read line from input file.
Below is my code:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

struct Record_info {
string name;
double midterm;
double quiz;
double final;
}student;
int main (void){

double grade;

ifstream in ("test2.txt");
string line,word;
while (getline(in,line)){
if (line.find_first_of("#")) continue; //Just want to skip "#" when
getline
istringstream anyname(line);

anyname>>student.name>>student.midterm>>student.qu iz>>student.final;
cout << student.name <<endl;
cout << student.midterm <<endl;
cout << student.quiz <<endl;
cout << student.final <<endl;
}
return 0;
}

//////////// input file: test2.txt /////////
##############
Tony 90.0 -15.2 98.2
Michael 95.0 17.2 92.2

////////////// output from program ////
##############
0
0
0
////////////////// I would like to have the output as follow ???????
Tony
90.0
-15.2
98.2
Michael
95.0
17.2
92.2


The way I did it in my code:

std::ifstream InFile(FileName.c_str());
if (InFile.is_open())
{
while (! InFile.eof() )
{
std::string Buffer;
InFile >> Buffer;
while ( Buffer[0] != '#' && ! InFile.eof())
{
std::getline( InFile, Buffer );
InFile >> Buffer;
}
if ( !InFile.eof() )
{
if ( Buffer == "#DAY_AMBIENT" )
...
}
Nov 22 '05 #3

P: n/a
"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:zF******************@fe04.lga...
<tv****@hotmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Please help,
basicly, I just want to skip "#" when read line from input file.
Below is my code:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

struct Record_info {
string name;
double midterm;
double quiz;
double final;
}student;
int main (void){

double grade;

ifstream in ("test2.txt");
string line,word;
while (getline(in,line)){
if (line.find_first_of("#")) continue; //Just want to skip "#" when
getline
istringstream anyname(line);

anyname>>student.name>>student.midterm>>student.qu iz>>student.final;
cout << student.name <<endl;
cout << student.midterm <<endl;
cout << student.quiz <<endl;
cout << student.final <<endl;
}
return 0;
}

//////////// input file: test2.txt /////////
##############
Tony 90.0 -15.2 98.2
Michael 95.0 17.2 92.2

////////////// output from program ////
##############
0
0
0
////////////////// I would like to have the output as follow ???????
Tony
90.0
-15.2
98.2
Michael
95.0
17.2
92.2


The way I did it in my code:

std::ifstream InFile(FileName.c_str());
if (InFile.is_open())
{
while (! InFile.eof() )
{
std::string Buffer;
InFile >> Buffer;
while ( Buffer[0] != '#' && ! InFile.eof())
{
std::getline( InFile, Buffer );
InFile >> Buffer;
}
if ( !InFile.eof() )
{
if ( Buffer == "#DAY_AMBIENT" )
...
}


Oooops, mine does the opposite. Mine ignores any line NOT starting with #.
Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.