English 中文(简体)
用另一种文字代替。
原标题:Replace string with another
  • 时间:2012-04-25 04:25:34
  •  标签:
  • c

我并不清楚我为什么要把我的<代码>替换为Word。 is在档案中,一味使用所有评论,等等。 我只是试图用从指挥线论点得到的文本取代。 我知道,我可能很远,只是想找到一种比较容易的办法。

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

int main ( int argc, char *argv[] )
{
if ( argc != 4 ) /* argc should be 2 for correct execution */
    {
        /* We print argv[0] assuming it is the program name */
        printf( "usage: %s filename
", 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
" );
        }
        else 
        {
           char string[100];
           int len = 0;
            /* read one character at a time from file, stopping at EOF, which
               indicates the end of the file.  Note that the idiom of "assign
               to a variable, check the value" used below works because
               the assignment statement evaluates to the value assigned. */
            while  ( (fscanf( file, "%s", string ) ) != EOF )
            {
                len = strlen(string);
                printf( "%s
", string );
                if(strcmp(string, wordReplace) == 0){
                //fseek (file, (-strlen(string) + 1), 1);
                //fputc(*replaceWord,file);
                //replaceWord++;
                //strcpy(string, replaceWord);
                fprintf(file,"%s",replaceWord);
                fputs(replaceWord, file);
                printf("
%d
", len);
                }
            }
            fclose( file );
        }
      }
    printf("
");
    return 0;
}
最佳回答

页: 1

在更正后,请注意,如果你想要取代<>>><>>/strong>上的文件,所替换的字数和字数必须相同。 你们最终将重写其他数据。 你们需要利用<代码>fseek 等职能,将内部档案点重新定位为fp。 将在<代码>fscanf后再行。

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签