<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Tabs und führende Leerzeichen eines std::string löschen]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich lese eine Datei in einen ifstream ein übergebe dann jede fileline als string an eine Funktion, um Kommentare zu löschen.<br />
Das ganze funktioniert leider bisher suboptimal bis gar nicht. Es kompiliert zwar und läuft auch durch, in cout sieht man noch alle Tabs und Leerzeichen. Mein Code ist:</p>
<pre><code class="language-cpp">#include &lt;boost/algorithm/string/predicate.hpp&gt;
#include &lt;boost/lexical_cast.hpp&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;

ifstream file (&quot;path to file&quot;);
if (file.is_open()) {
    std::cout &lt;&lt; &quot;file opened correctly&quot; &lt;&lt; std::endl;
  } else {
    std::cerr &lt;&lt; &quot;error while opening file&quot; &lt;&lt; std::endl; return 0;
  }

while(!file.eof()){
    	  std::string fileline;
    	  getline(file, fileline);
    	  cout&lt;&lt;fileline&lt;&lt;endl;
    	  fileline.erase(remove(fileline.begin(), fileline.end(), '\t'), fileline.end());
    	  cout&lt;&lt;fileline&lt;&lt;endl;
    	  trim(fileline);
    	  cout&lt;&lt;fileline&lt;&lt;endl;
    	  if(boost::starts_with(fileline, &quot;//&quot;)){
//    	  std::string comm_test = &quot;//&quot;;
//    	  if(fileline.compare(0, comm_test.length(), comm_test) == 0) {
    		  std::cout &lt;&lt; &quot;Comment seen and ignored&quot; &lt;&lt; std::endl;
    	  } 
      }

void trim(string&amp; str)
	{
	  string::size_type pos = str.find_last_not_of(' ');
	  if(pos != string::npos) {
	    str.erase(pos + 1);
	    pos = str.find_first_not_of(' ');
	    if(pos != string::npos) str.erase(0, pos);
	  }
	  else str.erase(str.begin(), str.end());
	}
</code></pre>
<p>Kann mir jemand sagen, warum das so gar nicht funktioniert?</p>
<p>Herzlichen Dank im Vorraus!!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/303466/tabs-und-führende-leerzeichen-eines-std-string-löschen</link><generator>RSS for Node</generator><lastBuildDate>Mon, 10 Aug 2026 11:57:32 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/303466.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 14 May 2012 19:58:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Tabs und führende Leerzeichen eines std::string löschen on Mon, 14 May 2012 19:58:00 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich lese eine Datei in einen ifstream ein übergebe dann jede fileline als string an eine Funktion, um Kommentare zu löschen.<br />
Das ganze funktioniert leider bisher suboptimal bis gar nicht. Es kompiliert zwar und läuft auch durch, in cout sieht man noch alle Tabs und Leerzeichen. Mein Code ist:</p>
<pre><code class="language-cpp">#include &lt;boost/algorithm/string/predicate.hpp&gt;
#include &lt;boost/lexical_cast.hpp&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;

ifstream file (&quot;path to file&quot;);
if (file.is_open()) {
    std::cout &lt;&lt; &quot;file opened correctly&quot; &lt;&lt; std::endl;
  } else {
    std::cerr &lt;&lt; &quot;error while opening file&quot; &lt;&lt; std::endl; return 0;
  }

while(!file.eof()){
    	  std::string fileline;
    	  getline(file, fileline);
    	  cout&lt;&lt;fileline&lt;&lt;endl;
    	  fileline.erase(remove(fileline.begin(), fileline.end(), '\t'), fileline.end());
    	  cout&lt;&lt;fileline&lt;&lt;endl;
    	  trim(fileline);
    	  cout&lt;&lt;fileline&lt;&lt;endl;
    	  if(boost::starts_with(fileline, &quot;//&quot;)){
//    	  std::string comm_test = &quot;//&quot;;
//    	  if(fileline.compare(0, comm_test.length(), comm_test) == 0) {
    		  std::cout &lt;&lt; &quot;Comment seen and ignored&quot; &lt;&lt; std::endl;
    	  } 
      }

void trim(string&amp; str)
	{
	  string::size_type pos = str.find_last_not_of(' ');
	  if(pos != string::npos) {
	    str.erase(pos + 1);
	    pos = str.find_first_not_of(' ');
	    if(pos != string::npos) str.erase(0, pos);
	  }
	  else str.erase(str.begin(), str.end());
	}
</code></pre>
<p>Kann mir jemand sagen, warum das so gar nicht funktioniert?</p>
<p>Herzlichen Dank im Vorraus!!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2211506</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2211506</guid><dc:creator><![CDATA[JRS]]></dc:creator><pubDate>Mon, 14 May 2012 19:58:00 GMT</pubDate></item><item><title><![CDATA[Reply to Tabs und führende Leerzeichen eines std::string löschen on Mon, 14 May 2012 20:12:29 GMT]]></title><description><![CDATA[<p>Ich habe zwar keine Ahnung was das da werden soll, aber führende Leerzeichen kannste mit</p>
<pre><code class="language-cpp">#include &lt;cctype&gt;
#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

void remove_leading_spaces(std::string&amp; s)
{
  auto pred = static_cast&lt;int (*) (int)&gt;(std::isspace);
  auto i = std::find_if_not(s.begin(), s.end(), pred);
  s.erase(s.begin(), i);
}

int main()
{
  std::string s = &quot;    \t\t   Hallo, Welt!&quot;;
  remove_leading_spaces(s);
  std::cout &lt;&lt; s &lt;&lt; '\n';
}
</code></pre>
<p>löschen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2211517</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2211517</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Mon, 14 May 2012 20:12:29 GMT</pubDate></item><item><title><![CDATA[Reply to Tabs und führende Leerzeichen eines std::string löschen on Mon, 14 May 2012 20:10:51 GMT]]></title><description><![CDATA[<p>cooky451 schrieb:</p>
<blockquote>
<p>GCC</p>
</blockquote>
<p>Du verwendest GCC? Seit wann? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2211518</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2211518</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 14 May 2012 20:10:51 GMT</pubDate></item><item><title><![CDATA[Reply to Tabs und führende Leerzeichen eines std::string löschen on Mon, 14 May 2012 20:13:51 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>cooky451 schrieb:</p>
<blockquote>
<p>GCC</p>
</blockquote>
<p>Du verwendest GCC? Seit wann? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
</blockquote>
<p>Seit ich Ideone für solche Kleinigkeiten nutze. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2211523</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2211523</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Mon, 14 May 2012 20:13:51 GMT</pubDate></item><item><title><![CDATA[Reply to Tabs und führende Leerzeichen eines std::string löschen on Mon, 14 May 2012 20:31:09 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19400">@JRS</a><br />
Wenn do sowieso schon die Boost verwendest, wieso dann nicht gleich auch <code>trim</code> (bzw. <code>trim_left</code> ) aus der Boost?</p>
<p><a href="http://www.boost.org/doc/libs/1_49_0/doc/html/string_algo/reference.html#header.boost.algorithm.string.trim_hpp" rel="nofollow">http://www.boost.org/doc/libs/1_49_0/doc/html/string_algo/reference.html#header.boost.algorithm.string.trim_hpp</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2211539</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2211539</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 14 May 2012 20:31:09 GMT</pubDate></item><item><title><![CDATA[Reply to Tabs und führende Leerzeichen eines std::string löschen on Mon, 14 May 2012 21:54:29 GMT]]></title><description><![CDATA[<p>Hallo nochmal,</p>
<p>danke für die super schnelle Antwort.<br />
Also, was ich vor habe:</p>
<pre><code class="language-cpp">ifstream file;
remove_comments(file);
</code></pre>
<p>Hierbei sollen alle Zeilen, die mit &quot;//&quot; anfangen aus dem stream gelöscht werden. Das will ich eigentlich über eine If Anfrage machen</p>
<pre><code class="language-cpp">while(!file.eof){
iostream processed;
getline(file, fileline);
if(boost::starts_with(fileline, &quot;//&quot;)){
//do nothing
} else {
processed &lt;&lt; fileline;
}
}
</code></pre>
<p>Aber die Abfrage funktioniert bei mir nicht. Daher dachte ich, den string erst zu trimmen. Das ging dann mit den Whitespaces ganz gut, aber anscheinend nicht mit dem Tabs.</p>
<p>Naja ich versuche es mal mit boost::trim</p>
<p>Danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2211606</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2211606</guid><dc:creator><![CDATA[JRS]]></dc:creator><pubDate>Mon, 14 May 2012 21:54:29 GMT</pubDate></item></channel></rss>