C / C++ Verständnisfrage
robobimbo 01.04.2005 - 00:24 875 11
DirtyHarry
aka robobimbo
|
Wieso kommt bei folgenden Source: #include <io.h>
#include <iostream.h>
#include <string.h>
#include <stdio.h>
using namespace std;
char *temp = "Profil_XXXXXX";
char *result;
char *resultTXT = "";
char names[5][200];
int resultINT;
FILE *fp;
int main( void )
{
for( int i = 0; i < 5; i++ )
{
strcpy( names[i], temp );
result = _mktemp( names[i] );
if( result == NULL )
{
printf( "Fehler im Template: %s\n", temp );
}
else
{
fp = fopen( result, "w" );
if ( fp == NULL )
{
printf( "Fehler beim Erzeugen von %s\n", result );
}
strcpy( names[i], result );
printf( "%d Datei ´%s´ (%s) erzeugt.\n", i,names[i],result);
fclose( fp );
}
}
for( int i = 0; i < 5; i++ )
{
strcpy(result, names[i]);
strcpy(resultTXT, names[i]);
strcat(resultTXT, ".txt");
printf ( "%d Durchlauf: %s (%s)nach %s\n",i,names[i],result,resultTXT);
resultINT = rename( result, resultTXT );
if( resultINT != 0 )
printf( "Kann ´%s´ nicht umbenennen.\n", result );
else
{
printf( "Datei ´%s´ umbenannt nach ´%s´\n", result, resultTXT );
}
}
return 0;
}
folgende Ausgabe raus:
C:\watcom\src\test>runa
0 File "Profil_a03468" (Profil_a03468) created.
1 File "Profil_b03468" (Profil_b03468) created.
2 File "Profil_c03468" (Profil_c03468) created.
3 File "Profil_d03468" (Profil_d03468) created.
4 File "Profil_e03468" (Profil_e03468) created.
0 Durchlauf: Profil_a03468 (Profil_a03468)nach Profil_a03468.txt
File "Profil_a03468" renamed to "Profil_a03468.txt"
1 Durchlauf: Profil_b03468 (Profil_b03468)nach Profil_b03468.txt
File "Profil_b03468" renamed to "Profil_b03468.txt"
2 Durchlauf: Profil_c03468 (Profil_c03468)nach Profil_c03468.txt
File "Profil_c03468" renamed to "Profil_c03468.txt"
3 Durchlauf: Profil_d03468 (Profil_d03468)nach Profil_d03468.txt
File "Profil_d03468" renamed to "Profil_d03468.txt"
4 Durchlauf: Profil_d03468 (Profil_d03468)nach Profil_d03468.txt
Could not rename "Profil_d03468"
Mein Kopfkratzen kommt von da: Wenn in names[4] in der ersten Schleife noch "Profil_e03468" warum steht in der zweiten Schleife dann auf einmal "Profil_d03468" drinnen? Ich steh echt am Schlauch  Danke für Hilfe
Bearbeitet von DirtyHarry am 20.04.2005, 21:01
|
that
Hoffnungsloser Optimist
|
Du kopierst in der zweiten Schleife etwas in "result" und "resultTXT", obwohl das nur char* sind, ohne eigenen Speicher dahinter.
Reiner Zufall, wenn das Programm die zweite Schleife überhaupt ohne Absturz überlebt.
|
ica
hmm
|
bevor ich mir das ganze durchdenke: dein output stimmt ja nichtmal mit dem programm überein...
|
DirtyHarry
aka robobimbo
|
Ja, hab eine paar Texte noch eingedeutscht - aber das Problem wars gleiche.  @that - danke werd mal guckern
|
samuel
.:: unnahbar ::.
|
darf man fragen was du mit dieser grausamen mischung und C und C++ bezwecken willst?
warum verwendest du nicht gleich C++ ?
*wondering* sam
|
that
Hoffnungsloser Optimist
|
Das ist eh C++; ich mag stdio.h auch lieber als iostream, gerade u.a. wegen printf für so kleine Programme.
|
ica
hmm
|
Das ist eh C++; ich mag stdio.h auch lieber als iostream, gerade u.a. wegen printf für so kleine Programme. ? wieso lieber stdio.h als iostream? wieso char* statt strings?
|
that
Hoffnungsloser Optimist
|
stdio: vielleicht einfach aus Gewohnheit. Für die Strings würde ich aber natürlich schon string und vector<string> verwenden.
|
samuel
.:: unnahbar ::.
|
? wieso lieber stdio.h als iostream? wieso char* statt strings? ich habe auch vor allem wegen den char* gefragt. ein cstring ist ja was nettes, aber speziell hier hat sich bei C++ schon viel getan. vor allem die STL ist ein sehr maechtiges werkzeug. sam btw: wenn du mal mit STL umgehen gelernt hast empfehle ich effective STL von scott meyers. ein sehr gutes buch. sam
|
mat
AdministratorLegends never die
|
stdio: vielleicht einfach aus Gewohnheit. Für die Strings würde ich aber natürlich schon string und vector<string> verwenden. full ack, stdio + eigene string klasse, bei der ich 100% weiss was wo wann passiert und ein paar nette ol operators. manchmal reicht aber auch ein struct mit char *, kommt eben auf die situation an (egal ob mit stl oder ohne).
|
ica
hmm
|
also ich seh einfach keinen vorteil in stdio, gibts überhaupt einen? wozu immer umständlich typen angeben mit % wenns ein simples cout auch macht.
|
that
Hoffnungsloser Optimist
|
stdio Code ist einfach kürzer und IMHO übersichtlicher (wenn auch fehleranfälliger). Vergleiche: printf("%d Datei ´%s´ (%s) erzeugt.\n", i, names[i], result);
cout << i << " Datei ´" << names[i] << "´ (" << result << ") erzeugt." << endl;
|