• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Problem z kodem - projekt z plikiem tekstowym

Cloud VPS
0 głosów
270 wizyt
pytanie zadane 12 października 2019 w C i C++ przez amelia.cpp Obywatel (1,860 p.)

Cześć, co jest nie tak z tym kodem?

		    file.open("mainFile.txt");
			string str;
			if (file.is_open()) {
				while (getline(file, str))
				{
					stringstream ss(str);
					string name, surname, trade;
					float salary;
					getline(ss, name, ' ');
					getline(ss, surname, ':');
					getline(ss, trade, '-');
					ss >> salary;
					createSimpleEmployer(name, surname, trade, salary);
				}
				file.close();
			}

Główny problem jest taki że kod w while się w ogóle nie wykonuje :(

Plik ma taki format:

Imię Nazwisko:xxxx-2987

 

komentarz 12 października 2019 przez tkz Nałogowiec (42,040 p.)

Nie widzę reszty kodu, ale z tego co masz to działa. 

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

using namespace std;
int main() 
{
ifstream file;
file.open("mainFile.txt");
string str;
if (file.is_open()) {
    while (getline(file, str))
    {
        stringstream ss(str);
        string name, surname, trade;
        float salary;
        getline(ss, name, ' ');
        getline(ss, surname, ':');
        getline(ss, trade, '-');
        ss >> salary;
        cout<<name<<' '<<surname<<' '<<trade;
        //createSimpleEmployer(name, surname, trade, salary);
    }
    file.close();
}
}

Jedyny błąd jaki mogłaś zrobić to ustawić ofstream, zamiast ifstream. 

komentarz 12 października 2019 przez amelia.cpp Obywatel (1,860 p.)

cała funkcja wygląda tak

	void getEmployeesFromFile(ifstream& file)
	{
		    file.open("mainFile.txt", ios::in);
			string str;
			if (file.is_open()) {
				while (getline(file, str))
				{ 
					stringstream ss(str);
					string name, surname, trade;
					float salary;
					getline(ss, name, ' ');
					getline(ss, surname, ':');
					getline(ss, trade, '-');
					ss >> salary;
					createSimpleEmployer(name, surname, trade, salary);
				}
				file.close();
			}
	}

 

komentarz 12 października 2019 przez amelia.cpp Obywatel (1,860 p.)

@tkz, zamiast przekazywać ifstream jako argument, to poprostu zadeklarowałam go w ciele funkcji i działa, dzięki za pomoc ;)

komentarz 12 października 2019 przez tkz Nałogowiec (42,040 p.)
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
void foo(ifstream &);

int main()
{
    ifstream file("mainFile.txt");
    foo(file);
}

void foo(ifstream &file)
{
    
    string str;
    if (file.is_open())
    {
        while (getline(file, str))
        {
            stringstream ss(str);
            string name, surname, trade;
            float salary;
            getline(ss, name, ' ');
            getline(ss, surname, ':');
            getline(ss, trade, '-');
            ss >> salary;
            cout << name << ' ' << surname << ' ' << trade;
            //createSimpleEmployer(name, surname, trade, salary);
        }
        file.close();
    }
}

Tylko, że to też powinno działać. 

1 odpowiedź

0 głosów
odpowiedź 12 października 2019 przez j23 Mędrzec (195,240 p.)
void getEmployeesFromFile()
{
    ifstream file("mainFile.txt");
    string str, name, surname, trade;
    float salary;

    while (getline(file, str)) {
        istringstream ss(str);
        if (getline(ss, name, ' ') && getline(ss, surname, ':') && getline(ss, trade, '-') && ss >> salary)
            createSimpleEmployer(name, surname, trade, salary);
    }
}

Nie wiem, po co przekazujesz file w parametrze, skoro i tak używasz ten strumień jakby był lokalny.

Podobne pytania

0 głosów
1 odpowiedź 1,347 wizyt
+1 głos
2 odpowiedzi 520 wizyt
pytanie zadane 27 stycznia 2016 w C i C++ przez Kimi Bywalec (2,050 p.)
0 głosów
0 odpowiedzi 1,229 wizyt
pytanie zadane 27 maja 2020 w C i C++ przez pozdro600 Początkujący (260 p.)

93,484 zapytań

142,417 odpowiedzi

322,763 komentarzy

62,895 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj

Kursy INF.02 i INF.03
...