C 如何打印特定单词前的所有内容以及特定单词后的所有内容?
我有一个名为test.xml的文件。我要做的是把文件之前的所有内容打印到一个名为的新文件中。然后打印我自己的自定义C 如何打印特定单词前的所有内容以及特定单词后的所有内容?,c,file,while-loop,printf,filestream,C,File,While Loop,Printf,Filestream,我有一个名为test.xml的文件。我要做的是把文件之前的所有内容打印到一个名为的新文件中。然后打印我自己的自定义n。然后打印 test.xml: 10.213123 41.21231 23.15323 我试过使用memcmp,但似乎不起作用 // The main hub for generating new files with all the information in it. void create_new_files(FILE *original, char *new_name
n
。然后打印
test.xml:
10.213123 41.21231 23.15323
我试过使用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);
}
}
//用于生成包含所有信息的新文件的主中心。
无效创建新文件(文件*原始,字符*新文件名,双x,双z,
双y,内值位置,内增量,内最大值){
双i=0;
y=10.1234;
z=30.231;
FILE*generated_FILE=fopen(新的_名称,“r”);
//假设在打印之前打印所有内容
打印顶部部分(原件);
fprintf(生成的_文件,“%lf%lf%lf\n\n”,i,z,y);
//假设以后打印所有内容
打印底部部分(原件);
fclose(生成的_文件);
}
//在顶部看到之前打印它
无效打印顶部部分(文件*原件){
字符行[最大行长度];
while(memcmp(行“,”的大小(“”-1)){
FPUT(线路、标准输出);
}
}
//在看到后打印底部部分
无效打印\u底部\u部分(文件*原件){
字符行[最大行长度];
while(memcmp(行“,”的大小(“”-1)){
FPUT(线路、标准输出);
}
}
尝试使其输出为:
<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 10.1234 30.231//这是我自己的自定义行。
目标:打印所有不在
和
您正在使用memcmp()与新声明的char[]进行比较
char line[MAX_LINE_LENGTH];
while (memcmp(line, "<start>", sizeof("<start>")-1))
char行[最大行长度];
while(memcmp(行“,”的大小(“”-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:
单独行-无标记
案例2:
福
案例3:
福
酒吧
案例4:
前后
案例5:
之前
福
酒吧
之后
- 案例1:您的线路没有
或
标记 - 案例2:您的线路只有
和 - 案例3:您的
和
单独在不同的行中 - 案例4:您的
和
在同一行,但不是单独一行 - 案例5:您的
和
在不同的行中,但不是单独的
和
之间的数据
在
getline(&line,&len,fp)
char*start=strstr(行“”)
int index=起始线;
printf(“%s”,索引,行);
在
getline(&line,&len,fp)
char*end=strstr(行“”)
int index=结束行+strlen(“”);
printf(“%s”,行+索引);
这里有一个解决方案
FILE*fp=fopen(“test.xml”、“r”);
//为getline()设置
字符*行;
尺寸透镜;
阅读;
bool enteredStart=false;//如果我们已经进入一个。。。块
整数指数;
char*start;//将用于确定之前的任何内容
字符*结束;//将用于确定之后的任何内容
while((read=getline(&line,&len,fp))!=-1){
//如果该行同时包含和
如果((start=strstrstr(line,“”)!=NULL)和(&(end=strstrstr(line,“”)!=NULL)){
//获取存在的索引并打印之前的所有内容
索引=起始线;
printf(“%s”,索引,行);
//在此处插入任何自定义输入
//获取结束位置的索引,然后打印所有内容
索引=结束行+strlen(“”);
printf(“%s”,行+索引);
}
如果((开始=strstr(行“”)!=NULL),则为else{
//获取存在的索引并打印之前的所有内容
索引=起始线;
printf(“%s”,索引,行);
//我们已经进入,所以我们将停止打印,直到遇到
enteredStart=true;
//在此处插入任何自定义输入
}
如果((end=strstr(第行“”)!=NULL),则为else{
//我们遇到了一些问题,因此我们将再次开始打印
enteredStart=false;
//获取结束位置的索引,然后打印所有内容
索引=结束行+strlen(“”);
printf(“%s”,行+索引);
}
//如果线路中既不存在也不存在
//我们不在中间,然后我们将打印行
否则,如果(!enteredStart)printf(“%s”,第行);
}
fclose(fp);
注意:如果您不想打印空行或仅带空格的行,您可以自己修剪输出。这是与相同问题的另一部分吗?这里的答案有用吗?@SteveSummit这是另一个问题。我在那里得到了帮助,解决了另一个问题。我正在尝试打印
(int包括
标记之前和
(不包括
)之后的所有内容。@SarahChan.
是否总是在输入文件中的同一行中?我可以将此函数拆分为两个函数吗?因为我仍然需要执行fprintf(生成的\u文件,%lf%lf%lf\n\n“,i,z,y);
这基本上会将我自己的自定义数值放入文件中,因为目标是找到
和
并将我自己的数据放在它们之间。您不需要为此创建两个函数。我修改了原始解决方案。请参阅//在此处插入任何自定义输入,在那里您可以用自己的数据粘贴自己的标记。这将替换原始文件中的文件。哦,您使用了不同的方法。我正在考虑将其拆分为两个函数。我想将其分开,因为如果我将其拆分为两个函数,我可以打开和关闭两个文件,因为它必须从test.xml读取并将其打印为changed.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
getline(&line, &len, fp)
char* start = strstr(line, "<start>")
int index = start - line;
printf("%.*s", index, line);
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);