I would like to know how to change the name of a file that has already a fixed default name in the main().
As you can see in the main, the name of the file is already set to movies.dat (char dateiname[50] ="movies.dat") and I would like to change that with case 'd' that goes to the function "dateiUmbenennen()". Also the changed name should be used everywhere where I used "dateiname".
Any help would be nice!
This is my code so far:
int
dateiUmbenennen(char* pdateiname)
{
char newName[50];
printf("Bitte geben Sie den neuen Namen mit gewuenschtem Dateitypen ein: ");
scanf("%s", &newName);
int res = rename(pdateiname, newName);
if (!res)
{
printf("Datei wurde erfolgreich in %s umbenannt!\n", newName);
*pdateiname = newName[50];
}
else
{
printf("ERROR: Umbenennung fehlgeschlagen mit dem Fehler: %s!\n", strerror(errno));
}
return 0;
}
// ================================================================
// MAIN
// ================================================================
int main(void)
{
// Default fuer Namen der Filmdatei
char dateiname[50]= "movies.dat";
// Hilfsvariablen-------------------------------------------------------------------------------------------------
// Hilfsvariable fuer do-while-Schleife
int stopLoop = 0;
// Hauptschleife
do {
// Ausgabe des Bildschirm-Menus
printf("\nAUSWAHL-MENU:\n");
printf("(n) Neuen Film eingeben\n");
printf("(a) Alle Filme anzeigen lassen\n");
printf("(o) Filmdatenbank aus Datei einlesen\n");
printf("(s) Filmdatenbank in Datei speichern\n");
printf("(d) Dateinamen fuer Lesen/Speichern festlegen (Aktuell: %s)\n",
dateiname);
printf("(q) Programm verlassen\n");
//printf("\nIhre Wahl: ");
// Abfrage der Benutzerwahl
char ch = _getch();
printf("%c\n\n", ch);
// Aktionen in Abhaengigkeit von der Benutzerwahl
switch (ch)
//----------------------------------------------------------------------------------------------------------------------
{
case 'q': // Programm verlassen
stopLoop = 1;
break;
case 'n': // Neuen Film eingeben
movieDatensatz = benutzerEingabe();
break;
case 'a': // Alle Filme anzeigen lassen
konsolenAusgabe(movieDatensatz, anzahlFilme=dateiEinlesen(dateiname));
break;
case 'o': // Filmdatenbank aus Datei einlesen
dateiEinlesen(dateiname);
break;
case 's': // Filmdatenbank in Datei speichern
break;
case 'd': // Dateinamen fuer Lesen/Speichern festlegen
dateiUmbenennen(&dateiname[50]);
break;
case 'h':
printf("%s", dateiname);
break;
default: // keine gueltige Eingabe
printf("==> Ungueltige Eingabe!\n");
}
} while (!stopLoop);
return 0;
}
CodePudding user response:
If you want the function dateiUmbenennen
to change the contents of the array dateiname
in the function main
, then, when calling the function dateiUmbenennen
from main
, you should pass a pointer to that array.
You seem to be attempting to do exactly that in the line
dateiUmbenennen(&dateiname[50]);
However, this is passing the address of the 51st element of that array (which does not even exist, because the array only has 50 elements) to the function. This does not make sense. You should instead pass the address of the start of the array, i.e. the address of the 1st element. You can do it like this:
dateiUmbenennen( &dateiname[0] );
However, it is more common to simply write
dateiUmbenennen( dateiname );
as using the array in this context will cause the array to automatically decay to a pointer to the first element.
The line
*pdateiname = newName[50];
also does not make sense, as that line copies the (non-existant) 51st element of the array newName
to whatever pdateiname
is pointing to (which should be the start of dateiname
, but is not in your posted code). Assuming that pdateiname
is pointing to the start of dateiname
, then you can copy the whole string using the function strcpy
, like this:
strcpy( pdateiname, newName );
Also, I recommend that you change the line
scanf("%s", &newName);
to
scanf("Is", &newName);
so that if the user enters more than 49 characters, you will not overflow the newName
array.
CodePudding user response:
The line
*pdateiname = newName[50];
doesn't really make any sense -- it reads an invalid location (newName[50] is past the end of the array) and copies a single character. What you probably meant to do is
strcpy(pdateiname, newName);
to copy the string from the newName
array to the pdateiname
array.