分段错误,C文件处理程序
轻松访问程序 首先使用linux。我正在尝试做一个程序,将首先注册一个用户,以防他没有帐户,然后他将被引导到一个登录屏幕,他将输入他的帐户详细信息,然后将登录。在那之后,他将得到一些选项,可以方便地访问e.t.c.网站,比如如果用户输入1,他将被引导到f.b,2代表quora等等。我成功地将程序编码到登录阶段,但我是在单个函数(即main())中完成的,因此我认为如果我有单独的函数来执行特定任务会更好。这次我用不同的函数对其进行了编码,但当我尝试使用fopen()打开文件时,我遇到了分段错误。另外,请告诉我一些使用控制台命令在浏览器中打开网站的方法。就像我们在windows中所做的那样(比如从www.facebook.com开始)。这是密码分段错误,C文件处理程序,c,file-handling,C,File Handling,轻松访问程序 首先使用linux。我正在尝试做一个程序,将首先注册一个用户,以防他没有帐户,然后他将被引导到一个登录屏幕,他将输入他的帐户详细信息,然后将登录。在那之后,他将得到一些选项,可以方便地访问e.t.c.网站,比如如果用户输入1,他将被引导到f.b,2代表quora等等。我成功地将程序编码到登录阶段,但我是在单个函数(即main())中完成的,因此我认为如果我有单独的函数来执行特定任务会更好。这次我用不同的函数对其进行了编码,但当我尝试使用fopen()打开文件时,我遇到了分段错误。另
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct user_data {
char name[50];
unsigned long password;
};
struct user_data new_user; // Will hold the data of new
struct user_data data_ver; // Will hold the data read
void sign_up(void);
void sign_in(void);
int main(void) {
beginning: // Beginning label in case of invalid input
printf("\t\t\t\t WELCOME TO EASY ACCESS APPLICATION");
printf("\n\n\nIf you have an account press 1.\n\nPress 2 for sign up.");
char user_choice; // Wil hold the
// user_choice i.e whether he wants to sign up / sign in
user_choice = getchar();
if (user_choice == '1') {
sign_in(); // In case of 1 goto sign in page
}
else if (user_choice == '2') {
sign_up(); // Opening file);
// In case of 2 goto sign up page
}
else {
printf("Invalid input. Try again.\n\n");
puts("Press any key to continue...");
getchar();
system("clear");
goto beginning;
}
return 0;
}
void sign_up(void) {
FILE *data = fopen("data.txt", "a");
if (data == NULL) {
printf("Unable to open file.");
scanf("%c");
system("clear");
}
system("clear");
printf("\t\t----------------------------\n"
"\t\t| |\n"
"\t\t| SIGN UP PAGE |\n"
"\t\t| |\n"
"\t\t----------------------------");
printf("\n\nName:");
scanf("%c"); // Dummy scanf
gets(new_user.name); // Getting name into the struct
printf("\nPassword.");
scanf("%lu", &new_user.password); // Getting pass into the struct
fprintf(data, "%s %lu\n", new_user.name, new_user.password); //Feeding data into FILE
system("clear");
printf("\n\nSign up complete. :) ");
printf("\n\nYou will now be directed to the sign in page. ");
printf("\nPress any key to contine...");
scanf("%c");
system("clear");
fclose(data);
}
void sign_in(void) {
}
#包括
#包括
#包括
结构用户数据{
字符名[50];
未签名的长密码;
};
结构用户\u数据新建\u用户;//将保存新的数据
结构用户数据版本//将保存读取的数据
作废登记(作废);
作废签字(作废);
内部主(空){
开始://输入无效时的开始标签
printf(“\t\t\t\t欢迎使用简易访问应用程序”);
printf(“\n\n\n如果您有帐户,请按1。\n\n按2注册。”);
char user_choice;//将保留
//用户选择,即他是否想要注册/登录
user_choice=getchar();
如果(用户_选项=='1'){
sign_in();//如果有1个转到登录页
}
else if(用户_选项=='2'){
注册();//打开文件);
//如果有2个转到注册页面
}
否则{
printf(“无效输入。请重试。\n\n”);
放置(“按任意键继续…”);
getchar();
系统(“清除”);
转到开始;
}
返回0;
}
作废登记(作废){
FILE*data=fopen(“data.txt”,“a”);
如果(数据==NULL){
printf(“无法打开文件”);
scanf(“%c”);
系统(“清除”);
}
系统(“清除”);
printf(“\t\t--------------------------------------\n”
“\t\t | | \n”
“\t\t |注册页面| \n”
“\t\t | | \n”
“\t\t----------------------------------------------------”;
printf(“\n\n名称:”);
scanf(“%c”);//虚拟scanf
获取(new_user.name);//将名称获取到结构中
printf(“\n密码”);
scanf(“%lu”,&new_user.password);//进入结构
fprintf(数据,“%s%lu\n”,new_user.name,new_user.password);//正在将数据导入文件
系统(“清除”);
printf(“\n\n注册完成:”);
printf(“\n\n您现在将被定向到登录页面。”);
printf(“\n按任意键继续…”);
scanf(“%c”);
系统(“清除”);
fclose(数据);
}
无效签入(无效){
}
我在打开文件的注册函数的第一行中遇到错误
scanf(“%c”)
需要一个指针来存储读取的字符。scanf()不知道是否提供了指针,它只是从预期的堆栈位置读取目标地址。实际上scanf()接受一个随机地址并将字符写入其中
使用
getchar()代码>或字符虚拟;scanf(“%c”、&Dummy)代码>这个scanf(“%c”)代码>不是你所想的。您希望scanf将读取的字符存储在哪里?@SeekAddo嗯,这是一个虚拟scanf()。我将它用作gethc()函数。@seekado,顺便说一句,它不会将读取的字符存储在任何地方(如果我没有错的话),因为我没有指定它的location@SeekAddo这给了我一些警告。如果我将其替换为*c,我的程序会正常运行吗?@Muneeb“嗯,这是一个虚拟的scanf()”。%*c
也是一件事。@melpomene我想问另一件事。由于我按enter键,FGET一直跳过输入。有什么办法解决吗?@Muneeb从不使用scanf
进行用户输入。仅使用fgets
。@melpomene strcmp()是否也计算字符串中的\n字符?@melpomene还有一件事,linux终端中是否有直接在浏览器中打开网站/网页的命令。就像我们在windows cmd提示符中有start命令一样。