Einlesen aus .txt

rojeroje

Mitglied
hi leute hab ein problem und brauche hilfe!

ich benutze folgenden Code:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  size_t found;
  size_t pos;
  string str,str1;
  string::iterator it;
  ifstream myfile ("Test.txt");
  if (myfile.is_open())
  {   
    
    while (! myfile.eof() )
    {
   
      getline (myfile,line);
      pos = line.find(",");
      if(pos==string::npos)
         continue;
      it = line.begin()+pos;
      line.erase(line.begin()+pos, line.end());
      cout <<line << endl;
    }
    myfile.close();
    getchar();
  }

  else cout << "Unable to open file"; 
getchar();

  return 0;

in der Test.txt steht folgendes:

Thomas Keller,Musterweg.6,Tel: 030 111111
Gabi Hammel,Musterweg.7,Tel 030 111111
usw....

Problem:
er gibt nur die namen aus vor dem ersten Komma ( , ). (Thomas Keller, Gabi Hammel)

Frage:
wie kann ich bestimmen das er nach dem ersten Komma lesen soll bis zum nächsten Komma? also Zeile1 zweites Wort (Musterweg.6)

z.b für if also wenn über cin eingabe Thomas Keller = Musterweg.6 usw...
 
ich korrigier das direkt mal ...
C++:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

int main () 
{
    std::ifstream file_stream("est.txt");
    if (!file_stream) return 1;

    std::string line;
    while (std::getline(file_stream, line))
    {
        std::ostringstream ss(line);
        std::string first_info;
        std::getline(ss, first_info, ',');
        std::string second_info;
        std::getline(ss, second_info, ',');
        std::cout << first_info  << " at  " << second_info << std::endl;
    }
    std::cin.get();
}
 
Vielen Dank, aber klappt nicht ganz.............


Code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
 
int main ()
{
    std::ifstream file_stream("est.txt");
    if (!file_stream) return 1;
 
    std::string line;
    while (std::getline(file_stream, line))
    {
        std::ostringstream ss(line);
        std::string first_info;
        std::getline(ss, first_info, ',');
        std::string second_info;
        std::getline(ss, second_info, ',');
        std::cout << first_info  << " at  " << second_info << std::endl;
    }
    std::cin.get();
}

ich benutze Dev,
Fehlermeldung:

In function `int main()':
16: error: no matching function for call to `getline(std::eek:stringstream&, std::string&, char)'
18: error: no matching function for call to `getline(std::eek:stringstream&, std::string&, char)'
 
Zurück