亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3894 | 回復(fù): 9
打印 上一主題 下一主題

[C] 請教:這個代碼的segmentation fault 在哪里 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2013-11-09 20:45 |只看該作者 |倒序瀏覽
我做了一個代碼,預(yù)期功能是從兩個文件list1.txt和list2.txt中讀取中文名單,然后從list2中找出list1里不存在的名字。每個名字占一行,由于是從excel拷貝過來到text中的,所以名字后面有個TAB。但這個代碼運行后總是出現(xiàn)segmentation fault的提示。請問哪里出現(xiàn)了問題,謝謝!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>

  5. #define SIZE 60
  6. #define MAX  20

  7. int main(void)
  8. {
  9.     char names_1[SIZE][MAX];
  10.     char names_2[SIZE][MAX];
  11.     char * pnames_1[SIZE], * pnames_2[SIZE];
  12.     char nms_1[SIZE][MAX];
  13.     char nms_2[SIZE][MAX];
  14.     char * pnms_1[SIZE], * pnms_2[SIZE];
  15.     char * fnd[SIZE];
  16.     int cnt_1 = 0, cnt_2 = 0, cnt_fnd = 0;
  17.     int i,j;
  18.     FILE *fp1, *fp2, *fp3;

  19.     if((fp1 = fopen("list1.txt", "r")) == NULL)
  20.     {
  21.         fputs("Error reading \"listing1.txt\".", stderr);
  22.         exit(1);
  23.     }
  24.     if((fp2 = fopen("list2.txt", "r")) == NULL)
  25.     {
  26.         fputs("Error reading \"listing2.txt\".", stderr);
  27.         exit(1);
  28.     }
  29.     if((fp3 = fopen("results.txt", "w")) == NULL)
  30.     {
  31.         fputs("Error writing \"results.txt\".", stderr);
  32.         exit(1);
  33.     }
  34.     while(cnt_1 <= SIZE && fgets(names_1[cnt_1], MAX, fp1) != NULL)
  35.     {
  36.         pnames_1[cnt_1] = names_1[cnt_1];
  37.         pnms_1[cnt_1] = nms_1[cnt_1];
  38.         cnt_1++;
  39.     }
  40.     while(cnt_2 <= SIZE && fgets(names_2[cnt_2], MAX, fp2) != NULL)
  41.     {
  42.         pnames_2[cnt_2] = names_2[cnt_2];
  43.         pnms_2[cnt_2] = nms_2[cnt_2];
  44.         cnt_2++;
  45.     }

  46.     for(i = 0; i < cnt_1; i++)
  47.     {
  48.         while(*(pnames_1[i]) && !isspace(*pnames_1[i]))
  49.         {
  50.             *pnms_1[i] = *pnames_1[i];
  51.             pnames_1[i]++;
  52.             pnms_1[i]++;
  53.         }
  54.         * pnms_1[i] = '\0';
  55.         puts(pnms_1[i]);
  56.     }
  57.     for(i = 0; i < cnt_2; i++)
  58.     {
  59.         while(*(pnames_2[i]) && !isspace(*pnames_2[i]))
  60.         {
  61.             *pnms_2[i] = *pnames_2[i];
  62.             pnames_2[i]++;
  63.             pnms_2[i]++;
  64.         }
  65.         * pnms_2[i] = '\0';
  66.         puts(pnms_2[i]);
  67.     }
  68.     for(i = 0; i < cnt_1; i++)
  69.         for(j = 0; j < cnt_2; j++)
  70.             if(strcmp(pnms_2[j], pnms_1[i]) != 0)
  71.             {
  72.                 fnd[i] = pnms_1[i];
  73.                 cnt_fnd++;
  74.             }
  75.     if(cnt_fnd > 0)
  76.     {
  77.         for(i = 0; i < cnt_fnd; i++)
  78.             fputs(fnd[i], fp3);
  79.         fprintf(fp3, "\n%d found.", cnt_fnd);
  80.     }
  81.     else
  82.         fputs("\nNo results found.", fp3);
  83.     printf("\n%d\n", cnt_fnd);
  84.     puts("\nDone!");
  85.     fclose(fp1);
  86.     fclose(fp2);
  87.     fclose(fp3);

  88.     return 0;
  89. }
