#include <stdio.h>
#include <string.h>
#include <windows.h>
#define TRAGE_FILE "Setup1.vdproj"
#define TMP_FILE "tmp.txt"
#define BUF_SIZE 256
// BOOL DeleteSubItem(const char *strItem, const char *strSubItem)
// {
// HANDLE hTargeFile;
// HANDLE hTmpFile;
//
// hTargeFile = CreateFile(TRAGE_FILE, // open MYFILE.TXT
// GENERIC_READ | GENERIC_WRITE, // open for reading and writing
// FILE_SHARE_READ | FILE_SHARE_WRITE, // share for reading and writing
// NULL, // no security
// OPEN_EXISTING, // existing file only
// FILE_ATTRIBUTE_NORMAL, // normal file
// NULL); // no attr. template
//
// if (hTargeFile == INVALID_HANDLE_VALUE)
// {
// printf("Could not open targe file!\n");
// }
//
// hTmpFile = CreateFile(TMP_FILE, // open MYFILE.TXT
// GENERIC_READ | GENERIC_WRITE, // open for reading and writing
// FILE_SHARE_READ | FILE_SHARE_WRITE, // share for reading and writing
// NULL, // no security
// CREATE_ALWAYS, // existing file only
// FILE_ATTRIBUTE_NORMAL, // normal file
// NULL); // no attr. template
//
// if (hTmpFile == INVALID_HANDLE_VALUE)
// {
// printf("Could not create tmp file!\n");
// }
//
// BOOL bFindItem = FALSE;
// BOOL bFindSubItem = FALSE;
// char strReadBuf[BUF_SIZE] = {0};
// DWORD dwReadBytes = BUF_SIZE;
// DWORD dwBytesRead = 0;
// DWORD dwOffset = 0;
//
// char strWriteBuf[BUF_SIZE] = {0};
// DWORD dwWriteBytes = BUF_SIZE;
// DWORD dwBytesWrite = 0;
//
// while (ReadFile(hTargeFile, &strReadBuf, dwReadBytes, &dwBytesRead, NULL))
// {
// // 到了文件结尾
// if (dwBytesRead == 0)
// {
// break;
// }
//
// // 找到了Item且到了Item块的结尾或找到SubItem
// if (bFindItem && strcmp(strReadBuf, "}") == 0
// || bFindSubItem)
// {
// break;
// }
//
// // 找到Item
// if (bFindItem)
// {
// dwOffset += dwBytesRead;
//
// if (strstr(strReadBuf, "TargetName") != NULL)
// {
// // 找到要删除的subitem
// if (strstr(strReadBuf, strSubItem) != NULL)
// {
// while (ReadFile(hTargeFile, &strReadBuf, dwReadBytes, &dwBytesRead, NULL))
// {
// if (strcmp(strReadBuf, "}") == 0) // 到了SubItem块结结尾
// break;
// memset(strReadBuf, 0, BUF_SIZE);
// }
// }
// else
// {
// SetFilePointer (hTargeFile, dwOffset, NULL, FILE_CURRENT);
// dwOffset = 0; // 该sub块没有要找的内容
// while (ReadFile(hTargeFile, &strReadBuf, dwReadBytes, &dwBytesRead, NULL))
// {
// WriteFile(hTmpFile, strReadBuf, dwBytesRead, &dwBytesWrite, NULL);
//
// if (strcmp(strReadBuf, "}") == 0) // 到了SubItem块结结尾
// break;
// memset(strReadBuf, 0, BUF_SIZE);
// }
// }
// }
// continue;
// }
//
// // 首先找到item
// if (strcmp(strReadBuf, strItem) == 0)
// {
// bFindItem = TRUE;
// ReadFile(hTargeFile, &strReadBuf, dwReadBytes, &dwBytesRead, NULL);
// }
//
//
//
// WriteFile(hTmpFile, strReadBuf, dwBytesRead, &dwBytesWrite, NULL);
//
// memset(strReadBuf, 0, BUF_SIZE);
//
// }
//
// while (ReadFile(hTargeFile, &strReadBuf, dwReadBytes, &dwBytesRead, NULL))
// {
// if (dwBytesRead == 0) // end of the file
// {
// break;
// }
//
// WriteFile(hTmpFile, strReadBuf, dwBytesRead, &dwBytesWrite, NULL);
// memset(strReadBuf, 0, BUF_SIZE);
// }
//
//
// CopyFile(TMP_FILE, TRAGE_FILE, FALSE);
//
// CloseHandle(hTmpFile);
// DeleteFile(TMP_FILE);
//
// CloseHandle(hTargeFile);
//
// return TRUE;
// }
//
BOOL DeleteSubItem(const char *strItem, const char *strSubItem)
{
FILE *pTargeFile;
FILE *pTmpFile;
pTargeFile = fopen(TRAGE_FILE, "r+");
if (pTargeFile == NULL)
{
printf("Could not open targe file!\n");
}
pTmpFile= fopen(TMP_FILE, "w+");
if (pTmpFile == INVALID_HANDLE_VALUE)
{
printf("Could not create tmp file!\n");
}
BOOL bFindItem = FALSE;
BOOL bFindSubItem = FALSE;
char strReadBuf[BUF_SIZE] = {0};
DWORD dwOffset = 0;
fpos_t position = 0; // 用于记录subitem的位置
while (fgets(strReadBuf, BUF_SIZE, pTargeFile))
{
// 找到了Item且到了Item块的结尾或找到SubItem
if (bFindItem && /*strcmp(strReadBuf, "}") == 0*/strstr(strReadBuf, "}\n")
|| bFindSubItem)
{
fputs(strReadBuf, pTmpFile);
memset(strReadBuf, 0, BUF_SIZE);
break;
}
// 找到Item
if (bFindItem)
{
dwOffset += strlen(strReadBuf);
if (strstr(strReadBuf, "TargetName") != NULL)
{
// 找到要删除的subitem
if (strstr(strReadBuf, strSubItem) != NULL)
{
bFindSubItem = TRUE;
while (fgets(strReadBuf, BUF_SIZE, pTargeFile))
{
// 到了SubItem块结结尾
if (/*strcmp(strReadBuf, "}") == 0*/strstr(strReadBuf, "}\n"))
break;
memset(strReadBuf, 0, BUF_SIZE);
}
}
else
{
fsetpos(pTargeFile, &position); // 回到subitem块头
while (fgets(strReadBuf, BUF_SIZE, pTargeFile))
{
fputs(strReadBuf, pTmpFile);
// 到了SubItem块结尾
if (/*strcmp(strReadBuf, "}") == 0*/strstr(strReadBuf, "}\n"))
{
fgetpos(pTargeFile, &position);
break;
}
memset(strReadBuf, 0, BUF_SIZE);
}
}
}
continue;
}
// 首先找到item
// if (strcmp(strReadBuf, strItem) == 0)
if (strstr(strReadBuf, strItem) != NULL)
{
bFindItem = TRUE;
fputs(strReadBuf, pTmpFile); // 跳过strItem与{
memset(strReadBuf, 0, BUF_SIZE);
fgets(strReadBuf, BUF_SIZE, pTargeFile);
fgetpos(pTargeFile, &position);
}
fputs(strReadBuf, pTmpFile);
memset(strReadBuf, 0, BUF_SIZE);
}
while (fgets(strReadBuf, BUF_SIZE, pTargeFile))
{
fputs(strReadBuf, pTmpFile);
memset(strReadBuf, 0, BUF_SIZE);
}
fclose(pTargeFile);
fclose(pTmpFile);
CopyFile(TMP_FILE, TRAGE_FILE, FALSE);
DeleteFile(TMP_FILE);
return TRUE;
}
int main(int argc, char *argv[])
{
DeleteSubItem("\"File\"", "test1.txt");
DeleteSubItem("\"File\"", "RTSI.ico");
DeleteSubItem("\"File\"", "test1 - 副本 (3).txt");
return 0;
}
Code Test
最新推荐文章于 2020-04-12 23:11:41 发布