我正在写的一段代码遇到了很多问题。我也不是很确定出了什么问题。
下面是我正在尝试的代码:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[]) {
char pass[10];
FILE *fp;
char username[10];
system("clear");
printf("\nWelcome to Sign-Up Testing.");
printf("\nWhat UserName would you like?");
printf(" Max 10 characters.");
printf("\n\n>>>");
scanf("%s", &username);
fp = fopen("%s.dgf", username,"r");
if(fp != NULL) {
printf("\n%s is already taken.\n");
sleep(1);
return 0;
}
else if(fp == NULL){
fopen("%s.dgf", username,"w");
printf("\nPassword:\n");
scanf("%s", &pass);
fprintf(fp,"%s", pass);
printf("\nThank you for signing up!");
}
return 0;
}这是Terminal告诉我的。
Sign-Up.c: In function ‘main’:
Sign-Up.c:15:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[10]’ [-Wformat]
Sign-Up.c:16:2: error: too many arguments to function ‘fopen’
/usr/include/stdio.h:273:14: note: declared here
Sign-Up.c:18:3: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat]
Sign-Up.c:23:3: error: too many arguments to function ‘fopen’
/usr/include/stdio.h:273:14: note: declared here
Sign-Up.c:25:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[10]’ [-Wformat]发布于 2014-07-03 11:28:19
fp = fopen("%s.dgf", username,"r");fopen不是一个变量函数,它不支持像%s这样的格式说明符。使用sprintf或strcpy使文件名成为字符串,然后使用它调用fopen。
另一个问题是scanf
scanf("%s", &username);username是一个char数组,并被转换为指向char的指针,这里不需要&:
scanf("%s", username);发布于 2014-07-03 11:57:41
fopen函数是fopen ("file", "r");该函数有3个参数。您可以使用
fopen(strcat("%s.dgf",username),"r");我更喜欢使用gets函数来获取字符串:
char username[100];
gets(username);https://stackoverflow.com/questions/24544346
复制相似问题