简体   繁体   English

在文件中找到字符串后替换它旁边的字符

[英]Replacing character next to it after find the string in file

My code is to accept the values from the command line args and search for string and replace the next character after the string found in the line where the keyword in each line is separated by separots. 我的代码是接受来自命令行args的值并搜索字符串并替换在每行中的关键字由separots分隔的行中找到的字符串之后的下一个字符。

Basically after finding the character position from the buffer string which corresponds to file pointer position of that string, then update the character at fp+X. 基本上,在从缓冲区字符串中找到与该字符串的文件指针位置相对应的字符位置之后,然后在fp + X处更新字符。 I want to know is there a way to do it? 我想知道有办法吗?

I am ok with C++ too. 我也熟悉C ++。

I have the file.txt like below 我有像下面这样的file.txt

FOO,val1=0,val2=0
BAR,val1=0,val2=0
TOO,val1=0,val2=0

Command line 命令行

#./a.out FOO val1 1

mycode.c mycode.c中

char        buffer[kKEYWORD_SIZE];
FILE*       fd;

if ((fd = fopen(file.txt, "r+")) != NULL)
{
    do
    {
        memset(buffer, 0x0, kKEYWORD_SIZE);
        if (fgets(buffer, kKEYWORD_SIZE, fd) != NULL)
        {
           //storing the entire line into pointer to character array.
            for (index = 0, token = strtok_r(buffer, sep, &last); token; token = strtok_r(NULL, sep, &last), index++)
                strcpy(*(keyword + index), token);


           // the first keyword found and search for 2nd keyword and replaces its value   
            if (strcmp(*(keyword + 0), key1) == 0)
            {
               if ( ( find = strstr(buffer, key2 )) != NULL )
                {
                      // I want fp pointers to move it to find+5
                      fputs(fp,argv[3]);
                }

it would be greatly appreciated if any one throw a light on this 如果有人对此有所了解,将不胜感激

Well it is not very clear to me, what are you trying there, but If this is not what you need, please let me know: 好吧,我不是很清楚,你在那里尝试什么,但如果这不是你需要的,请告诉我:

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

int findFOO( const char *const ptr, const char *const substr );
int search( const char *const src, const char *str );

size_t findOCC ( char *const src, const char *substr );

int main ( void )
{
    char arr[] = "FOO,val1=0,val2=0" ;

    printf("Before: %s\n", arr );

    size_t len = findOCC( arr, "var1" );
    arr[len] = '1';

    printf("After:  %s\n " , arr );

}

int findFOO( const char *const ptr, const char *const substr )
{
    char *ret = strstr( ptr, substr );
    if ( ret == NULL )
    {
        return 0;
    }
    return 1;
}


int search( const char *const src, const char *const str )
{
    int i, j, firstOcc;
    i = 0, j = 0;

    while (src[i] != '\0')
    {

        while (src[i] != str[0] && src[i] != '\0')
            i++;

        if (src[i] == '\0')
            return (-1);

        firstOcc = i;

        while (src[i] == str[j] && src[i] != '\0' && str[j] != '\0')
        {
            i++;
            j++;
        }

        if (str[j] == '\0')
            return ( firstOcc );
        if (src[i] == '\0')
            return (-1);

        i = firstOcc + 1;
        j = 0;
    }
    return 0;
}

size_t findOCC ( char *const src, const char *substr )
{
    char buffer[strlen( src) + 1 ];
    strcpy( buffer, src );
    if ( findFOO ( src, "FOO" ) )   /// Search if there is a Line containing FOO
    {

        int ret = search( buffer, substr );
        if ( ret ) /// found a Match
        {
            char *tmp = strtok( buffer, "=" );
            return strlen( tmp ) + 1; /// return the index
        }

    }
    return 0;
}

Output: 输出:

Before: FOO,val1=0,val2=0
After:  FOO,val1=1,val2=0

I was not searching in a file, because you need a way to modify your buffer, so I will let you to do the rest of the Job here. 我没有在文件中搜索,因为你需要一种方法来修改你的缓冲区,所以我会让你在这里做其余的工作。

Of course there are a lot of error checking needed here, but I am sure that you can figure out what you need to do. 当然,这里需要进行大量的错误检查,但我相信你可以弄清楚你需要做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM