File handling problems for struct.

Hi guys. can someone help me? my problem is that i want to put the data i streamed to a file back to the struct but i dont know how to do that. heres my program.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


struct Student{
string name;
int age;
string course;
};

int main()
{
int x;
string data;
// string b;
// getline(cin, b);
// cin.ignore(10000, '\n');

Student b[3];{
for(x=0;x<3;x++){
cout<<"Enter name: ";
getline(cin,b[x].name);
cin.ignore();
cout<<"Enter age: ";
cin>>b[x].age;
cin.ignore();
cout<<"Enter course: ";
getline(cin,b[x].course);}


}

for(x=0;x<3;x++){
cout<<b[x].name<<endl<<b[x].age<<endl<<b[x].course<<endl;

}
//ofstream
for(x=0;x<3;x++)
{
ofstream myfile ("student_data.txt", ios::app);
if (myfile.is_open()){

myfile << b[x].name<<" ";
myfile << b[x].age<<" ";
myfile << b[x].course<<endl;
myfile.close();
}

}
//ifstream

ifstream myfile2;
myfile2.open("student_data.txt");
while(!myfile2.eof()){
myfile2>>data;
cout<< data<<" ";
myfile2>>data;
cout<<data;}

system("pause");
return 0;

}

oh and why is it that when i read from file the last data i get is doubled?
> why is it that when i read from file the last data i get is doubled?

See: https://stackoverflow.com/a/5605159

Something like this, perhaps:
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>

struct student {

    std::string name;
    int age;
    std::string course;
};

// write student info to an output stream (like std::cout or an output file stream)
std::ostream& put_student( std::ostream& stm, const student& s ) {

    return stm << s.name << '\n' // name in the first line
               << s.age << '\n' // age in the second line
               << s.course << '\n' ; // course in the third line ;
}

// read student info from an input stream (like std::cin or an input file stream)
std::istream& get_student( std::istream& stm, student& s ) {

    std::getline( stm, s.name ) ; // read name from the first line
    stm >> s.age // read age from the second line
        >> std::ws ; // skip over the new line to get to the third line
    return std::getline( stm, s.course ) ; // read course from the third line ;
}

int main() {

    student my_students[] { { "bugs bunny", 3, "intro to C++" }, { "woody woodpecker", 1, "C++ 102" } } ;

    {
        // write to an output file
        std::ofstream file( "students.txt" ) ;  // open the file for output
        for( const student& s : my_students) put_student( file, s ) ;
    }

    {
        // read from the file and print to stdout
        std::ifstream file( "students.txt" ) ;  // open the file for input
        student s ;
        while( get_student( file, s ) ) put_student( std::cout, s ) << "-----------\n" ;
    }
}

http://coliru.stacked-crooked.com/a/29a0f67da5ca1c2b
Last edited on
hi. thanks for helping. Im new to programmin so i dont understand a thing about that program. We only covered the basics in class so sorry about that. what is stm? why is there a const before student? and what is the meaning of the parameters on your loops? oh and i tried to run your program but i get an error on line 36 saying "range based 'for' loops are not allowed in C++98 mode".
> what is stm

stm is the name of the variable (of type 'reference to input or output stream').
We can think of it as another name for the stream object.
For example, in the call put_student( file, s ) ;, stm becomes an alias for the input file stream file


> why is there a const before student?

Because the student object need not be a modifiable object; we only want to read from it.


> and what is the meaning of the parameters on your loops?

See: http://www.stroustrup.com/C++11FAQ.html#for


> i tried to run your program but i get an error on line 36 saying "range based 'for' loops are not allowed in C++98 mode".

C++11 (or later) is required for range-based for loops..
Compile with the options -std=c++11 -pedantic-errors
Topic archived. No new replies allowed.