如何在特定单词和特定单词后的所有内容之前打印所有内容?

时间:2018-06-16 02:12:04

标签: c file while-loop printf filestream

我有一个名为test.xml的文件。我想要做的是将所有内容打印到一个名为的新文件中。然后打印我自己的自定义<start>n n n</start>。然后在</start>

之后打印所有内容

的test.xml:     

<more tests = 42 and more "34">

<start>10.213123 41.21231 23.15323</start>

<random stuff = "4">

<blah 234>

我尝试过使用memcmp,似乎无法正常工作。

// The main hub for generating new files with all the information in it.
void create_new_files(FILE *original, char *new_name, double x, double z,
                      double y, int val_pos, int incr_val, int max_val) {

    double i = 0;
    y = 10.1234;
    z = 30.231;

    FILE *generated_file = fopen(new_name, "r");

    // suppose to print everything before the <start>
    print_top_section(original);

    fprintf(generated_file, "<start>%lf %lf %lf</start>\n\n", i, z, y);

    // suppose to print everything after </start>
    print_bottom_section(original);

    fclose(generated_file);
}

// Prints the top section before it sees <start>
void print_top_section(FILE *original) {

    char line[MAX_LINE_LENGTH];
    while (memcmp(line, "<start>", sizeof("<start>")-1)) {

        fputs(line, stdout);
    }

}

// Prints the bottom section after it sees </start>
void print_bottom_section(FILE *original) {

    char line[MAX_LINE_LENGTH];
    while (memcmp(line, "</start>", sizeof("</start>")-1)) {

        fputs(line, stdout);
    }
}

尝试使其输出为:

<this is a test = 1>

<more tests = 42 and more "34">

<start>0 10.1234 30.231</start> // This is my own custom line.

<random stuff = "4">

<blah 234>

1 个答案:

答案 0 :(得分:0)

目标:打印不在<start></start>

之间的所有数据

您正在使用 memcmp() char [] 的新声明进行比较。

char line[MAX_LINE_LENGTH];
while (memcmp(line, "<start>", sizeof("<start>")-1))

那不会有太大成就。行的内容是什么?

现在,假设您的XML文件看起来如此:

CASE 1: 
ALONE LINE - NO TAGS

CASE 2:
<start>FOO</start>

CASE 3:
<start>
FOO
BAR
</start>

CASE 4: 
BEFORE <start> FOO  </start> AFTER

CASE 5: 
BEFORE <start>
FOO
BAR
</start> AFTER
  • 案例1:您的专线没有<start></start>代码
  • 案例2:您的专线只有<start></start>
  • 案例3:您的<start></start>单独在不同的行
  • 案例4:您的<start></start>在同一条线上,但并非一个人
  • 案例5:您的<start></start>分开但不单独

我们需要确保获取不在<start></start>之间的所有数据

<start>

之前获取数据
getline(&line, &len, fp)
char* start = strstr(line, "<start>")
int index = start - line; 
printf("%.*s", index, line);

</start>

之后获取数据
getline(&line, &len, fp)
char* end = strstr(line, "</start>")
int index = end - line + strlen("</start>"); 
printf("%s", line + index);

这是一个解决方案

FILE* fp = fopen("test.xml", "r");

// Set up for getline()
char* line;
size_t len;
ssize_t read;

bool enteredStart = false; // If we have entered into a <start>...</start> block
int index;
char* start; // Will be used to determine anything before <start>
char* end; // Will be used to determine anything after </start>

while ((read = getline(&line, &len, fp)) != -1) {
    // If the line contains both <start> and </start>
    if( ((start = strstr(line, "<start>")) != NULL) && ((end = strstr(line, "</start>")) != NULL) ) {   
        // Get the index where <start> exists and print everything before
        index = start - line; 
        printf("%.*s", index, line);

        // INSERT ANY CUSTOM INPUTS HERE

        // Get index where </start> has ended and print everything after
        index = end - line + strlen("</start>"); 
        printf("%s", line + index);

    }
    else if((start = strstr(line, "<start>")) != NULL) {
        // Get the index where <start> exists and print everything before
        index = start - line; 
        printf("%.*s", index, line);

        // We have entered <start> so we will stop printing until we encounter </start>
        enteredStart = true;

        // INSERT ANY CUSTOM INPUTS HERE
    }
    else if((end = strstr(line, "</start>")) != NULL) {
        // We have encounted </start> so we will start printing again
        enteredStart = false;

        // Get index where </start> has ended and print everything after
        index = end - line + strlen("</start>"); 
        printf("%s", line + index);
    }
    // If neither <start> nor </start> exist in the line 
    // and we are not in between <start> and </start> then we will print the line
    else if(!enteredStart) printf("%s", line);
}
fclose(fp);

注意:如果您不想打印空行或只有空格的行,可以自行修剪输出。