Я в основном разрабатываю программное обеспечение на Java, но в настоящее время я пробую кое-что на C, и я застрял на странной проблеме.scanf меняет значения из других строк
Я использую метод scanf()
для изменения значения из строки, но scanf()
не изменяет только значение из параметризованной строки, которое оно также меняет и значение из других строк.
Теперь мой вопрос: я просто избалованный от Java-разработчика, и я слишком глупый, чтобы использовать его правильно? Я не вижу, где я делаю ошибку.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char lFileType[] = ".txt";
char lFilePath[] = "C:\\Notenverwaltungssystem";
char lFileFinalPath[] = "";
char lFileName[] = "";
printf("lFileType before scanf: ");
printf("%s \n", lFileType);
printf("lFilePath before scanf: ");
printf("%s \n", lFilePath);
printf("lFileName before scanf: ");
printf("%s \n", lFileName);
printf("lFileFinalPath before scanf: ");
printf("%s \n\n", lFileFinalPath);
printf("Bitte geben Sie den Namen der Pruefung an: \n\n");
scanf("%s", &lFileName);
printf("\nlFileType after scanf: ");
printf("%s \n", lFileType);
printf("lFilePath after scanf: ");
printf("%s \n", lFilePath);
printf("lFileName after scanf: ");
printf("%s \n", lFileName);
printf("lFileFinalPath after scanf: ");
printf("%s \n\n", lFileFinalPath);
system("PAUSE");
return 0;
}
Ожидаемый результат:
lFileType before scanf: .txt
lFilePath before scanf: C:\Notenverwaltungssystem
lFileName before scanf:
lFileFinalPath before scanf:
Bitte geben Sie den Namen der Pruefung an:
Test
lFileType after scanf: .txt
lFilePath after scanf: C:\Notenverwaltungssystem
lFileName after scanf: Test
lFileFinalPath after scanf:
Press any key to continue . . .
Что я получаю в качестве выходного сигнала, когда я исполняю программка:
lFileType before scanf: .txt
lFilePath before scanf: C:\Notenverwaltungssystem
lFileName before scanf:
lFileFinalPath before scanf:
Bitte geben Sie den Namen der Pruefung an:
Test
lFileType after scanf: .txt
lFilePath after scanf: st
lFileName after scanf: Test
lFileFinalPath after scanf: est
Press any key to continue . . .
О размере буфера есть несколько ответов. Я хотел бы добавить, что 'scanf ("% s ", & lFileName)" можно изменить на 'scanf ("% s ", lFileName)', потому что 'lFileName' является адресом буфера, и нет необходимости в' '. – i486