<?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[Länge eines Strings bestimmen]]></title><description><![CDATA[<pre><code>#include &lt;math.h&gt;
#include &lt;conio.h&gt; //getch();
#include &lt;cstring&gt;
#include &lt;fstream&gt;
#include &lt;iostream&gt;

using namespace std;

    int a=0;
    fstream rf,wf;
    char name[15];

int txtcsv(string=name)
{
    int v=0,len;
    //len=std::string::size()
    len=name.length();
    name.erase(len-4,3);
    cout&lt;&lt;name&lt;&lt;endl;
    if((strcmp(name,&quot;go.txt&quot;))==0)
    {v=1;}
    return v;
    }
</code></pre>
<p>hallo,</p>
<p>ich habe hier ein Problem mit len=name.length(), ich frag mich warum dies nicht funktioniert? Weil ich die stringlänge 15 schon definiert habe?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/223890/länge-eines-strings-bestimmen</link><generator>RSS for Node</generator><lastBuildDate>Sat, 03 Oct 2026 12:53:23 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/223890.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 02 Oct 2008 08:49:57 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Länge eines Strings bestimmen on Thu, 02 Oct 2008 08:49:57 GMT]]></title><description><![CDATA[<pre><code>#include &lt;math.h&gt;
#include &lt;conio.h&gt; //getch();
#include &lt;cstring&gt;
#include &lt;fstream&gt;
#include &lt;iostream&gt;

using namespace std;

    int a=0;
    fstream rf,wf;
    char name[15];

int txtcsv(string=name)
{
    int v=0,len;
    //len=std::string::size()
    len=name.length();
    name.erase(len-4,3);
    cout&lt;&lt;name&lt;&lt;endl;
    if((strcmp(name,&quot;go.txt&quot;))==0)
    {v=1;}
    return v;
    }
</code></pre>
<p>hallo,</p>
<p>ich habe hier ein Problem mit len=name.length(), ich frag mich warum dies nicht funktioniert? Weil ich die stringlänge 15 schon definiert habe?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1591939</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1591939</guid><dc:creator><![CDATA[Gustl]]></dc:creator><pubDate>Thu, 02 Oct 2008 08:49:57 GMT</pubDate></item><item><title><![CDATA[Reply to Länge eines Strings bestimmen on Thu, 02 Oct 2008 09:01:34 GMT]]></title><description><![CDATA[<p>Gustl schrieb:</p>
<blockquote>
<pre><code>char name[15];
</code></pre>
</blockquote>
<p>Hi, das liegt einfach daran, dass Du keinen std::string benutzt, sondern ein char Array. versuchs mal mit</p>
<pre><code>std::string name;
</code></pre>
<p>grüße</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1591950</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1591950</guid><dc:creator><![CDATA[Con*n]]></dc:creator><pubDate>Thu, 02 Oct 2008 09:01:34 GMT</pubDate></item><item><title><![CDATA[Reply to Länge eines Strings bestimmen on Thu, 02 Oct 2008 09:07:44 GMT]]></title><description><![CDATA[<p>Häää... dein Code ist völlig wirr..</p>
<p>- erstmal was soll das ganze eigentlich machen ??<br />
- wenn du std::string benutzen willst musst du es auch inkluden ( #include &lt;string&gt; )<br />
- dein funktionskopf ist irgendwie falsch... mach mal das &quot;=&quot; weg..<br />
- es ist sehr verwirrend, dass du sowohl &quot;name&quot; als parameter(string) hast als auch als globales char-array<br />
- was willst du bei dieser if-abfrage vergleichen ? &quot;char name[15]&quot; oder &quot;(string name)<br />
-&gt; zur zeit wird char name[15] verglichen<br />
-&gt; zum vergleichen des strings machst du</p>
<pre><code class="language-cpp">if((strcmp(name.c_str(),&quot;go.txt&quot;))==0)

// oder

if(name == &quot;go.txt&quot;)
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1591954</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1591954</guid><dc:creator><![CDATA[DataByte]]></dc:creator><pubDate>Thu, 02 Oct 2008 09:07:44 GMT</pubDate></item><item><title><![CDATA[Reply to Länge eines Strings bestimmen on Thu, 02 Oct 2008 09:09:53 GMT]]></title><description><![CDATA[<p>Anfänger?<br />
Du mischst hier wie wild C++ und C und das geht in den seltesten Fällen wirklich gut.</p>
<p>Gustl schrieb:</p>
<blockquote>
<pre><code>#include &lt;math.h&gt;
#include &lt;conio.h&gt; //getch();
#include &lt;cstring&gt;
#include &lt;fstream&gt;
#include &lt;iostream&gt;

using namespace std;

    // warum globale Variablen?
    int a=0;
    fstream rf,wf;
    char name[15];

// Warum gibt es ein Default Paramter und dafür keinen Namen für die Variable?
// Was soll der Rückgabewert für eine Aufgabe erfüllen.
// Es riecht danach, daß bool der bessere Typ wäre.    
int txtcsv(string=name)
{
    int v=0,len;
    //len=std::string::size()
    len=name.length();
    name.erase(len-4,3);
    cout&lt;&lt;name&lt;&lt;endl;
    // Warum strcmp?
    // if (&quot;go.txt&quot; == name) tut es bei einem std::string auch
    if((strcmp(name,&quot;go.txt&quot;))==0)
    {v=1;}
    return v;
    }
</code></pre>
</blockquote>
<p>Versuchen wir mal das Ganze abzurunden</p>
<pre><code>#include &lt;cmath&gt;
#include &lt;conio.h&gt; //getch();
#include &lt;string&gt;
#include &lt;fstream&gt;
#include &lt;iostream&gt;

using namespace std;

bool txtcsv(std::string name) {
    std::string::size_type len (name.length());

    name.erase(len-4,3);

    cout &lt;&lt; name &lt;&lt; endl;

    if (&quot;go.txt&quot; == name) {
        return true;
    }

    return false;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1591957</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1591957</guid><dc:creator><![CDATA[*john 0]]></dc:creator><pubDate>Thu, 02 Oct 2008 09:09:53 GMT</pubDate></item><item><title><![CDATA[Reply to Länge eines Strings bestimmen on Thu, 02 Oct 2008 09:23:03 GMT]]></title><description><![CDATA[<p>Danke, ja ich bin Anfänger, haben letztes Jahr die Grundlagen C gerlent und nun C++ und haben in der Schule eine Übung gemacht die ich noch etwas ausbauen will.</p>
<p>Hier der Code:</p>
<pre><code>#include &lt;math.h&gt;
#include &lt;conio.h&gt; //getch();
#include &lt;cstring&gt;
#include &lt;fstream&gt;
#include &lt;iostream&gt;

using namespace std;

    int a=0;
    fstream rf,wf;
    std::string name;

int txtcsv(string=name)
{
    int v=0,len;
    //len=std::string::size()
    len=name.length();
    //std::distance(name.begin(), name.end());
    name.erase(len-4,3);
    cout&lt;&lt;name&lt;&lt;endl;
    //if((strcmp(name,&quot;go.txt&quot;))==0)
    {v=1;}
    return v;
    }

void schreiben()  //schreibt die Tablelle in die Datei
{
    wf.open(name,ios::out);
    wf&lt;&lt;&quot;i.;i;i*i;sqrt(f);log(d)&quot;&lt;&lt;endl;   
    wf&lt;&lt;endl;
    for(int i=1;i&lt;=100;i++)
    {
    float f=i;
    double d=i;    
    wf&lt;&lt;i&lt;&lt;&quot;.;&quot;&lt;&lt;i&lt;&lt;&quot;;&quot;&lt;&lt;i*i&lt;&lt;&quot;;&quot;&lt;&lt;sqrt(f)&lt;&lt;&quot;;&quot;&lt;&lt;log(d)&lt;&lt;endl;                
    }
    /*wf&lt;&lt;setw(8)&lt;&lt;&quot;i&quot;&lt;&lt;setw(8)&lt;&lt;&quot;i&quot;&lt;&lt;setw(8)&lt;&lt;&quot;i*i&quot;&lt;&lt;setw(11)&lt;&lt;setprecision(3)&lt;&lt;setiosflags(ios::fixed)&lt;&lt;&quot;sqrt(f)&quot;&lt;&lt;setw(11)&lt;&lt;&quot;log(d)&quot;&lt;&lt;endl;   
    wf&lt;&lt;endl;
    for(int i=1;i&lt;=100;i++)
    {
    float f=i;
    double d=i;    
    wf&lt;&lt;setw(7)&lt;&lt;i&lt;&lt;setw(1)&lt;&lt;&quot;.&quot;&lt;&lt;setw(8)&lt;&lt;i&lt;&lt;setw(8)&lt;&lt;i*i&lt;&lt;setw(11)&lt;&lt;setprecision(3)&lt;&lt;setiosflags(ios::fixed)&lt;&lt;sqrt(f)&lt;&lt;setw(11)&lt;&lt;log(d)&lt;&lt;endl;                
    }*/
    wf.close();
    return;    
}

int main()
{
    do
    {
    cout&lt;&lt;&quot;Name der Datei?&quot;&lt;&lt;endl;
    cin&gt;&gt;name;
    int gang=0;
    gang=txtcsv(name);
    cout&lt;&lt;&quot;gang=&quot;&lt;&lt;gang&lt;&lt;endl;
    rf.open(name,ios::in);
    if(rf)
          {
          cout&lt;&lt;&quot;Die Datei existiert bereits!\n&quot;
              &lt;&lt;&quot;1 - Datei ueberschreiben?\n&quot;
              &lt;&lt;&quot;2 - Datei auslesen?\n&quot;
              &lt;&lt;&quot;3 - Dateinamen aendern?\n&quot;
              &lt;&lt;&quot;4 - Inhalt der Datei loeschen?&quot;&lt;&lt;endl;
          cin&gt;&gt;a;
          switch(a)
                   {
                   case 1:
                        schreiben();
                        cout&lt;&lt;&quot;Datei wurde ueberschrieben...&quot;&lt;&lt;endl;
                        break;
                   case 2:
                        char s[100];
                        while(!rf.eof())                        //bis zum ende der file eof
                        {
                        rf.getline(s, 100);                     //liest Zeilenweise in char s ein
                        cout&lt;&lt;s&lt;&lt;endl;
                        }
                        rf.close();
                        break; 
                   case 3:
                        rf.close();
                        break;
                   case 4:
                        wf.open(name,ios::out|ios::trunc);      //öffnet die Datei zum schreiben und löscht den Inhalt
                        wf.close();
                        rf.close();
                        cout&lt;&lt;&quot;Inhalt der Datei geloescht&quot;&lt;&lt;endl; 
                        break;   
                   }   
          }
    else
    {
    schreiben();
    cout&lt;&lt;&quot;Datei wurde erstellt... / Tabelle wurde einschrieben...&quot;&lt;&lt;endl;
    a=0;
    }
    }while(a==3);
    getch();
    return EXIT_SUCCESS;
}
</code></pre>
<p>Ich will das ich einen Dateinamen angeben kann und das Programm dann auf die Endungen csv und txt entsprechend reagiert. in txt soll er eine tabelle mit abständen und Kommastellen etc. schreiben und in csv alles nur mit trennzeichen ;<br />
Das habe ich vor...</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/14195">@Con</a>@n<br />
Wenn ich std::string name; anstatt char name[15];<br />
einsetze bringt er mir dann beim zugreifen auf dateien fehler... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>das mit dem if(go.txt)... sollte nur ein test sein und wird dann abgeändert.</p>
<p>Hab auch schon gegooglet und hier gesucht, aber irgendwie keine antwort auf mein problem gefunden. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1591970</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1591970</guid><dc:creator><![CDATA[Gustl]]></dc:creator><pubDate>Thu, 02 Oct 2008 09:23:03 GMT</pubDate></item><item><title><![CDATA[Reply to Länge eines Strings bestimmen on Thu, 02 Oct 2008 15:48:17 GMT]]></title><description><![CDATA[<p>Diese überarbeitete Version macht nun das gewünschte. Es sind noch ein paar Bugs drin. Z.B. verabschiedet sich das Problem gleich, wenn man nur xyz als Dateiname vergibt. Dateiendungen sind nun einmal nicht auf jeder Plattform notwendig.</p>
<pre><code>#include &lt;cmath&gt;
#include &lt;cstring&gt;
#include &lt;fstream&gt;
#include &lt;ostream&gt;
#include &lt;iostream&gt;

using namespace std;

// globale Variablen entfernt, die braucht man nicht

// richtige Parameter Übergabe eingeführt
// wobei sich die Frage stellt, ob Du das Original verändern willst
// oder nur die lokale Kopie?
int txtcsv(string name) {
	// den richtige Datentyp für len eingesetzt
	std::string::size_type len (name.length());

	int v(0);

	name.erase(len-4,3);
	cout &lt;&lt; name &lt;&lt; endl;
	//if((strcmp(name,&quot;go.txt&quot;))==0)
	{v=1;}

	return v;
}

void schreiben(std::ostream&amp; wf) {
    wf &lt;&lt; &quot;i.;i;i*i;sqrt(f);log(d)\n\n&quot;;

    for(int i=1;i&lt;=100;i++) {
		float	f(i);
		double	d(i);    
		wf &lt;&lt; i &lt;&lt; &quot;.;&quot; &lt;&lt; i &lt;&lt; &quot;;&quot; &lt;&lt; i*i &lt;&lt; &quot;;&quot; &lt;&lt; sqrt(f) &lt;&lt; &quot;;&quot; &lt;&lt; log(d) &lt;&lt; &quot;\n&quot;;                
    }

    /* auskommentiertes gelöscht */

    return;    
}

int main() {
	int a = 0;
	std::string	name;
	std::fstream	rf;

    do {
		cout &lt;&lt; &quot;Name der Datei?&quot; &lt;&lt; endl;
		cin &gt;&gt; name;

		int gang (txtcsv(name));

		cout &lt;&lt; &quot;gang=&quot; &lt;&lt; gang &lt;&lt; endl;

		rf.open(name.c_str(),std::ios::in);

		if (rf) {
			cout &lt;&lt; &quot;Die Datei existiert bereits!\n&quot;
			     &lt;&lt; &quot;1 - Datei ueberschreiben?\n&quot;
			     &lt;&lt; &quot;2 - Datei auslesen?\n&quot;
			     &lt;&lt; &quot;3 - Dateinamen aendern?\n&quot;
			     &lt;&lt; &quot;4 - Inhalt der Datei loeschen?&quot; &lt;&lt; endl;
			cin &gt;&gt; a;

			switch(a) {
				case 1:
					{
						std::fstream datei (name.c_str(), std::ios::out);
						schreiben(datei);
						cout &lt;&lt; &quot;Datei wurde ueberschrieben...&quot; &lt;&lt; endl;
					}
					break;
				case 2:
					char s[100];
					while(!rf.eof()) {		// bis zum ende der file eof
						rf.getline(s, 100);	// liest Zeilenweise in char s ein
						cout &lt;&lt; s &lt;&lt; endl;
					}
					rf.close();
					break;
				case 3:
					rf.close();
					break;
				case 4:
					{
						std::fstream datei (name.c_str(), std::ios::out|std::ios::trunc);
					}
					rf.close();
					cout&lt;&lt;&quot;Inhalt der Datei geloescht&quot;&lt;&lt;endl;
					break;  
			}  
		} else {
			std::fstream datei (name.c_str(), std::ios::out);
			schreiben(datei);
			cout &lt;&lt; &quot;Datei wurde erstellt... / Tabelle wurde einschrieben...\n&quot;;

			a=0;
		}
	} while(a==3);

    // getch(); // reine Windows Eigenheit
    return EXIT_SUCCESS;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1592296</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1592296</guid><dc:creator><![CDATA[*john 0]]></dc:creator><pubDate>Thu, 02 Oct 2008 15:48:17 GMT</pubDate></item><item><title><![CDATA[Reply to Länge eines Strings bestimmen on Sat, 04 Oct 2008 10:30:09 GMT]]></title><description><![CDATA[<p>klasse, dankeschön. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1593265</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1593265</guid><dc:creator><![CDATA[Gustl]]></dc:creator><pubDate>Sat, 04 Oct 2008 10:30:09 GMT</pubDate></item></channel></rss>