我有一个名为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>
答案 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
<start>
或</start>
代码<start>
和</start>
<start>
和</start>
单独在不同的行<start>
和</start>
在同一条线上,但并非一个人<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);
注意:如果您不想打印空行或只有空格的行,可以自行修剪输出。