如何在适当的位置替换文件之间的数据 [英] how to replace data between files at proper position
问题描述
实际上我想在代码中进行一些更改。我读取输入sample.xml文件。
<? < span class =code-summarycomment> xml version = 1.0 编码 = UTF-8 独立 = no < span class =code-summarycomment>? >
< !DOCTYPE label SYSTEM label.dtd >
< 标签 _ FORMAT = BARCODE _ QUANTITY = 1 _ PRINTERNAME = P1 >
< label >
< 变量 < span class =code-attribute> name = PRTNUM > 234243242 < / variable >
< 变量 名称 = QUANTY > 004342 < / variable >
< 变量 名称 = PONUMB > 844463541 < / variable >
< 变量 名称 = SERIAL > DZ12 < / variable >
< 变量 名称 = REV > 234235 < / variable >
< variable name = UNITS > 234235 < / variable >
< < span class =code-leadattribute> / label >
< ; / labels >
&解析后的数据放在parsed.txt中
234243242
004342
844463541
DZ12
234235
234235
这个数据然后与barcode.txt合并
?c0001
?f260
?L
D11
H30
R0000
C0040
1X1100000100010B300300003003
181100202900027产品编号
181100202900097 [PRTNUM]
1e5504002400030B
1X1100002300010L300003
191100202000030数量
191100202000080 [QUANTY]
1e5504001500040B
1X1100001400010L300003
1X1100001400150L003090 >
191100202000170P.O.No
191100202000220 [PONUMB]
1e5504001500180B
191100201200030供应商
1e3304000700030B
1X1100000600010L300003
181100200300030Serial
181100200300090 [序列号]
171100300900190Rev
171100300300190 [REV]
171100300900240Units
171100300300240 [单位]
1X1100000100180L003130
Q0001
E
但是在合并数据的同时,无论xml&中包含的标记名称如何,它都会被合并。条形码文件。
例如
如果我把[QUANTY]放在[PRTNUM]的位置,它会放置相同的数据234243242(实际上为[ PRTNUM])。
如何使我的程序可以从barcode.txt文件中读取[...]内的数据。然后将解析文件中的相应数据放在该位置。
我的c代码xmldpl.txt
#include< stdlib.h>
#include< stdio.h>
#include< conio.h>
#include< DIR.h>
#include< string.h>
int main()
{
clrscr();
char a [ 1 ];
char cBuffer [ 50 ] = { 0 跨度>};
char ch,cSkipdata;
FILE * fPtr;
FILE * fPparse;
FILE * fPin;
FILE * fPout;
char * Ptr;
getcwd(cBuffer, 100 );
printf( 当前目录是%s \ n \ n,cBuffer );
fPtr = fopen( sample.xml, r); / * 打开XML文件* /
fPparse = fopen( parse.txt, W跨度>);
if (fPtr)
{
printf(< span class =code-string> 文件为\ n \ n);
scanf( %c,& a [ 1 跨度>]);
// 在>之间寻找测试和<
cSkipdata = 1 ;
while ((ch = getc(fPtr))!= EOF) / * 读取字符* /
{
if (ch == ' <')
cSkipdata = 1 跨度>;
if (!cSkipdata&& ch!= ' ')
{
putchar(ch);
// fputc(ch,stdout);
fputc(ch,fPparse );
}
/ * 写字符* /
if (ch == ' >')
cSkipdata = 0 ;
}
fclose(fPtr);
fclose(fPparse);
}
else
{
printf ( 文件无法打开\ n);
}
fPin = fopen( barcode.txt , r); / * 打开DPL文件* /
fPout = fopen( final.txt, W跨度>); / * 打开输出final以保存changea * /
fPparse = fopen( parse.txt, R跨度>);
scanf( %c,& a [ 1 跨度>]);
printf( \ n \ nnfinal.txt是\ n);
cSkipdata = 0 ;
while ((ch = getc(fPin))!= EOF)
{
< span class =code-keyword> if (ch == ' [') / * 如果条件为真,则跳过数据* /
{
int iLine = 0 ;
cSkipdata = 1 ;
while ((ch = getc(fPparse))!= EOF) // f读取字符
{
// 如果换行,我们有输出数据中断
if ((ch == ' \ n')&&(iLine> 0))
断裂跨度>; // 行尾
// 如果不是空格输出字符
if (ch > ' ')
{
// 有效字符输出
iLine ++;
putchar(ch);
fputc(ch,fPout);
}
}
}
如果(cSkipdata)
{
if (ch == ' ]')
cSkipdata = 0 ;
}
else
{
putchar(ch);
fputc(ch,fPout);
}
}
fclose(fPin);
fclose(fPout);
fclose(fPparse);
getch();
}
您真的要使用 C $ c $执行此类任务吗? c>'到金属'(没有使用库)?
你知道使用,例如, C ++
和一个 XML
解析库会让你的生活更轻松。
这就是说你需要存储已解析的 XML
变量一个容器,相当于 C ++
标准库容器 map< string,>
(或 map< ; string,>
- 您也可以使用 unordered_map
- )。
然后,在解析条形码文件时,你可以通过在这样的地图中查找值来添加所需的信息。
如果,例如,使用 C#
是一个选项,你的任务可能很糟糕。
嘿,任何人都可以提供帮助我吗?
Actually i want to do few changes in code. i read input sample.xml file.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE labels SYSTEM "label.dtd">
<labels _FORMAT ="BARCODE" _QUANTITY="1" _PRINTERNAME="P1">
<label>
<variable name= "PRTNUM">234243242</variable>
<variable name= "QUANTY">004342</variable>
<variable name= "PONUMB">844463541</variable>
<variable name= "SERIAL">DZ12</variable>
<variable name= "REV">234235</variable>
<variable name= "UNITS">234235</variable>
</label>
</labels>
& parsed data is placed in parsed.txt
234243242
004342
844463541
DZ12
234235
234235
this data is then merged with barcode.txt
c0001
f260
L
D11
H30
R0000
C0040
1X1100000100010B300300003003
181100202900027Part No
181100202900097[PRTNUM]
1e5504002400030B
1X1100002300010L300003
191100202000030Quantity
191100202000080[QUANTY]
1e5504001500040B
1X1100001400010L300003
1X1100001400150L003090
191100202000170P.O.No
191100202000220[PONUMB]
1e5504001500180B
191100201200030Supplier
1e3304000700030B
1X1100000600010L300003
181100200300030Serial
181100200300090[SERIAL]
171100300900190Rev
171100300300190[REV]
171100300900240Units
171100300300240[UNITS]
1X1100000100180L003130
Q0001
E
But while merging the data it is merged irrespective of the tag name that is included in xml & barcode file.
e.g
if i put [QUANTY] at the position of [PRTNUM] it will place the same data 234243242 (which actually for [PRTNUM]).
How to make it possible that my program will read the data inside [...] from barcode.txt file & then place the corresponding data from parsed file at that position.
my c code xmldpl.txt
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<DIR.h>
#include<string.h>
int main()
{
clrscr();
char a[1];
char cBuffer[50] = {0};
char ch, cSkipdata;
FILE* fPtr;
FILE* fPparse;
FILE* fPin;
FILE* fPout;
char* Ptr;
getcwd(cBuffer, 100);
printf("The current directory is %s\n\n",cBuffer);
fPtr = fopen("sample.xml", "r"); /* Open XML file*/
fPparse = fopen("parse.txt","w");
if(fPtr)
{
printf("The file is\n\n");
scanf("%c",&a[1]);
// look for test between > and <
cSkipdata=1;
while ((ch = getc(fPtr)) != EOF) /* read character */
{
if(ch == '<')
cSkipdata=1;
if(!cSkipdata && ch != ' ')
{
putchar(ch);
// fputc(ch ,stdout);
fputc(ch, fPparse);
}
/* write character */
if(ch == '>')
cSkipdata=0;
}
fclose(fPtr);
fclose(fPparse);
}
else
{
printf("file can not open\n");
}
fPin = fopen("barcode.txt", "r"); /* Open DPL file*/
fPout = fopen("final.txt", "w"); /* Open the output final to save changea*/
fPparse = fopen("parse.txt","r");
scanf("%c",&a[1]);
printf("\n\nfinal.txt is \n");
cSkipdata = 0;
while((ch = getc(fPin)) != EOF)
{
if(ch == '[') /* Skip the data if condition is true*/
{
int iLine=0;
cSkipdata = 1;
while ( (ch = getc(fPparse)) != EOF) //f read a character
{
//if newline and we have output data break
if((ch=='\n') && (iLine>0) )
break; // end of line
//if not space output character
if(ch > ' ')
{
// valid character output
iLine++;
putchar(ch);
fputc(ch, fPout);
}
}
}
if(cSkipdata)
{
if(ch == ']')
cSkipdata = 0;
}
else
{
putchar(ch);
fputc(ch ,fPout);
}
}
fclose(fPin);
fclose(fPout);
fclose(fPparse);
getch();
}
Have you really to perform such a task usingC
'to the metal' (that is without using libraries)?
You know using, for instance,C++
and aXML
parsing library would make your life easier.
That said you need to store the parsedXML
variables in a container equivalent to theC++
standard library containermap<string,>
(ormap<string,>
- you could also use anunordered_map
-).
Then, on parsing the barcode file you could add the required info by looking up the value in such map.
If, for instance, usingC#
is an option, the your task might becoma almost trivial.
hey is anyone out there who can help me?
这篇关于如何在适当的位置替换文件之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!