替换字符串

问题描述:

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

int main (int argc, char *argv[]) 
{ 
    if (argc != 4) /* argc should be 4 for correct execution */ 
    { 
     /* Print argv[0] assuming it is the program name */ 
     printf("usage: %s filename\n", argv[0]); 
    } 
    else 
    { 
     // We assume argv[1] is a filename to open 
     char* wordReplace = argv[1]; 
     char* replaceWord = argv[2]; 
     FILE *file = fopen(argv[3], "r+"); 
     /* fopen returns 0, the NULL pointer, on failure */ 
     if (file == 0) 
     { 
      printf("Could not open file\n"); 
     } 
     else 
     { 
      char string[100]; 
      int len = 0;int count = 0;int i = 0;int k = 0; 
      while ((fscanf(file, "%s", string)) != EOF) 
      { 
       len = strlen(string); 
       count++; 
       char charray[len+1]; 
       if(count == 1) 
       { 
        for (i = 0; i < len; i++) 
        { 
         charray[i] = replaceWord[i]; 
         printf("%c\n", charray[i]); 
        } 
       } 
       //printf("%c\n", charray[0]); 
       printf("%s\n", string); 
       if(strcmp(string, wordReplace) == 0) 
       { 
        for(k = 0; k < strlen(replaceWord); k++) 
        { 
         fseek (file, (-(long)len), SEEK_CUR); 
         fputc(charray[k],file); 
         //replaceWord++; 
        } 
        //strcpy(string, replaceWord); 
        //fprintf(file,"%s",replaceWord); 
        //fputs(string, file); 
        //printf("\n%d\n", len); 
       }  
      } 
      fclose(file); 
     } 
    } 
    printf("\n"); 
    return 0; 
} 

此代码目前工作在正常更换的第一个字,但如果有,我想改写为替换单词或单词的其他地方出现在文本多个单词将不能正常改变它,它会改变它ram垃圾等。我很好奇,如果有人可以引导我为什么谢谢你的理由。替换字符串

+0

您的使用信息不符合要求的使用。它应该看起来像'Usage:%s search replace filename'。 – 2012-04-26 14:56:10

+0

在'%s'中使用'fscanf()'意味着当您将结果回显到输出时,将会丢失输入文件中的所有空格。 (OTOH,它简化了字符串比较代码。)'fseek()'逻辑有点奇怪。在打印任何内容之前,为什么不直接比较传入的字符串和匹配词:如果两者相同,则打印替换;否则,打印传入的字符串。没有'fseek()'担心。你只需要一个'fseek()'。 – 2012-04-26 14:58:43

+0

好吧,我必须将文件中的文本更改为第三个单词,如果它们匹配的话,幸运的是,如果有帮助,他们改为必须是相同的大小。 附:印刷声明只是在那里测试,并确保其他一切正在工作,我的道歉不清晰。 – thekevshow 2012-04-26 15:08:45

假设的话都是一样的长度(如果不是你有相当多的更多的问题): 假设你有一个4个字符的字: fseek (file, (-(long)len), SEEK_CUR);将回到位置0(4-4),fputc(charray[k],file);将更新到1的位置,那么你又回来了4这是一个错误,但由于你没有检查fseek的返回值,你不会知道这一点。此时的算法是行不通的更多,因为你的假设文件位置都是错误的

编辑:

if(strcmp(string, wordReplace) == 0) 
{ 
    fseek (file, (-(long)len), SEEK_CUR); 
    for(k = 0; k < strlen(replaceWord); k++) 
    {       
     fputc(charray[k],file); 
    } 

} 
fflush(file); //you need to flush the file since you are switching from write to read 

编辑2:原因冲水:从4.5.9.2 ANSI C,在C99 7.19相似段落.5.3):

当以更新模式打开文件('+'作为mode参数中的第二个或第三个字符)时,输入和输出都可以在关联的流上执行。但是,如果没有对fflush函数或文件定位函数(fseek,fsetpos或rewind)的干预调用,则输出可能不会直接跟随输入,并且输入可能不会直接跟随输出,而不需要中间调用文件定位函数,除非输入操作遇到文件结束。

读写你有FSEEK之间已经所以这不是一个问题

+0

好点,错过了那一个 – JeremyP 2012-04-26 15:42:58

+0

有没有关于此问题的解决方案的任何建议,并且它们将与需要的长度相同。用编码编辑的 – thekevshow 2012-04-27 02:13:33

+0

。就我个人而言,我也会重新考虑你的命名约定,在同一个函数中有两个变量名'replaceWord'和'wordReplace'会引起混淆。使用'string'作为变量名也是有争议的。这可能只是一个练习,但是你想将它们放入几年后需要重新理解的生产代码中吗? – msam 2012-04-27 07:12:16