【问题标题】:Segmentation error, C File handling program分段错误,C 文件处理程序
【发布时间】: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


【解决方案1】:

scanf("%c") 需要一个指针来存储读取的字符。 scanf() 不知道您是否提供了指针,它只是从预期的堆栈位置读取目标地址。有效地 scanf() 采用随机地址并将字符写入那里。

使用getchar();char Dummy; scanf("%c",&amp;Dummy);

【讨论】:

  • %*c 也是一个东西。
  • @melpomene 还有一件事我想问。 fgets 一直跳过输入,因为我按 Enter。有什么办法解决吗?
  • @Muneeb 从不使用scanf 进行用户输入。仅使用 fgets
  • @melpomene strcmp() 是否也计算字符串中的 \n 字符?
  • @melpomene 还有一件事,Linux 终端中是否有一个命令可以直接在浏览器中打开网站/网页。就像我们在 windows cmd 提示符下启动命令一样。
猜你喜欢
  • 2018-11-18
  • 1970-01-01
  • 2019-07-05
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多