私は300.000行以上のファイルを持っているC言語を使ってコードを書こうとしています。ですから、1回以上出現する特定の単語(#P5P5)を見て、この単語のそれぞれの行を読みたいと思います。しかし、私のプログラムは、最初の単語を検索して停止します...私は他のものを見つけるためにループをしたいです。あなたは、whileループを停止し、ファイルから文字を読んで停止して休憩を、使用した単語を見つけたら#program in C - 単語とその単語の行を.txt形式で読むには? Cプログラム
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *arquivo = fopen("testando.txt", "r");
char caractere; // caractere a ser lido
int existe_linhas = 0;
int quant_linhas = 0;
int num = 0;
char pesquisa[] = "# 00"; // string a ser pesquisada
int pos = 0; // posicao de início da pesquisa
int encontrei = 0; // status da pesquisa
if(arquivo != NULL) //verifica se o arq foi aberto ok
{
while((caractere = fgetc(arquivo)) != EOF)
{
// vamos verificar se o caractere atual se iguala ao
// primeiro caractere da string a ser pesquisada
existe_linhas = 1; // há conteúdo no arquivo
if((caractere == pesquisa[0]) && (!encontrei))
{
encontrei = 1; // podemos continuar a pesquisa a partir daqui
pos = 0;
}
if(encontrei)
{
if(caractere == pesquisa[pos])
{
encontrei = 1; // continua a pesquisa
pos++; // incrementa a posição de busca
if(pos == strlen(pesquisa))
break;
}
else{
encontrei = 0;
}
}
if(caractere == '\n') //verificar se é quebra de linha
{
quant_linhas++;
}
} //fecha o while
if(existe_linhas)
quant_linhas++;
if(encontrei){
printf("A string pesquisada existe no arquivo");
}
else{
printf("A string pesquisada NAO existe no arquivo");
}
printf("\n\n");
printf("A string pesquisada está na linha %d.", quant_linhas);
fclose(arquivo); // libera o ponteiro para o arquivo
}
else
printf("Nao foi possivel abrir o arquivo.");
printf("\n\n");
system("PAUSE");
return 0;
}
こんにちは、 私は言葉を見つけた後、次の同じ言葉を探しています。私の.txtには何度もこの言葉があります。私はこの単語を見つける行の番号を知りたい。たとえば、3行目、200行目、1000行目など。この.txtはデータ.txtなので、データのブロックが300.000行以上あります。 – Gabriela