:confused: Datei Zeilenweise einlesen und Zeile durch Leerzeichen in Array splitten



  • Hi Leute,

    ich habe ein Bash-Script geschrieben, in dem ich das relativ einfach machen konnte.
    Nur jetzt muss ich das in C++ Programmieren und habe keine Ahnung wie ich das Anstellen soll.

    Das mit dem Zeilenweise einlesen und wieder ausgeben ging ja noch recht einfach, nur die Zeilen mittels Leerzeichen in ein Array Splitten habe ich so gar keine Idee.

    Bin für jede Hilfe Dankbar.
    gruß Picard



  • meinst du wohl so?

    CString cszTemp = "a b c d e f g", 
               cszItem;
       CStringArray csaItems;
       int iEnd = 0,;
       CString cszItem;
       while (iEnd != -1)
       {
          iEnd = cszTemp.Find(' ');
          cszItem = cszTemp.Mid(0,iEnd);
          csaItems.Add(cszItem);
          cszTemp.Delete(0,iEnd+1);
       }
    

    im CStringArray csaItems sind dann 7 Einträge.

    gruß
    Daniel



  • int main(int argc, char** argv)
    {
    	std::vector<std::string> words;
    
    	std::string textLine = "  abc DEF ghi    blub dings dongs    ";
    	// for all lines of text:
    	// (substitute loop over all lines here instead of this comment)
    	{
    		std::stringstream tempStream(textLine);
    		std::string str;
    		while(tempStream.good())
    		{
    			tempStream >> std::skipws >> str;
    			if(!tempStream.fail())
    				words.push_back(str);
    		}
    	}
    
    	// print the words, each on it's own line, embraced by angle brackets
    	for(std::vector<std::string>::iterator iter = words.begin(); iter != words.end(); iter++)
    		std::cout << "<" << *iter << ">" << std::endl;
    
    	return 0;
    }
    

Anmelden zum Antworten