復(fù)制代碼

論壇徽章:
0
2 [報告]
發(fā)表于 2013-11-09 22:19 |只看該作者
我修正了一些錯誤,但主要錯誤依然存在:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>

  5. #define SIZE 60
  6. #define MAX  20

  7. int main(void)
  8. {
  9.     char names_1[SIZE][MAX];
  10.     char names_2[SIZE][MAX];
  11.     char * pnames_1[SIZE], * pnames_2[SIZE];
  12.     char nms_1[SIZE][MAX];
  13.     char nms_2[SIZE][MAX];
  14.     char * pnms_1[SIZE], * pnms_2[SIZE];
  15.     char * fnd[SIZE];
  16.     int cnt_1 = 0, cnt_2 = 0, cnt_fnd = 0;
  17.     int i,j;
  18.     FILE *fp1, *fp2, *fp3;

  19.     if((fp1 = fopen("list1.txt", "r")) == NULL)
  20.     {
  21.         fputs("Error reading \"listing1.txt\".", stderr);
  22.         exit(1);
  23.     }
  24.     if((fp2 = fopen("list2.txt", "r")) == NULL)
  25.     {
  26.         fputs("Error reading \"listing2.txt\".", stderr);
  27.         exit(1);
  28.     }
  29.     if((fp3 = fopen("results.txt", "w")) == NULL)
  30.     {
  31.         fputs("Error writing \"results.txt\".", stderr);
  32.         exit(1);
  33.     }
  34.     while(cnt_1 <= SIZE && fgets(names_1[cnt_1], MAX, fp1) != NULL)
  35.     {
  36.         pnames_1[cnt_1] = names_1[cnt_1];
  37.         pnms_1[cnt_1] = nms_1[cnt_1];
  38.         cnt_1++;
  39.     }
  40.     while(cnt_2 <= SIZE && fgets(names_2[cnt_2], MAX, fp2) != NULL)
  41.     {
  42.         pnames_2[cnt_2] = names_2[cnt_2];
  43.         pnms_2[cnt_2] = nms_2[cnt_2];
  44.         cnt_2++;
  45.     }

  46.     for(i = 0; i < cnt_1; i++)
  47.     {
  48.         while(*(pnames_1[i]) && !isspace(*pnames_1[i]))
  49.         {
  50.             *pnms_1[i] = *pnames_1[i];
  51.             pnames_1[i]++;
  52.             pnms_1[i]++;
  53.         }
  54.         * pnms_1[i] = '\0';
  55.     }
  56.     for(i = 0; i < cnt_2; i++)
  57.     {
  58.         while(*(pnames_2[i]) && !isspace(*pnames_2[i]))
  59.         {
  60.             *pnms_2[i] = *pnames_2[i];
  61.             pnames_2[i]++;
  62.             pnms_2[i]++;
  63.         }
  64.         * pnms_2[i] = '\0';
  65.     }
  66.     for(i = 0; i < cnt_1; i++)
  67.         for(j = 0; j < cnt_2; j++)
  68.             if(strcmp(nms_1[i], nms_2[j]) == 0)
  69.             {
  70.                 fnd[i] = nms_1[i];
  71.                 cnt_fnd++;
  72.             }
  73.     if(cnt_fnd > 0)
  74.     {
  75.         for(i = 0; i < cnt_fnd; i++)
  76.         {
  77.             fputs(fnd[i], fp3);
  78.             putchar('\n');
  79.         }
  80.         fprintf(fp3, "\n%d found.", cnt_fnd);
  81.     }
  82.     else
  83.         fputs("\nNo results found.", fp3);
  84.     printf("\n%d\n", cnt_fnd);
  85.     puts("\nDone!");
  86.     fclose(fp1);
  87.     fclose(fp2);
  88.     fclose(fp3);

  89.     return 0;
  90. }
