gpt4 book ai didi

c - 如何打印特定单词之前的所有内容以及特定单词之后的所有内容?

转载 作者:行者123 更新时间:2023-11-30 16:30:52 25 4
gpt4 key购买 nike

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

测试.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>

最佳答案

目标:打印 <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);

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

关于c - 如何打印特定单词之前的所有内容以及特定单词之后的所有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50884425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com