Dec 17, 2016 at 8:06pm Dec 17, 2016 at 8:06pm UTC
i have a .txt file with format like this :
Student
1001
25 Bedford St. New York City, N.Y. 10014
Student
1002
125 Maiden Lane, 11th Floor New York, NY 10038
When i get student, i am supposed to store the number and also the address on the next line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
ifstream fin("list.txt" )
string found = "Student" ;
string line, address;
int number;
while (fin >> line)
{
if (line == found)
{
fin >> number;
getline(fin, address);
}
}
although the number is stored, but
getline(fin,address)
cant store the address.. what did i do wrong?
Last edited on Dec 20, 2016 at 5:22pm Dec 20, 2016 at 5:22pm UTC
Dec 17, 2016 at 8:44pm Dec 17, 2016 at 8:44pm UTC
Hello abcjune;
Your line 11 will read and store the number, but it won't remove the newline character from the stream afterwards. Thus, the following getline will read the rest of the line ... which is empty.
I think the 'ignore' statement below will work (100 is just allowing for a few blanks):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string found = "Student" ;
string line, address;
int number;
ifstream fin( "list.txt" );
while (fin >> line)
{
if (line == found)
{
fin >> number; fin.ignore( 100, '\n' );
getline(fin, address);
cout << "Student number: " << number << " Address " << address << endl;
}
}
}
An alternative approach might be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
string found = "Student" ;
string line, address;
string dummy;
int number;
ifstream fin( "list.txt" );
while ( getline( fin, line ) ) // read a line from the file
{
if ( line.find( found) != string::npos ) // check if it contains the text in 'found'
{
stringstream ss( line ); ss >> dummy >> number; // stream this line into a dummy variable and number
getline( fin, address ); // next line should be address
cout << "Student number: " << number << " Address " << address << endl;
}
}
}
Both produce
Student number: 1001 Address 25 Bedford St. New York City, N.Y. 10014
Student number: 1002 Address 125 Maiden Lane, 11th Floor New York, NY 10038
Last edited on Dec 17, 2016 at 8:47pm Dec 17, 2016 at 8:47pm UTC
Dec 18, 2016 at 4:56am Dec 18, 2016 at 4:56am UTC
ohh so all along i was storing the empty spaces next to the student line?
i finally get it!! thanks alot!
Dec 18, 2016 at 12:01pm Dec 18, 2016 at 12:01pm UTC
OP: if the format of the file repeats itself throughout you can set up a (template) struct Student and read into and
print the struct directly. With template the number data-member could be string, double, unsigned int etc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
template <typename T1, typename T2>
struct Student
{
T1 _number;
T2 _address;
};
template <typename T1, typename T2>
std::ostream& operator << (std::ostream& os, const Student<T1, T2>& s)
{
os << "Student number: " << s._number << std::setw(8) << " Address: " << s._address << '\n' ;
return os;
}
int main()
{
std::fstream file("D:\\input.txt" );
using Student_T = Student<double , std::string>;
Student_T s;
std::vector<Student_T> vec;
std::string line;
while (file)
{
std::string line, dummy;
getline(file, line);
std::stringstream stream(line);
stream >>dummy >> s._number;
getline(file,s._address);
if (file)
{
vec.push_back(std::move(s));
}
getline(file, dummy);
}
for (auto & elem : vec)
{
std::cout << elem;
}
}
Last edited on Dec 18, 2016 at 12:06pm Dec 18, 2016 at 12:06pm UTC