在列中添加整数值

时间:2017-09-25 02:50:50

标签: c

我必须在行中添加整数值。

  1. 我必须使用strstr函数找到String
  2. 从string_1(COORDINATES)到string_2(END_COORDINATES)我已将整数值添加到行中。
  3. 给我一些建议如何执行此操作。
  4. 示例输入文件

    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 );
            }
            */
        }
    }
    

    代码

    get()

1 个答案:

答案 0 :(得分:0)

如果我理解你的要求,那么“如何使用strstr来识别包含"COORDINATES"的行,然后对该行后面的行执行某些操作,直到我到达包含"END_COORDINATES"的行,其余行保持不变。“

当你需要找出一个函数man strstr时(或者你没有安装手册页 strstr(3) - Linux man page)时,从你总是开始的地方开始。它有什么参数take?返回什么?(“指向子字符串开头的指针,如果找不到子字符串则为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.

仔细看看,如果您有其他问题,请告诉我。