Hello !

I have array of struct and i want
1] when i run the program to load the data from it.
2] when i make changes to be saved in the text file the same time or when i quit the program

I tried to put data in txt file with the following format but i can only read until the space. I do a lot of search but wasn`t able to find what i look for. Mainly i don`t know how to read strings

A1 false Alexa Trina
A2 falseGeorge Ali
A3 false Comina Riviera

My sample code is:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class classroom
{
public:
    string studentClass;
    bool studentBonus;
    string studentName;

    void classroom::createStudent(string sClass, bool bonus, string name);
};

void classroom::createStudent(string sClass, bool bonus, string name)
{
    studentClass=sClass;
    studentBonus=bonus;
    studentName=name;
}

int main()
{
    //number of students
    const int numstudents=10;
    
    //create structure
    classroom student[numstudents];

    //initialize variables
    student[0].createStudent("A1",false,"Alexa Trina");
    student[1].createStudent("A2",false,"George Ali");
    student[2].createStudent("A3",false,"Comina Riviera");

    return 0;//indicate that program end succesfully
}//end main

Thanks for your time!

fstream file;
file.open("foo.bar","r");
string str;
while(getline(file,str)) //Read lines until getline() returns NULL at EOF.
{
   //Read a line
   //Do something
}
file.close();

Yeah but i want to read 3 variables in the same line seperated by space and after do the same for the next line

fstream file;
file.open("foo.bar","r");
string str;
while(getline(file,str)) //Read lines until getline() returns NULL at EOF.
{
   //Read a line
   //Do something
   string var1,var2,var3;
   stringstream sStream(str);
   sStream>>var1>>var2>>var3;   
}
file.close();
This article has been dead for over six months. Start a new discussion instead.