无法使用 fopen() 打开文件
Can't open a file using fopen()
我正在编写一个模拟用户注册平台的程序。在注册功能中,我询问用户他的用户名,程序应该创建一个新文件,用户名作为文件名来存储他的密码和其他功能的其他数据。这里的问题是 fopen returns NULL 这意味着它不能被创建。
void Register()
{
char name[11];
char password[11];
char aux[11];
system("cls");
FILE *fp=fopen("Users.txt","a+"); //I've created this .txt, just has one user which is admin
if (fp==NULL){
printf("\nFile cant be opened");
getchar();
return;
}
printf("Write a new username (no more than 10 characters, no spaces) ");
fflush(stdin);
fgets(name,sizeof(name),stdin);
getchar();
do{
if((fgets(aux,sizeof(aux),fp))!=NULL){ //Checks is reading lines
if ((strcmp(name,aux))==0){ //Username already exists
printf("\nUsername already exists, try another: ");
fflush(stdin);
fgets(name,sizeof(name),stdin);
rewind(fp); //Takes pointer to the beginning
}
}
}while(!(feof(fp)));
fseek(fp,0,SEEK_END); //Takes pointer to end
fprintf(fp,"%s",name);
fclose(fp);
fp=fopen(name,"w"); /*THIS IS WHERE IT FAILS, RETURNS NULL*/
if (fp==NULL){
printf("\nFile cant be opened");
getchar();
return;
}
printf("\nChoose a password(no more than 10 characters): ");
fflush(stdin);
fgets(password,sizeof(password),stdin);
getchar();
fprintf(fp,"%s\n",name);
fprintf(fp,"%s",password);
fclose(fp);
printf("\nUser successfully registered\nName: %s\nPassword: %s",name,password);
getchar();
}
我已经尝试过另一种方法。例如,使用 strcpy() 将名称复制到新数组,然后使用 strcat() 添加“.txt”,但它也不起作用。像这样:
[...]
char aux2[20];
strcpy(aux2,name);
strcat(aux2,".txt");
fp=fopen(aux2,"w"); /*FAILS TOO */
if (fp==NULL){
printf("\nFile cant be opened");
getchar();
return;
}
无法弄清楚出了什么问题。希望你能帮助我
问题列表:
您在尝试使用它之前没有声明 fp
(至少在显示的代码中)。
将行更改为:
FILE *fp=fopen("Users.txt","a+");
您还没有申报nombre
。添加行:
char nombre[11];//匹配 name
的大小
(或者,您可能打算使用 name
?)
代码中存在的所有地方,注释掉该行:
//fflush(标准输入);
换行
fp=fopen(name,"w"); /*THIS IS WHERE IT FAILS, RETURNS NULL*/
至:
fp=fopen(name,"w+"); /*THIS IS WHERE IT FAILS, RETURNS NULL*/
刚弄明白,只好换行了
if((fgets(aux,sizeof(aux),fp))!=NULL){ //Checks is reading lines
而是像这样使用 fscanf():
if((fscanf(fp,"%s",&aux)==1){ //Checks if is reading a string (a line)
问题不清楚文件中数据的实际格式。所以我决定对文件的每一行使用 name password
的格式。
这是一个可以干净地编译并执行所需功能的代码版本。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Register( void );
void Register( void )
{
char name[11];
char password[11];
char aux[11];
system("cls");
printf("Write a new username (no more than 10 characters, no spaces) ");
if( !fgets(name,sizeof(name),stdin) )
{
fprintf( stderr, "fgets for user name failed" );
exit( EXIT_FAILURE );
}
// implied else, fgets successful
// code should remove trailing new line, if any
char *newline;
if( (newline = strchr( name, '\n' ) ) )
{
*newline = '[=10=]';
}
FILE *fp=fopen("Users.txt","a+"); //I've created this .txt, just has one user which is admin
if ( !fp)
{
perror("fopen for Users.txt for read/write failed");
getchar();
exit( EXIT_FAILURE );
}
// implied else, fopen successful
while( fgets(aux,sizeof(aux),fp) )
{ //Checks is reading lines
// separate out the name field
char *token = strtok( aux, " " );
if( token )
{
if ((strcmp(name,aux))==0)
{ //Username already exists
printf("\nUsername already exists, try another: ");
rewind(fp); //Takes pointer to the beginning
}
}
}
// name not already in file
printf("\nChoose a password(no more than 10 characters): ");
if( !fgets(password,sizeof(password),stdin) )
{
perror( "fgets for password failed" );
fclose( fp ); // cleanup
exit( EXIT_FAILURE );
}
// implied else, fgets successful
//remove trailing newline
if( (newline = strchr( password, '\n' ) ) )
{
*newline = '[=10=]';
}
fprintf(fp,"%s %s\n",name, password);
fclose(fp);
printf("\nUser successfully registered\nName: %s\nPassword: %s",name,password);
getchar();
} // end function: Register
我正在编写一个模拟用户注册平台的程序。在注册功能中,我询问用户他的用户名,程序应该创建一个新文件,用户名作为文件名来存储他的密码和其他功能的其他数据。这里的问题是 fopen returns NULL 这意味着它不能被创建。
void Register()
{
char name[11];
char password[11];
char aux[11];
system("cls");
FILE *fp=fopen("Users.txt","a+"); //I've created this .txt, just has one user which is admin
if (fp==NULL){
printf("\nFile cant be opened");
getchar();
return;
}
printf("Write a new username (no more than 10 characters, no spaces) ");
fflush(stdin);
fgets(name,sizeof(name),stdin);
getchar();
do{
if((fgets(aux,sizeof(aux),fp))!=NULL){ //Checks is reading lines
if ((strcmp(name,aux))==0){ //Username already exists
printf("\nUsername already exists, try another: ");
fflush(stdin);
fgets(name,sizeof(name),stdin);
rewind(fp); //Takes pointer to the beginning
}
}
}while(!(feof(fp)));
fseek(fp,0,SEEK_END); //Takes pointer to end
fprintf(fp,"%s",name);
fclose(fp);
fp=fopen(name,"w"); /*THIS IS WHERE IT FAILS, RETURNS NULL*/
if (fp==NULL){
printf("\nFile cant be opened");
getchar();
return;
}
printf("\nChoose a password(no more than 10 characters): ");
fflush(stdin);
fgets(password,sizeof(password),stdin);
getchar();
fprintf(fp,"%s\n",name);
fprintf(fp,"%s",password);
fclose(fp);
printf("\nUser successfully registered\nName: %s\nPassword: %s",name,password);
getchar();
}
我已经尝试过另一种方法。例如,使用 strcpy() 将名称复制到新数组,然后使用 strcat() 添加“.txt”,但它也不起作用。像这样:
[...]
char aux2[20];
strcpy(aux2,name);
strcat(aux2,".txt");
fp=fopen(aux2,"w"); /*FAILS TOO */
if (fp==NULL){
printf("\nFile cant be opened");
getchar();
return;
}
无法弄清楚出了什么问题。希望你能帮助我
问题列表:
您在尝试使用它之前没有声明
fp
(至少在显示的代码中)。 将行更改为:FILE *fp=fopen("Users.txt","a+");
您还没有申报
nombre
。添加行:char nombre[11];//匹配
name
的大小
(或者,您可能打算使用 name
?)
代码中存在的所有地方,注释掉该行:
//fflush(标准输入);
换行
fp=fopen(name,"w"); /*THIS IS WHERE IT FAILS, RETURNS NULL*/
至:
fp=fopen(name,"w+"); /*THIS IS WHERE IT FAILS, RETURNS NULL*/
刚弄明白,只好换行了
if((fgets(aux,sizeof(aux),fp))!=NULL){ //Checks is reading lines
而是像这样使用 fscanf():
if((fscanf(fp,"%s",&aux)==1){ //Checks if is reading a string (a line)
问题不清楚文件中数据的实际格式。所以我决定对文件的每一行使用 name password
的格式。
这是一个可以干净地编译并执行所需功能的代码版本。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Register( void );
void Register( void )
{
char name[11];
char password[11];
char aux[11];
system("cls");
printf("Write a new username (no more than 10 characters, no spaces) ");
if( !fgets(name,sizeof(name),stdin) )
{
fprintf( stderr, "fgets for user name failed" );
exit( EXIT_FAILURE );
}
// implied else, fgets successful
// code should remove trailing new line, if any
char *newline;
if( (newline = strchr( name, '\n' ) ) )
{
*newline = '[=10=]';
}
FILE *fp=fopen("Users.txt","a+"); //I've created this .txt, just has one user which is admin
if ( !fp)
{
perror("fopen for Users.txt for read/write failed");
getchar();
exit( EXIT_FAILURE );
}
// implied else, fopen successful
while( fgets(aux,sizeof(aux),fp) )
{ //Checks is reading lines
// separate out the name field
char *token = strtok( aux, " " );
if( token )
{
if ((strcmp(name,aux))==0)
{ //Username already exists
printf("\nUsername already exists, try another: ");
rewind(fp); //Takes pointer to the beginning
}
}
}
// name not already in file
printf("\nChoose a password(no more than 10 characters): ");
if( !fgets(password,sizeof(password),stdin) )
{
perror( "fgets for password failed" );
fclose( fp ); // cleanup
exit( EXIT_FAILURE );
}
// implied else, fgets successful
//remove trailing newline
if( (newline = strchr( password, '\n' ) ) )
{
*newline = '[=10=]';
}
fprintf(fp,"%s %s\n",name, password);
fclose(fp);
printf("\nUser successfully registered\nName: %s\nPassword: %s",name,password);
getchar();
} // end function: Register