怪哉,怪哉,夜壶扛盖盖,有人见过吗?(编程技术类)

dchg2000 2005-03-31 09:57:39
代码是标准I/O写的,
在VC下OK,运行很好
在LINUX下运行就“段错误”
^_^

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int get_input(const char* ask, char** opts, int len, char* result)
{
int i;
int loop = 3;

while ( loop-- ) {

printf("%s ", ask);
for (i = 0; i < len; i++) {
printf("%s ", opts[i]);
}
printf("? ");
printf("[%s]: ", opts[0]);
gets(result);
if ( strcmp(result, "") == 0 ) { /* default */
strcpy(result, opts[0]);
return 0;
}

for (i = 0; i < len; i++) {
if ( strcmp(result, opts[i]) == 0 ) {
return i;
}
}
}

return -1;
}

int main(int argc, char **argv)
{
int i;
int rs;
FILE* fp;
char sn[17];
char** opts;
char result[128];
char digest[33];

opts = (char**) malloc(sizeof(char*) * 20);
for (i = 0; i < 20; i++) {
opts[i] = (char*)malloc(30);
}
strcpy(opts[0], "1000");
strcpy(opts[1], "2000");
strcpy(opts[2], "3000");

if ( (fp = fopen("sn.txt", "rb")) != NULL ) {
fgets(sn,17,fp);
fclose(fp);
}
else {
printf("can't find file sn.txt\n");
return -1;
}
sn[16] = '\0';
printf("Read sn.txt: %s\n", sn);
//fclose(fp);

if ( (fp = fopen("serial.txt", "wb")) == NULL ) {
printf("can't create file serial.txt\n");
return -1;
}
fprintf(fp, "License\n");
fprintf(fp, "Please not modify this file and binary transfers.\n");
fprintf(fp, "--------------------- BEGIN -------------------------\n");
fprintf(fp, "SN %s\n", sn);
rs = get_input("Please input version", opts, 3, result);
if ( rs < 0 ) { printf("invalid input, exit ...\n"); return -1; }
fprintf(fp, "VERSION %s\n", result);
strcpy(digest, "1234567890asdfghjkkkkk");
fprintf(fp, "%s\n", digest);

fprintf(fp, "--------------------- END ---------------------------\n");
fclose(fp);
printf("Generate serial.txt file successfully\n", sn);
return 0;
}
...全文
125 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jellen 2005-04-01
  • 打赏
  • 举报
回复
把你程序中的gets(result)用fgets()函数代替,Linux对堆栈溢出很小心(gets()函数没有保护)
Sword_Sharp 2005-03-31
  • 打赏
  • 举报
回复
有没有产生core文件?

gdb 程序命 core

看看
hs_guanqi 2005-03-31
  • 打赏
  • 举报
回复
没错啊,
dchg2000 2005-03-31
  • 打赏
  • 举报
回复
//fclose(fp);
改为
fclose(fp);
dchg2000 2005-03-31
  • 打赏
  • 举报
回复
有人遇见过吗?
呵呵!
很还改的啊
dchg2000 2005-03-31
  • 打赏
  • 举报
回复
sn.txt
03L9O0871M1459N1
serial.txt
License
Please not modify this file and binary transfers.
--------------------- BEGIN -------------------------
SN 03L9O0871M1459N1
VERSION 1000
1234567890asdfghjkkkkk
--------------------- END ---------------------------

23,128

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