我必须将整数值添加到行。


我必须使用strstr函数找到String
从string_1(COORDINATES)到string_2(END_COORDINATES),我已将整数值添加到行中。
给我一些建议如何执行此操作。


输入文件示例

COORDINATES
5   10   20   30
5   10   20   30
5   10   20   30
5   10   20   30
5   10   20   30
5   10   20   30
5   10   20   30
5   10   20   30
END_COORDINATES
3   30   40   50
3   30   40   50
3   30   40   50
3   30   40   50
3   30   40   50


输出文件示例

COORDINATES
5   110   220   330
5   110   220   330
5   110   220   330
5   110   220   330
5   110   220   330
5   110   220   330
5   110   220   330
5   110   220   330
END_COORDINATES
3   30   40   50
3   30   40   50
3   30   40   50
3   30   40   50
3   30   40   50




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

FILE *file_input;
FILE *file_output;

#define COORDINATES     "COORDINATES"
#define END_COORDINATES "END_COORDINATES"

int main(){
    char line[256];
    int SI_no, X_cord, Y_cord, Z_cord;

    int change_in_X_cord, change_in_Y_cord, change_in_Z_cord;
    int user_X_input, user_Y_input, user_Z_input;

    user_X_input = 100;
    user_Y_input = 200;
    user_Z_input = 300;

    ///change in cordinates
    change_in_X_cord = X_cord + user_X_input;
    change_in_Y_cord = Y_cord + user_Y_input;
    change_in_Z_cord = Z_cord + user_Z_input;

    /// File input
    file_input  = fopen("File_output.domm", "r");
    if (file_input == NULL)
    {
        perror("fopen");
        exit(EXIT_FAILURE);
    }
    printf("%s \n " COORDINATES);

    /// File output
    file_output = fopen("File_output2.domm", "w");
    if (file_output == NULL)
    {
        perror("fopen");
        exit(EXIT_FAILURE);
    }

    printf("%s \n " END_COORDINATES);

    while(fgets(line, sizeof(line),file_input)) {
        if (!strstr(line, COORDINATES)){
            continue;
        }

        if (!fgets(line, sizeof line,file_input)) {
            return 3;
        }

        // pls give me suggestion how to perform this operation
        /*
        while(fscanf(file_input,"%d %d %d %d", &SI_no, &X_cord, &Y_cord, &Z_cord )) {
            if (strstr(line, END_COORDINATES)){
                return 0;
            }
            printf("%d %d %d %d \n", X_cord, Y_cord, Z_cord );
            printf("%d %d %d %d", change_in_X_cord, change_in_Y_cord, change_in_Z_cord );
        }
        */
    }
}

最佳答案

如果我理解您的要求,那就是“如何使用strstr识别包含"COORDINATES"的行,然后对该行之后的行进行一些操作,直到到达包含"END_COORDINATES"的行,剩下的行保持不变。”

当需要找出功能man strstr时(或如果没有安装手册页strstr(3) - Linux man page时,请从总是开始的地方开始。它使用什么参数?返回什么?(指向起点的指针)子字符串的值;如果未找到子字符串,则为NULL”)

为了在代码中将返回值用于比较目的,最好将其保存,例如

...
while (fgets (buf, MAXC, fp)) {
    char *p = buf;
    /* does buf contain "COORDINATES"? */
    if ((p = strstr (buf, "COORDINATES"))) {
    ...


因此,您已阅读每一行并找到了"COORDINATES"。给定保存在buf中的"COORDINATES"返回值,如何测试strstr是否以p开头?

如果pbuf开头,"COORDINATES"将指向何处? (回想一下:“指向子字符串开头的指针”)如果bufp都指向"COORDINATES",则您已经找到了行"COORDINATES"

如果buf包含"COORDINATES",但不是以p != buf开头(例如"END_COORDINATES"),则说明您已找到"COORDINATES"

您如何知道是否已经找到"END_COORDINATES"但尚未遇到int coords = 1?最简单的方法是在遇到"COORDINATES"时简单地设置一个标志,例如0并将其设置为"END_COORDINATES"。然后,您只需要检查if (coords) { /* do stuff to rows */ }。例:

    int coords = 0;     /* flag indicating between tags */
    ...
    while (fgets (buf, MAXC, fp)) { /* read each line */
        char *p = buf;
        /* does buf contain "COORDINATES"? */
        if ((p = strstr (buf, "COORDINATES"))) {
            if (p == buf) { /* does buf begin with it? */
                printf ("start mod of rows.\n");
                coords = 1;  /* set flag */
            }
            else {  /* contains, but doesn't begin with */
                printf ("end mod of rows.\n");
                coords = 0;  /* unset flag */
            }
        }
        else if (coords) /* flag set, modify rows */
            printf ("modify: %s", buf);
    }
    ...


将各个部分放在一起,以一个示例说明如何使用strstr标识要修改的行(其余的取决于您),您可以执行以下操作:

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

#define MAXC 512

int main (int argc, char **argv) {

    char buf[MAXC] = "";
    int coords = 0;     /* flag indicating between tags */
    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!fp) {  /* validate file open for reading */
        fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
        return 1;
    }

    while (fgets (buf, MAXC, fp)) { /* read each line */
        char *p = buf;
        /* does buf contain "COORDINATES"? */
        if ((p = strstr (buf, "COORDINATES"))) {
            if (p == buf) { /* does buf begin with it? */
                printf ("start mod of rows.\n");
                coords = 1;  /* set flag */
            }
            else {  /* contains, but doesn't begin with */
                printf ("end mod of rows.\n");
                coords = 0;  /* unset flag */
            }
        }
        else if (coords) /* flag set, modify rows */
            printf ("modify: %s", buf);
    }

    if (fp != stdin) fclose (fp);     /* close file if not stdin */

    return 0;
}


使用/输出示例

$ ./bin/coords dat/coords.txt
start mod of rows.
modify: 5   10   20   30
modify: 5   10   20   30
modify: 5   10   20   30
modify: 5   10   20   30
modify: 5   10   20   30
modify: 5   10   20   30
modify: 5   10   20   30
modify: 5   10   20   30
end mod of rows.


仔细研究一下,如果您还有其他问题,请与我联系。

关于c - 在列中添加整数值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46396944/

10-11 16:33