URL: https://www.overclockers.at/coding-stuff/c_c_verstaendnisfrage_139891/page_1 - zur Vollversion wechseln!
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; }
Code: PHPC:\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"
Danke für Hilfe 
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.
bevor ich mir das ganze durchdenke: dein output stimmt ja nichtmal mit dem programm überein...
Ja, hab eine paar Texte noch eingedeutscht - aber das Problem wars gleiche. 
@that - danke werd mal guckern 
darf man fragen was du mit dieser grausamen mischung und C und C++ bezwecken willst?
warum verwendest du nicht gleich C++ ?
*wondering* sam
Das ist eh C++; ich mag stdio.h auch lieber als iostream, gerade u.a. wegen printf für so kleine Programme.
Zitat von thatDas ist eh C++; ich mag stdio.h auch lieber als iostream, gerade u.a. wegen printf für so kleine Programme.
stdio: vielleicht einfach aus Gewohnheit.
Für die Strings würde ich aber natürlich schon string und vector<string> verwenden.
Zitat von iCA-? wieso lieber stdio.h als iostream? wieso char* statt strings?
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).Zitat von thatstdio: vielleicht einfach aus Gewohnheit.
Für die Strings würde ich aber natürlich schon string und vector<string> verwenden.
also ich seh einfach keinen vorteil in stdio, gibts überhaupt einen? wozu immer umständlich typen angeben mit % wenns ein simples cout auch macht.
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;
overclockers.at v4.thecommunity
© all rights reserved by overclockers.at 2000-2026