字符串参数变空 [英] string parametres becoming empty
问题描述
我正在尝试这样做:
#include< stdlib.h>
#include< stdio.h>
#define FILE" / tmp / myfile"
#define USERS_LIST" / tmp / userslist"
int main(){
//打开文件
FILE * fp = fopen(FILE," r");
char buf [512];
fgets(buf,sizeof(buf),fp);
char * username,* password,* pid ;
pid = strtok(buf," ;;");
username = strtok(NULL," ;;");
password = strtok(NULL;" ;;");
if(SearchUser(username)!= NULL){
printf(" user exists");
}
else {
if(RegisterUser(用户名,密码)== 0){
printf( 注册成功);
}
}
}
/ *搜索名称为<的用户;用户名> ;.
返回< password>如果找到用户。
如果找不到具有给定用户名的用户,则返回NULL。* /
char * SearchUser(char * username){
FILE * fp = fopen(USERS_LIST," r");
while(feof(fp)== 0){
char line [100 ];
char * buf = fgets(line,sizeof(line),fp);
char * iUsername = strtok(buf," ;;");
char * iPassword = strtok(NULL," ;;");
if(strcmp(iUsername,username)== 0){
fclose(fp);
返回iPassword;
}
}
fclose(fp );
返回NULL;
}
/ *使用给定的< username>注册用户和<密码>。
如果注册成功,则返回0。
如果注册失败,则返回-1。* /
int RegisterUser(char *用户名,字符*密码){
FILE * fp = fopen(USERS_LIST," a");
if(fprintf(fp,"%) s;%s; \ n",用户名,密码)< 0){
fclose(fp);
返回-1;
}
fclose(fp);
返回0;
}
但是当我在SearchUser和RegisterUser函数中查看gdb我需要注意我传递的字符串参数是空的...不是NULL,(但是
例如而不是< username> ;是erik它是"
....而在RegisterUser中,当我打开文件时,有些东西已被写入了b
但它是无稽之谈(比如< username = erik password = mypwd,
我找到的文件/()/£GY;()"(G())....我无法理解
存在问题......
非常感谢
Erik写道:我是试图这样做:
#include< stdlib.h>
#include< stdio.h>
#define FILE" / tmp / myfile" ;
你确定要这么做????
-
Ian Collins。
是的,为什么? FILE定义将消失,因为我会询问用户
文件名,但是现在它就像那样......
Erik写道:是的,为什么? FILE定义将消失,因为我会询问用户
文件名,但现在它就像那样...
你有#define FILE" something ;
再向下你有
FILE * fp = fopen(FILE,r);
想想看什么在那里发生了第一个FILE
令牌,你发布的代码不能编译。
你的代码也缺乏错误处理。
Hi, i''m trying to do this :
#include <stdlib.h>
#include <stdio.h>
#define FILE "/tmp/myfile"
#define USERS_LIST "/tmp/userslist"
int main() {
//open file
FILE *fp = fopen(FILE, "r");
char buf[512];
fgets(buf, sizeof(buf), fp);
char *username, *password, *pid;
pid = strtok(buf, ";");
username = strtok(NULL, ";");
password = strtok(NULL; ";");
if (SearchUser(username) != NULL) {
printf("user exists");
}
else {
if (RegisterUser(username, password) == 0) {
printf("registration successful");
}
}
}
/* Searches for user with name <username>.
Returns <password> if user is found.
Return NULL if no user with given username is found.*/
char *SearchUser(char *username) {
FILE *fp = fopen(USERS_LIST,"r");
while (feof(fp) == 0) {
char line[100];
char *buf = fgets(line, sizeof(line), fp);
char *iUsername = strtok(buf, ";");
char *iPassword = strtok(NULL, ";");
if (strcmp(iUsername, username) == 0) {
fclose(fp);
return iPassword;
}
}
fclose(fp);
return NULL;
}
/* Registers user with given <username> and <password>.
Returns 0 if registration successful.
Return -1 if registration failed.*/
int RegisterUser(char *username, char *password) {
FILE *fp = fopen(USERS_LIST, "a");
if ( fprintf(fp, "%s;%s;\n", username, password) < 0) {
fclose(fp);
return -1;
}
fclose(fp);
return 0;
}
But when I look with gdb inside SearchUser and RegisterUser functions I
notice that the string parameters I passed are empty... not NULL, (but
for example instead of <username> being "erik" it is "")
.... and in RegisterUser when I open the file, something has been
written but it is nonsense( like <username=erik password = mypwd,
inside the file i found /()/£GY;()"(G() ) .... I can''t understand
where resides the problem...
thank you very much
Erik wrote:Hi, i''m trying to do this :
#include <stdlib.h>
#include <stdio.h>
#define FILE "/tmp/myfile"
Are you sure you want to do that????
--
Ian Collins.
yes, why ? the FILE define will disappear, as I will ask user for
filename, but for now it''s like that...
Erik wrote:yes, why ? the FILE define will disappear, as I will ask user for
filename, but for now it''s like that...
You have #define FILE "something"
Further down you have
FILE *fp = fopen(FILE, "r");
Think about what happens with the first FILE
token there, your code as posted does not compile.
Your code also lack error handling all over.
这篇关于字符串参数变空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!