ページ 11

文字列検索について

Posted: 2010年8月27日(金) 21:23
by 屑鐵
ファイルの文字列を検索して配列に代入し、検索したいと思っています
しかしどうしても以下のエラーが出ます。何をどう変えればよいのか教えてください。


1> main01.cpp
1>c:\users\owner\main01.cpp(56): error C2086: 'char *result' : 再定義されました。
1>c:\users\owner\main01.cpp(17) : 'result' の宣言を確認してください。
========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ==========


#define _CRT_SECURE_NO_DEPRECATE 1 /* VisualC++2005 での警告抑制 */
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <cstring>

int main(void)
{

FILE *fp;
int c;
int j=0;
char *buff;
char *find="<form name=\"delform\" action=";
char *result;



fp = fopen( "C:/MyC/index.txt", "r" );
if( fp == NULL )
{
puts( "index.txtが開けません\n" );

return 1;
}

/* 1文字ずつ読み込んで、表示していくループ */
while( 1 ) /* ファイルの末尾まで */
{
c = fgetc( fp );
if( feof( fp ) ){ break; } /* ファイルの末尾まで */
printf( "%c", c );

j = j+1;/*while内を数える*/
buff[j] = c;

}

fclose( fp );

/* 終了前の挿入句 */
printf("読み込んだ文字数を表示します\n続行するにはエンターキーを押してください…\n");
rewind(stdin);
getchar();

printf("文字数は%dです\n", strlen(buff));

/* 終了前の挿入句 */
printf("続行するにはエンターキーを押してください…");
rewind(stdin);
getchar();

/* 文字列検索 そしてそのアドレス返却 */
char *result = strstr( buff, find );/*検索:*/

if (*result == NULL) {
printf("Not Found\n");
} else {
printf("%s\n", result); // "<form name=\"delform\" action="
}





return 0;
}

Re:文字列検索について

Posted: 2010年8月27日(金) 21:44
by あたっしゅ
>int main(void)
>{
>
> FILE *fp;
> int c;
> int j=0;
> char *buff;
> char *find="<form name=\"delform\" action=";
> char *result;

"char *result;" を削除する。
あと、エラー・メッセージは出ていないが

> /* 文字列検索 そしてそのアドレス返却 */
> char *result = strstr( buff, find );/*検索:*/
>
> if (*result == NULL) {
> printf("Not Found\n");
> } else {
> printf("%s\n", result); // "<form name=\"delform\" action="
> }

の "if (*result == NULL ) {"
result には、"*" つけない。

Re:文字列検索について

Posted: 2010年8月27日(金) 22:58
by 屑鐵
ありがとうございます
とりあえずはエラーがでなくなりました