ein einziges wort von einem file zu lesen
-
Hallo ich lerne jetyt grade C++ , und ich habe es grade geschafft mit die klasse fstream , in einem file zu schreiben und von dem zu lesen ,
ales funktioniert aber jetzt dass ich eine ganze Zeile lese !!
kann ich nicht ein wort lese also bis " " und es in einem String speichern ??
bei mir sieht der Code grade so
ifstream myfile ("example.txt"); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file";kann mir vielleicht jemand helfen ?
Danke im Voraus
Eure eliaa
-
std::string wort; myfile >> wort;
-
Die STL bietet auch eine schöne Möglichkeit eine Datei in Wörter auseinanderzunehmen:
char *infile = argv[1]; char *outfile = argv[2]; list<string> words; string word; ifstream ifs; ifs.open( infile); if (! ifs) { cerr << "cannot open file " << infile << " for input\n"; exit(1); } while (ifs >> word) { words.push_back( word); } if (! ifs.eof()) { cerr << "error while reading from file " << infile << endl; exit(1); } ifs.close(); words.sort(); list<string>::iterator p = unique( words.begin(), words.end()); words.erase(p, words.end()); ofstream ofs; ofs.open( outfile); if (! ofs) { cerr << "cannot open file " << outfile << " for output\n"; exit(1); } list<string>::iterator iword; for (iword = words.begin(); iword != words.end(); ++iword) { ofs << *iword << endl; }