15-09-2005, 17:25
|
|
|
|
חבר מתאריך: 04.08.02
הודעות: 4,468
|
|
sorry for posting in english...
he doesn't need strcpy.. and it wouldn't work anyway, you've created a string with a pre-specified size allocated automatically by the compiler (in your example), so trying to copy something there or even concatenate, would result to writing into a memory which you don't own.
A C++ solution would be to use STL String.
If he does want to get dirty, he can allocate an amount of size, input a string, if there is enough space to concatenate ".txt" do so, else, use realloc() and get yourself more space.
Now, the easiest solution would be to create a buffer big enough to hold the user input + ".txt", but you can never trust the user
char sFileName[BUFSIZ];
char *p;
FILE *fp;
fgets( sFileName, sizeof( sFileName ), stdin );
p = strrchr( sFileName, '\n' );
if( p ) *p = '\0';
strcat( sFileName, ".txt" );
fp = fopen( sFileName, "w" );
if( fp == NULL ) {
//do error handling here
}
Sorry for posting in english and not using CODE tags..
Itzik
_____________________________________
C pogrammers never die. They are just cast into void.
|