当我尝试运行它时出现分段错误(核心转储)。它可以完美地编译,但是出现错误,我也不知道为什么。文件写入必定有问题,因为没有它,效果很好。任何帮助将是巨大的。谢谢!#include <stdio.h>#include <time.h>#include <unistd.h>#include <crypt.h>#include <string.h>intmain(void){FILE *f=fopen("shadow1.txt","w"); if (f=NULL) { printf("ERROR"); } unsigned long seed[2]; char salt[] = "$1$........"; const char *const seedchars = "./0123456789ABCDEFGHIJKLMNOPQRST" "UVWXYZabcdefghijklmnopqrstuvwxyz"; char *password; int i; /* Generate a (not very) random seed. You should do it better than this... */ seed[0] = time(NULL); seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000); /* Turn it into printable characters from ‘seedchars’. */ for (i = 0; i < 8; i++) salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f]; /* Read in the user’s password and encrypt it. */ password = crypt(getpass("Password:"), salt); /* Print the results. */ //fprintf(f,"%s $ %s",password); printf("Success Registration to file !"); fclose(f); return 0;}
3 回答
开心每一天1111
TA贡献1836条经验 获得超13个赞
void Register(char u,char p) {
您可能希望这些是char *因为fprintf将它们视为字符串:
fprintf(f,"%s $ %s",u,p);
并且由于您传递了char *s:
char *password,*username;
//...
Register(username,password);
这很可能已被编译器警告捕获。从编译器得到答案的速度比从这里得到的速度要快得多。
如果您无法弄清楚程序为何无法运行,则可以使用启用所有需要的警告,-Wall -Wextra然后使用将警告变成错误-Werror。
- 3 回答
- 0 关注
- 327 浏览
添加回答
举报
0/150
提交
取消