Help, read input from file ?

T

tvn007

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.quiz>>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
 
R

Rolf Magnus

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;
 
J

Jim Langston

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.quiz>>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" )
...
}
 
J

Jim Langston

Jim Langston said:
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.quiz>>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 #.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top