Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
scanf()语句上的分段错误(内核转储)_C_File - Fatal编程技术网

scanf()语句上的分段错误(内核转储)

scanf()语句上的分段错误(内核转储),c,file,C,File,我已经测试了代码,错误似乎来自第二次scanf() 我已经对整个if语句进行了注释,以测试哪些有效,哪些无效。我还测试了strcmp()是否有效,以及它是否有效。我不明白为什么我会在scanf()上出现分割错误 #包括 #包括 #包括 //#包括“Admin.h” //#包括“customer.h” int main(int argc,char*argv[]) { typedef char*字符串; 字符串AdminUser=“Admin”; 字符串AdminPW=“Admin”; 字符串用户名

我已经测试了代码,错误似乎来自第二次scanf()

我已经对整个if语句进行了注释,以测试哪些有效,哪些无效。我还测试了strcmp()是否有效,以及它是否有效。我不明白为什么我会在scanf()上出现分割错误

#包括
#包括
#包括
//#包括“Admin.h”
//#包括“customer.h”
int main(int argc,char*argv[])
{
typedef char*字符串;
字符串AdminUser=“Admin”;
字符串AdminPW=“Admin”;
字符串用户名、密码;
printf(“欢迎使用网上银行/ATM系统”);
printf(“=======================================================\n\n\n”);
printf(“输入您的客户/管理员ID:”);
scanf(“%s”,用户名);
if(strcmp(用户名,管理员用户)==0){
printf(“输入您的客户/管理员密码:”);
scanf(“%s”,密码);
if(strcmp(密码,AdminPW)==0){
而(1){
打破
}
}
}
}
另外,如果有人能向我解释如何读写文件,那就太好了。我了解打开和关闭文件以及读写的基本知识,但具体来说,我需要知道的是如何将信息写入文件中,以便我可以引用它并从中提取信息片段。 例:文本文件中有一个个人银行信息列表,我特别想查看客户ID 12345。 我如何才能做到这一点,如何编辑它,如何删除它


我可能最终会弄明白的,但我会非常感谢你的帮助

用户名和密码只是声明为char*,它们没有分配任何内存,这将导致分段错误,您需要调用malloc为它们分配内存,或者将它们声明为字符数组

typedef char*String
是一个糟糕的主意。千万不要这样做。你没有给
username
一个值。stackoverflow上有很多关于读写文件的问题。搜索一下,你会得到很大的帮助。您还可以从cpluslus.com、tutorialspoint.com等网站和论坛获取工作示例。不要这样做
typedef char*String
,因为这会让您和任何阅读您代码的人感到困惑。此外,您需要为scanf分配或提供一些内存以将内容放入其中。
scanf("%s", password);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "Admin.h"
//#include "customer.h"


int main(int argc, char *argv[])
{
        typedef char * String;
        String AdminUser = "Admin";
        String AdminPW = "Admin";
        String username, password;

        printf("Welcome to Online Banking/ATM System\n");
        printf("====================================\n\n\n");

        printf("Enter your Customer/Admin ID: ");
        scanf("%s", username);

        if (strcmp(username, AdminUser) == 0) {
                printf("Enter your Customer/Admin Password: ");
                scanf("%s", password);

                if (strcmp(password, AdminPW) == 0) {
                        while(1){
                                break;
                        }
                }
        }
}