復(fù)制代碼

論壇徽章:
0
3 [報告]
發(fā)表于 2013-11-13 10:53 |只看該作者
問一下樓主:在line41:     pnms_1[cnt_1] = nms_1[cnt_1];這句之前,對nms_1[cnt_1]賦初值了嗎?

論壇徽章:
0
4 [報告]
發(fā)表于 2013-11-13 10:54 |只看該作者
問一下樓主:在line41:     pnms_1[cnt_1] = nms_1[cnt_1];這句之前,對nms_1[cnt_1]賦初值了嗎?

論壇徽章:
0
5 [報告]
發(fā)表于 2013-11-13 19:05 |只看該作者
似乎問題出在fputs上

論壇徽章:
0
6 [報告]
發(fā)表于 2013-11-13 21:18 |只看該作者
回復(fù) 2# mcmay


    我在MacOS上運行了你的程序,自己建了小數(shù)據(jù)list1和list2,沒有你說的錯誤,能執(zhí)行。
你需要把數(shù)據(jù)也貼上來,看看。也許是數(shù)組越界錯誤,大數(shù)據(jù)才顯現(xiàn)出來。

論壇徽章:
780
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉雞
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥豬
日期:2014-04-22 16:13:09獅子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53處女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
7 [報告]
發(fā)表于 2013-11-14 16:46 |只看該作者
回復(fù) 6# Hadron74
同意樓上的,我在linux也可以編譯運行,
只是結(jié)果也不大對,只找出了第一個在list2不在list1的名字,
我用的是樓主2樓改進(jìn)過的代碼。


論壇徽章:
8
CU大牛徽章
日期:2013-04-17 10:59:39CU大;照
日期:2013-04-17 11:01:45CU大;照
日期:2013-04-17 11:02:15CU大牛徽章
日期:2013-04-17 11:02:36CU大牛徽章
日期:2013-04-17 11:02:58技術(shù)圖書徽章
日期:2013-12-04 10:48:50酉雞
日期:2014-01-03 10:32:30辰龍
日期:2014-03-06 15:04:07
8 [報告]
發(fā)表于 2013-11-14 17:15 |只看該作者
既然是必現(xiàn),那還不簡單……

windows,用你的vs單步執(zhí)行,甚至只是用ide執(zhí)行都行,它自己會告訴你到那行訪問違例的;達(dá)到及格線的ide都允許你查看當(dāng)時的調(diào)用棧以及變量值。

如果是linux,編譯時加-g參數(shù),然后ulimit -c unlimited允許吐core;等程序core掉,gdb 程序名 -c core.xxxx,然后bt看一下調(diào)用棧,或者用p看一下變量值。

有這些信息,結(jié)合程序邏輯稍微做一下推斷……就這幾行代碼,不是太奇葩的情況都是秒殺……


先學(xué)會用好你手頭的工具……尤其是在論壇提問時。這些工具的報告,或許你看起來是天書,但在別人眼里,很可能就是赤裸裸的答案……

論壇徽章:
5
2015年迎新春徽章
日期:2015-03-04 09:58:1115-16賽季CBA聯(lián)賽之上海
日期:2016-01-18 13:24:3015-16賽季CBA聯(lián)賽之佛山
日期:2016-01-27 10:13:0515-16賽季CBA聯(lián)賽之北控
日期:2016-08-04 22:33:2115-16賽季CBA聯(lián)賽之山西
日期:2016-08-06 15:49:33
9 [報告]
發(fā)表于 2013-11-15 09:49 |只看該作者
cnt_1 <= SIZE

pnames_1[cnt_1] = names_1[cnt_1];


等于可以嗎?

fgets(names_1[cnt_1], MAX, fp1)
MAX可以嗎?

論壇徽章:
1
CU十二周年紀(jì)念徽章
日期:2013-10-24 15:41:34
10 [報告]
發(fā)表于 2013-11-15 11:35 |只看該作者
調(diào)試core文件
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP