に問題がありfgets
ます。文字列 "temp" を if ステートメントに保存できません。
コード:
1 #include <stdio.h>
2 #include <string.h>
3 #include<stdlib.h>
4
5 void moep(char** tmp){
6 FILE *fp;
7 fp=fopen("moep", "r");
8 int line_num = 1;
9 int find_result = 0;
10 char* str="jochen";
11 char temp[512];
12
13 while(fgets(temp, 512, fp) != NULL) {
14 if((strstr(temp, str)) != NULL) {
15 printf("A match found on line: %d \n", line_num);
16 tmp[find_result]=temp;
17 printf("\n%s\n", tmp[find_result] );
18 find_result++;
19 }
20 line_num++;
21 }
22 fclose(fp);
23 printf("tmp[0]:%s\n",tmp[0]);
24 tmp[1]="ich funktioniere";
25 }
26
27 int main(){
28
29 char* tmp[1];
30 moep(tmp);
31 printf("%s, %s\n",tmp[0],tmp[1]);
32 return 0;
33 }
moep は次のとおりです。
unwichtig1
jochen g
unwichtig2
unwichtig3
出力:
/一致する行が見つかりました: 2 ヨッヘン・グ tmp[0]:unwichtig3 ��Ł�, ich funktioniere
「jochen」を に保存できないのはなぜtmp[0]
ですか?