我成功地从阅读器中获取了数据,并将其写入了文本文件。
检测到的标签示例: 4330-3031-3138-3031-2D32-2D31
但是,将标记数据写入文本文件的方式如下:
43
4330
4330-30
4330-3031
4330-3031-31
4330-3031-3138
4330-3031-3138-30
4330-3031-3138-3031
4330-3031-3138-3031-2D
4330-3031-3138-3031-2D32
4330-3031-3138-3031-2D32-2D
4330-3031-3138-3031-2D32-2D31
我试图使用if语句(count ++)将自上而下最长的最后一个值写入文本文件,但即使变量保留其值,也不知何故该数据不会写入文本文件。
Below are my codes:
{
// Format Tag and print on screen
void printTagData(TAG_DATA *pTagData)
{
fstream myTextFile,myTextFile2, myTextFile3;
string fileText,fileText2;
char tagBuffer[1024] = {0,};
char* pTagReportData = tagBuffer;
int index = 0;
int count = 0;
TCHAR resultBuffer[MAX_PATH];
for(index = 0; index < pTagData->tagIDLength; index++)
{
myTextFile.open("text1.txt",ios::app);
if (0 < index && index % 2 == 0)
{
*pTagReportData++ = '-';
}
sprintf(pTagReportData, "%02X", pTagData->pTagID[index]);
while (*pTagReportData) pTagReportData++;
stringstream ss;
ss << tagBuffer;
ss >> fileText;
myTextFile << fileText << endl;
myTextFile.close();
}
_stprintf(resultBuffer, TEXT("%S"), tagBuffer);
SendDlgItemMessage(g_hDlg, IDC_INVENTORY_LIST, LB_ADDSTRING, 0, (LPARAM)resultBuffer);
SendDlgItemMessage(g_hDlg, IDC_INVENTORY_LIST, WM_VSCROLL, (WPARAM)SB_BOTTOM, 0);
}
请随时给我任何提示/指导。 我是否应该编写另一个代码来提取与“ 4330-3031-3138-3031-2D32-2D31”匹配的模式并将其写入新的文本文件?
答案 0 :(得分:2)
此代码非常复杂。我会做这样的事情(为了清楚起见,代码未经测试,并且省略了错误处理):
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.boraji.tutorial.springboot</groupId>
<artifactId>spring-boot-hello-world-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<java.version>1.7</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
仅此而已。
答案 1 :(得分:2)
您正在循环中打开并附加到该文件,因此每次迭代仅获得一行。
相反,请先收集整个结果,然后将其写入文件。
这里的版本更“不是C,而是C ++”:
#include <iomanip> // For 'setw' and 'hex'
void printTagData(TAG_DATA *pTagData)
{
std::stringstream ss;
for(int index = 0; index < pTagData->tagIDLength; index++)
{
if (0 < index && index % 2 == 0)
{
ss << '-';
}
ss << std::hex << std::setw(2) << pTagData->pTagID[index];
}
std::string tag = ss.str();
std::ofstream myTextFile("text1.txt");
myTextFile << tag << endl;
TCHAR resultBuffer[MAX_PATH];
_stprintf(resultBuffer, TEXT("%S"), tag.c_str());
SendDlgItemMessage(g_hDlg, IDC_INVENTORY_LIST, LB_ADDSTRING, 0, (LPARAM)resultBuffer);
SendDlgItemMessage(g_hDlg, IDC_INVENTORY_LIST, WM_VSCROLL, (WPARAM)SB_BOTTOM, 0);
}