我想编辑文件的某些位。我打开文件并确定第一个字节指针。因此,在 ChangeBit 函数中,将一些位替换为1。
当我想调试 ChangeBit 函数时,在for循环的第一次迭代中,调试光标从函数中跳出并且过程剂量不会终止,代码在函数后继续行。
怎么了?
void changeBit(char *ptr, int bitLen, int startPoint)
{
ptr += startPoint / 8;
startPoint %= 8;
int tmpBit = 1;
for (int i = startPoint; i < bitLen; i++)
{
ptr[i / 8] = (ptr[i / 8] | tmpBit << (7 - (i % 8)));
//after this line in first iteration, debug cursor jump out of function
}
}
int main(void)
{
// open and mmap() the file
hFile = CreateFile(input_file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
bytes_left = GetFileSize(hFile, NULL);
hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
stream_pos = (unsigned char*)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
changeBit(stream_pos,5,0); // not working
// copy all bytes to other memory but still not working
char *OutFileContent;
OutFileContent = malloc(bytes_left);
memcpy(OutFileContent, stream_pos, bytes_left);
changeBit(OutFileContent,5,0); // not working
//write new bits stream to out file
}
指针有问题吗?
答案 0 :(得分:1)
您可以在调试模式下使用“异常”设置窗口抛出异常:
https://msdn.microsoft.com/en-us/library/x85tt0dd.aspx
请同时检查以下代码:
hFile = CreateFile(input_file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
bytes_left = GetFileSize(hFile, NULL);
hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
stream_pos = (unsigned char*)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
我像下面的示例一样更改它,它在我身边很有效:
HANDLE hFile = NULL;
HANDLE hMap = NULL;
char *stream_pos = NULL;
LPCTSTR input_file_name = _T("C:\\Users\\xxx\\Desktop\\Test\\test.txt");
DWORD bytes_left = 0;
hFile = CreateFile(input_file_name, GENERIC_ALL, 0, NULL, OPEN_EXISTING, 0, NULL);
bytes_left = GetFileSize(hFile, NULL);
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
stream_pos = (char*)MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
该文件使用了只读属性,如果我们更改属性,它将运行良好。