"We are back" « oc.at

C / C++ Verständnisfrage

robobimbo 01.04.2005 - 00:24 875 11
Posts

DirtyHarry

aka robobimbo
Avatar
Registered: Apr 2001
Location: outer space
Posts: 464
Wieso kommt bei folgenden Source:

Code: PHP
#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:

Code: PHP
 
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
Avatar
Registered: Mar 2000
Location: MeidLing
Posts: 11346
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
Avatar
Registered: Jul 2002
Location: Graz
Posts: 9833
bevor ich mir das ganze durchdenke: dein output stimmt ja nichtmal mit dem programm überein...

DirtyHarry

aka robobimbo
Avatar
Registered: Apr 2001
Location: outer space
Posts: 464
Ja, hab eine paar Texte noch eingedeutscht - aber das Problem wars gleiche. :)

@that - danke werd mal guckern :)

samuel

.:: unnahbar ::.
Avatar
Registered: Jul 2000
Location: hagenberg
Posts: 2680
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
Avatar
Registered: Mar 2000
Location: MeidLing
Posts: 11346
Das ist eh C++; ich mag stdio.h auch lieber als iostream, gerade u.a. wegen printf für so kleine Programme.

ica

hmm
Avatar
Registered: Jul 2002
Location: Graz
Posts: 9833
Zitat von that
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
Avatar
Registered: Mar 2000
Location: MeidLing
Posts: 11346
stdio: vielleicht einfach aus Gewohnheit.
Für die Strings würde ich aber natürlich schon string und vector<string> verwenden.

samuel

.:: unnahbar ::.
Avatar
Registered: Jul 2000
Location: hagenberg
Posts: 2680
Zitat von iCA-
? 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

Administrator
Legends never die
Avatar
Registered: Aug 2003
Location: nö
Posts: 25691
Zitat von that
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
Avatar
Registered: Jul 2002
Location: Graz
Posts: 9833
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
Avatar
Registered: Mar 2000
Location: MeidLing
Posts: 11346
stdio Code ist einfach kürzer und IMHO übersichtlicher (wenn auch fehleranfälliger). Vergleiche:

Code:
printf("%d Datei ´%s´ (%s) erzeugt.\n", i, names[i], result);

cout << i << " Datei ´" << names[i] << "´ (" << result << ") erzeugt." << endl;
Kontakt | Unser Forum | Über overclockers.at | Impressum | Datenschutz