2011-03-30 105 views
0

喜逢身体 可以有一个人帮助我与THI问题 我有这是继C文件处理问题

hi Hello this is my Hello to 
the Hello world 

这是继

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

void main() 
{ 
    FILE *fp,*fout; 
    int i=0,len_string; 
    char SearchText[]="Hello"; /* You can replace this text */ 
    char ReplaceText[]="Help"; /*You can also replace this text. */ 
    char temp[30]; 
    fp=fopen("ram.txt","a+"); 
    fout=fopen("temp.txt","a+"); 
    rewind(fp); /* for going to start of file. */ 
    if(fp==NULL || fout==NULL) 
    { 
     printf("File couldn't be opened "); 
     exit(0); 
    } 
    len_string=strlen(SearchText); 
    while(!feof(fp)) 
    { 
     for(i=0;i<len_string;i++) 
     { 
      temp[i]=fgetc(fp); 
     } 
     temp[i]='\0'; 
     if(strcmp(SearchText,temp)==0) /* the stricmp() is used for comparing both string. */ 
     { 

      fprintf(fp,"%s ",ReplaceText); 
      fprintf(fout,"%s",ReplaceText); 
      fflush(fp); 
      fclose(fp); 
      fclose(fout); 
      exit(1); 
     } 
    } 
    fclose(fp); 
    fclose(fout); 
} 
一个文本文件,我用C编写代码

现在我出来放就是这样

hi Hello this is my Hello to 
the Hello world 

帮助帮助 我做错了什么? 如何替换你好,以帮助我的文本文件? 如何获得我的输出?

hi Help this is my Hello to 
the Help world 

任何人都可以用代码解释吗?

+2

main'的'返回类型'int'。另外,请尝试缩进您的代码。 – 2011-03-30 13:26:26

+0

是为了你自己的原因缩进你的代码 – Blorgbeard 2011-03-30 13:26:54

+0

什么是'temp.txt'应该做什么? – nmichaels 2011-03-30 13:30:36

回答

2

您在5个字符组中搜索字符串“Hello”。所以你正在看你的文件是这样的:

hi He 
llo t 
his i 
s my 
Hello 
to\n 
the H 
ello 
world 

其中只有一个匹配你在找什么。您应该逐行读取文件(使用更大的缓冲区以防万一),并在每行上搜索/替换您的搜索文本。

此外,您在退出时失败0,在成功时退出1(这是倒退; 0表示成功,其他任何事情都意味着失败,传统上)。而你在第一场比赛后退出,而不是继续寻找更多。

要通过线线读取并执行搜索/替换,做这样的事情:

FILE *f = ...; 
char buf[1024]; 
while (fgets(buf, sizeof(buf), f)) { 
    char *pos; 
    while ((pos = strstr(buf, search)) != NULL) { 
     char temp = *pos; 
     *pos = 0; 
     fprintf(out, "%s%s", buf, replace); 
     *pos = temp; 
     buf = pos + strlen(search); 
    } 
    fprintf(out, "%s", buf); 
} 

这不是理想的,由于路线长(> 1023字)将得到切成片,可能在搜索标记的中间,但它在大多数情况下都可以使用。

+0

请解释我hoew逐行阅读和搜索所需的字符串 – user513164 2011-03-31 02:58:24

0

你已经逐行读入一个临时字符数组,并用strstr找到你好,那么事情会更快.. 检查strstr函数。

+0

请你可以用代码解释 – user513164 2011-03-31 02:59:37

0
#include <stdio.h> 
#include <string.h> 
#define BUF_LEN 100 
#define STR_REQ "Hello" 
main() 
{ 
    FILE * read_file, * write_file; 
    read_file = fopen("file_read.dat", "r"); 
    if(read_file == NULL) 
    { 
     perror("Unable to open file for reading"); 
     exit(-1); 
    } 
    else 
    { 
     write_file = fopen("file_write.dat", "w"); 
     if(write_file == NULL) 
     { 
      perror("Unable to open file for writing"); 
      exit(-1); 
     } 
     else 
     { 
      char temp_buf[BUF_LEN], req_buf[BUF_LEN+strlen(STR_REQ)+10]; 
      char * index; 
      while(!feof(read_file)) 
      { 
       memset(temp_buf, 0, BUF_LEN); 
       fscanf(read_file, "%[^\n]", temp_buf); 
       fgetc(read_file);   /*To remove \n at last*/ 
       index = strstr(temp_buf, STR_REQ); 
       if(index !=NULL) 
       { 

        memset(req_buf, 0, BUF_LEN+strlen(STR_REQ)+10); 
        //strncpy(req_buf, temp_buf, index-temp_buf); 
        memcpy(req_buf, temp_buf, index - temp_buf);    /*This will copy upto the match*/ 

        strcat(req_buf, "Help ");           /*Copy the help word*/ 

        strcat(req_buf, index+strlen(STR_REQ)); 

       } 
       else 
       { 
        strcpy(req_buf, temp_buf); 
       } 
       fprintf(write_file, "%s\n", req_buf); 
      } 

     } 
    } 
} 

有这样的代码..它应该工作...如果你有任何问题,你可以问我