gubie dane z pliku

0

w pliku "Students.txt znajdują się np dane

Adam Pierwszy Człowiek 0
Ewa Kobieta 2

do vector'a którego typem jest struktura nie wczytuje się ostatnia linijka, lub jest nadpisywana
nie wiem też, czy dobrze ustawiłem flagi

std::ifstream infile("Students.txt", ios::out);
std::ofstream outfile("Tmp_Students.txt", ios::trunc);

poniżej listing

#include <iostream>
#include <iomanip>
#include <limits>       // std::numeric_limits
#include <cstring>      // strlen
#include <fstream>
#include <string>
#include <vector>

using namespace std;

struct Student
{
    std::string firstName;
    std::string lastName;
    int luckyNumber;
};

std::istream& operator>>(std::istream& is, Student& s)
{
    return is >> s.firstName >> s.lastName >> s.luckyNumber;
}
std::ostream& operator<<(std::ostream& os, Student& s )
{
    os << s.firstName  << " " << s.lastName << " " << s.luckyNumber << "\n";
    return os;
}

int main()
{
    std::vector<Student> students;
    Student new_students;
    int how_much, luckyNumber;
    string firstName, lastName;

    std::ifstream infile("Students.txt", ios::out);
    std::ofstream outfile("Tmp_Students.txt", ios::trunc); 

    infile.seekg(0, ios::beg);
    while (infile >> new_students) //|| !infile.eof()
    {
        students.push_back(new_students);
    }

    cout << "\tStudents " << students.size() << endl;
    for(auto el: students)
    {
        cout << el.firstName << " " << el.lastName << " " << el.luckyNumber << endl;
    }

    cout << "\nHow many students you want to add  : ";
    cin >> how_much;
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');

    for(int i = 0; i < how_much; ++i)
    {
        cout << "Data for " << i+1 << " student \n";
        cout << "your name : ";
        //cin >> firstName;
        getline(cin, firstName); //o.k.
        cout << "your last name  : ";
        //cin >> lastName;
        getline(cin, lastName); //o.k.
        cout << "Enter the lucky number  : ";
        cin >> luckyNumber;
        cout << "\nyour data : " << firstName << " " << lastName << " " << luckyNumber << "\n\n";
        new_students = { firstName, lastName, luckyNumber };
        students.push_back( new_students );
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    }

    cout << "\n\noutfile " << endl;
    outfile.seekp(0, ios::beg);
    for(auto el: students)
    {
        cout << el ;
        new_students = el;
        (outfile << new_students );
    }

    cout << "\n";

    infile.close();
    outfile.close();

    remove("Students.txt");
    rename("Tmp_Students.txt","Students.txt");

    return 0;
}

0

std::ifstream infile("Students.txt", ios::out);
zdecyduj się czy in czy out ;)

2

Adam Pierwszy Człowiek 0 od kiedy "Człowiek" wczyta się do inta? Masz świadomość, że Twoje wczytywanie separuje na whitespace'ach? Ewa wczyta się ok, ale z takimi danymi jakimi podałeś masz od razu błąd wczytywania i program leci dalej z pustym wektorem.

0

Coś a'la CSV, czyli np. z jakimś separatorem jak wspomniałeś wyżej, potem getline i split na separatorze? Nie wiem co dokładnie chcesz zrobić ale taka opcja wydaje się mieć sens ;)

1
std::ofstream outfile("Tmp_Students.txt", ios::trunc|ios::in|ios::out);
...
outfile.clear(ios::goodbit);
outfile.seekp(0,ios::beg);
...
0

Napisałem sobie na boku taki podprogram do dzielenia stringa, ale dalej gubię tekst

#include <iostream>
#include <cstring>
#include <algorithm>
#include <limits>       //std::numeric_limits
#include <vector>       //std::vector
#include <sstream>      //std::stringstream

using namespace std;

int main()
{
    vector < string > sliceText;
    cout << "vector size : " << sliceText.size() << "\n";
    std::string sWiersz = "Dzieki | radom | droga | jest| dluga, | dzieki | przykladom| – krotka| i |skuteczna"; //specjalnie tak ustawiam pipe |
    string slice;
    std::stringstream ss(sWiersz);
    while( (std::getline(ss,sWiersz,'|')) )
    {
        std::getline(ss,slice,'|');
        //cout << slice << "\n"; // for check
        sliceText.push_back(slice);
    };
    cout << "vector size : " << sliceText.size() << "\n";
    for(auto el: sliceText)
    {
        cout << el << "\n";
    }

    return 0;
}

0

Powinno być:


  ...

    while ((std::getline(ss, slice, '|')))
    {
        sliceText.push_back(slice);
    }
...

0

@robert Karpiński Nie, ale thx za dobre chęci
poprawiłem sobie

while( (std::getline(ss,sWiersz,'|')) )
    {
        sliceText.push_back(sWiersz); //add line
        std::getline(ss,slice,'|');
        //cout << slice << "\n"; // for check
        sliceText.push_back(slice);
    };

//edit
Dobra temat do zamknięcia rozkminiłem tego "getline'a i split'a z separatorem" i szpila wszystko tak jak chciałem.

1 użytkowników online, w tym zalogowanych: 0, gości: 1