【发布时间】:2017-07-16 17:23:15
【问题描述】:
轻松访问计划
首先使用linux。我正在尝试制作一个程序,该程序将首先注册用户,以防他没有帐户,之后他将被引导到登录屏幕,在那里他将输入他的帐户详细信息,然后将登录。之后他将提供选项以提供对网站 e.t.c. 的轻松访问。就像如果用户输入 1,他将被定向到 f.b,2 代表 quora,依此类推。我成功地将程序编码到登录阶段,但我是在一个函数中完成的,即 main(),所以我认为如果我有单独的函数来执行特定任务会很好。这次我在单独的函数中对其进行了编码,但是这次当我尝试使用 fopen() 打开 FILE 时出现分段错误。还请告诉我一些使用控制台命令在浏览器中打开网站的方法。就像我们在 Windows 中一样(例如,开始 www.facebook.com)。这是代码。
#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) {
}
我在打开 FILE 的 sign_up 函数的第一行遇到错误。
【问题讨论】:
-
这
scanf("%c");不是你想的那样。你想让 scanf 把读取的字符存储在哪里? -
@SeekAddo 好吧,它是一个虚拟的 scanf()。我将它用作 gethc() 函数。
-
@SeekAdoo 顺便说一句,它不会将读取的字符存储在任何地方(如果我没记错的话),因为我没有指定它的位置
-
@SeekAddo 好吧,它给了我一些警告。如果我将它替换为 *c 它会使我的程序正确吗?
-
@Muneeb _“嗯,它是一个虚拟的 scanf()”_先从好书开始。
标签: c file-handling