在 Linux 上运行时,C fgets 函数不起作用

标签 c

所以我遇到的问题是我相信 fgets 被跳过,但我不确定。我正在使用的代码如下所示:

void createAccount()
{
ignore = fseek(stdin,0,SEEK_END);
printf("Creating A New Account:\n");
FILE *input;
input = fopen("accounts.txt", "a");
if (input == NULL)
{
    printf("Error opening accounts.txt file!\n");
    exit(1);
}
char username[USERNAME_LENGTH];
char password[PASSWORD_LENGTH];
char type[CHOICE_LENGTH];
char age[10];
printf("Username:");
a = fgets(username,USERNAME_LENGTH,stdin);
char *fileOpener = malloc(strlen(username));
if (fileOpener!=NULL) {
    strcpy(fileOpener, username);
}
const size_t end = strlen(fileOpener) - 1;
if (fileOpener[end] == '\n')
    fileOpener[end] = '\0';
ignore = fseek(stdin,0,SEEK_END);
printf("age:");
a = fgets(age,10,stdin);
ignore = fseek(stdin,0,SEEK_END);
printf("Password:");
a = fgets(password,PASSWORD_LENGTH,stdin);
ignore = fseek(stdin,0,SEEK_END);
printf("Account Type:");
a = fgets(type,CHOICE_LENGTH,stdin);
ignore = fseek(stdin,0,SEEK_END);
ignore = fputs(username,input);
ignore = fputs(password,input);
ignore = fputs(type,input);
fclose(input);
FILE *newFile = fopen(fileOpener,"ab+");
char *newAge = malloc(strlen("AGE: " + strlen(age)));
if (newAge!=NULL) {
    strcpy(newAge,"AGE: ");
    strcat(newAge,age);
}
char *newName =malloc(strlen("NAME: " + strlen(username)));
if (newName!=NULL) {
    strcpy(newName,"NAME: ");
    strcat(newName,username);
}
ignore = fputs(newName,newFile);
ignore = fputs(newAge,newFile);
ignore = fputs("MEDICATIONS: ",newFile);
ignore = fputs("\nVITALS: ",newFile);
ignore = fputs("\nHISTORY: ",newFile);
ignore = fputs("\nALLERGIES: ",newFile);
fclose(newFile);
printf("Account made.");
}

它扫描用户输入并将其放入文件中。问题是它从不提示输入用户名,而是直接提示年龄,输出如下所示:

Creating A New Account:
Username:age:(user input)
Password: (user input)
Account Type: (user input)

当它看起来像这样时:

Creating A New Account:
Username:(user input)
age:(user input)
Password:(user input)
Account Type:(user input)

所以看起来用户名的 fgets 被忽略了?它直接进入老化状态,将其打印在同一行上。当我在 mac 上运行这段代码时,它工作正常,但我只在 linux 上遇到这个问题。请帮忙。

最佳答案

void createAccount()
{
fseek(stdin,0,SEEK_END);
printf("Creating A New Account:\n");
FILE *input = fopen("accounts.txt", "a");
if (input == NULL)
{
    printf("Error opening accounts.txt file!\n");
    exit(1);
}
char username[USERNAME_LENGTH];
char password[PASSWORD_LENGTH];
char type[CHOICE_LENGTH];
char age[10];
printf("Username:");
fgets(username,USERNAME_LENGTH,stdin);
char *fileOpener = malloc(strlen(username));
 if (fileOpener == NULL)
{
    printf("cannot allocating the space in malloc!\n");
    exit(1);
}
    strcpy(fileOpener, username);
const size_t end = strlen(fileOpener) - 1;
if (fileOpener[end] == '\n')
    fileOpener[end] = '\0';
fseek(stdin,0,SEEK_END);
printf("age:");
fgets(age,10,stdin);
fseek(stdin,0,SEEK_END);
printf("Password:");
fgets(password,PASSWORD_LENGTH,stdin);
fseek(stdin,0,SEEK_END);
printf("Account Type:");
fgets(type,CHOICE_LENGTH,stdin);
fseek(stdin,0,SEEK_END);
fputs(username,input);
fputs(password,input);
fputs(type,input);
fclose(input);
FILE *newFile = fopen(fileOpener,"ab+");
char *newAge = malloc(strlen("AGE: ") + strlen(age));
if (newAge == NULL) {
   printf("cannot allocating the space in malloc!\n");
    exit(1);
}
 strcpy(newAge,"AGE: ");
    strcat(newAge,age);
char *newName =malloc(strlen("NAME: ") + strlen(username));
if (newName == NULL) {
   printf("cannot allocating the space in malloc!\n");
    exit(1);
}
strcpy(newName,"NAME: ");
    strcat(newName,username);
fputs(newName,newFile);
fputs(newAge,newFile);
fputs("MEDICATIONS: ",newFile);
fputs("\nVITALS: ",newFile);
fputs("\nHISTORY: ",newFile);
fputs("\nALLERGIES: ",newFile);
fclose(newFile);
printf("Account made.");
}

您的代码存在一些问题。

1:如果你不真正使用它们,则不需要编写a =ignore =

2:malloc(strlen("things"+ strlen(things))) 更改为 `malloc(strlen("things: ") + strlen(things) )

检查更多...

关于在 Linux 上运行时,C fgets 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43132483/

相关文章:

C:如何使用while循环计算多位整数的位数?

c - 变量类型如何影响 C 中的指针算术工作?

c - 在 C 中使用 "multidimensional" "dynamic arrays"

将 double 转换为 int 而不四舍五入

c - 矩阵转置在 C 中发送错误

c - OpenGL 绘制缓冲区问题

c - Windows C 运行时库没有像我预期的那样链接?

c - 使用 Pipe() 时,子进程如何向父进程返回两个值?

c - 在 C 中,缺少 "Main process ends"到 "call any functions registered with atexit"之间的链接

c - 从 CC2530 上的 GPIO 引脚读取