问题描述
我刚刚编写了一个C程序(Ubuntu平台,使用GCC作为编译器),其中我有很大的内存,所有内存中都填充有有意义的数据:
u_char* bigChunkOfMem = populateData(); // This works
int memSize = sizeOfChunk( bigChunkOfMem ); // This also works
麻烦的是,我需要将此数据提交给另一个我未编写的函数。此函数将文件描述符作为输入,因此我需要将数据复制到临时文件中:
FILE* myTmpFile = tmpfile();
printf("Now copying contents of bigChunkOfMem into myTmpFile!\n");
int i=0;
while( i < memSize ){
fputc( bigChunkOfMem[i],myTmpFile );
printf("...just copied bigChunkOfMem[%d]:%u\n",i,bigChunkOfMem[i]);
i++;
}
printf("\n");
rewind( myTmpFile );
我猜这在起作用...?我的输出是这样的:
Now copying contents of bigChunkOfMem into myTmpFile!
...just copied bigChunkOfMem[0]:101
...just copied bigChunkOfMem[1]:102
...just copied bigChunkOfMem[2]:103
...and so on...
但是,当然,我将myTmpFile提交给其他函数,但没有任何反应。另一个功能是不处理我的数据。
很可能我的数据格式错误。但是,暂时将其搁置一旁。在我的疑难解答中,确实让我感到困扰的是,在创建临时文件后,我没有办法检查它。我尝试使用fread():
char[200] buffer;
fread( buffer,memSize+1,1,myTmpFile );
printf("buffer is:: %s\n",buffer);
rewind( myTmpFile );
但这似乎没有用。输出为:
buffer is:: [[not my data]]
我假定临时文件在逻辑上与写入磁盘的“常规”文件在逻辑上没有什么不同,即,我应该能够使用与其他文件相同的所有工具检查临时文件。这个对吗?在线文档暗示了这一点,但是我找不到任何可以证实这一点的资料。谢谢。
解决方法
请尝试下面的代码,以了解如何在C中检查临时文件?
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#define BUFSIZE 1024
void PrintError(LPCTSTR errDesc);
int _tmain(int argc,TCHAR *argv[])
{
HANDLE hFile = INVALID_HANDLE_VALUE;
HANDLE hTempFile = INVALID_HANDLE_VALUE;
BOOL fSuccess = FALSE;
DWORD dwRetVal = 0;
UINT uRetVal = 0;
DWORD dwBytesRead = 0;
DWORD dwBytesWritten = 0;
TCHAR szTempFileName[MAX_PATH];
TCHAR lpTempPathBuffer[MAX_PATH];
char chBuffer[BUFSIZE];
LPCTSTR errMsg;
if(argc != 2)
{
_tprintf(TEXT("Usage: %s <file>\n"),argv[0]);
return -1;
}
// Opens the existing file.
hFile = CreateFile(argv[1],// file name
GENERIC_READ,// open for reading
0,// do not share
NULL,// default security
OPEN_EXISTING,// existing file only
FILE_ATTRIBUTE_NORMAL,// normal file
NULL); // no template
if (hFile == INVALID_HANDLE_VALUE)
{
PrintError(TEXT("First CreateFile failed"));
return (1);
}
// Gets the temp path env string (no guarantee it's a valid path).
dwRetVal = GetTempPath(MAX_PATH,// length of the buffer
lpTempPathBuffer); // buffer for path
if (dwRetVal > MAX_PATH || (dwRetVal == 0))
{
PrintError(TEXT("GetTempPath failed"));
if (!CloseHandle(hFile))
{
PrintError(TEXT("CloseHandle(hFile) failed"));
return (7);
}
return (2);
}
// Generates a temporary file name.
uRetVal = GetTempFileName(lpTempPathBuffer,// directory for tmp files
TEXT("DEMO"),// temp file name prefix
0,// create unique name
szTempFileName); // buffer for name
if (uRetVal == 0)
{
PrintError(TEXT("GetTempFileName failed"));
if (!CloseHandle(hFile))
{
PrintError(TEXT("CloseHandle(hFile) failed"));
return (7);
}
return (3);
}
// Creates the new file to write to for the upper-case version.
hTempFile = CreateFile((LPTSTR) szTempFileName,// file name
GENERIC_WRITE,// open for write
0,// do not share
NULL,// default security
CREATE_ALWAYS,// overwrite existing
FILE_ATTRIBUTE_NORMAL,// normal file
NULL); // no template
if (hTempFile == INVALID_HANDLE_VALUE)
{
PrintError(TEXT("Second CreateFile failed"));
if (!CloseHandle(hFile))
{
PrintError(TEXT("CloseHandle(hFile) failed"));
return (7);
}
return (4);
}
// Reads BUFSIZE blocks to the buffer and converts all characters in
// the buffer to upper case,then writes the buffer to the temporary
// file.
do
{
if (ReadFile(hFile,chBuffer,BUFSIZE,&dwBytesRead,NULL))
{
// Replaces lower case letters with upper case
// in place (using the same buffer). The return
// value is the number of replacements performed,// which we aren't interested in for this demo.
CharUpperBuffA(chBuffer,dwBytesRead);
fSuccess = WriteFile(hTempFile,dwBytesRead,&dwBytesWritten,NULL);
if (!fSuccess)
{
PrintError(TEXT("WriteFile failed"));
return (5);
}
}
else
{
PrintError(TEXT("ReadFile failed"));
return (6);
}
// Continues until the whole file is processed.
} while (dwBytesRead == BUFSIZE);
// The handles to the files are no longer needed,so
// they are closed prior to moving the new file.
if (!CloseHandle(hFile))
{
PrintError(TEXT("CloseHandle(hFile) failed"));
return (7);
}
if (!CloseHandle(hTempFile))
{
PrintError(TEXT("CloseHandle(hTempFile) failed"));
return (8);
}
// Moves the temporary file to the new text file,allowing for differnt
// drive letters or volume names.
fSuccess = MoveFileEx(szTempFileName,TEXT("AllCaps.txt"),MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED);
if (!fSuccess)
{
PrintError(TEXT("MoveFileEx failed"));
return (9);
}
else
_tprintf(TEXT("All-caps version of %s written to AllCaps.txt\n"),argv[1]);
return (0);
}
// ErrorMessage support function.
// Retrieves the system error message for the GetLastError() code.
// Note: caller must use LocalFree() on the returned LPCTSTR buffer.
LPCTSTR ErrorMessage(DWORD error)
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,NULL,error,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,NULL);
return((LPCTSTR)lpMsgBuf);
}
// PrintError support function.
// Simple wrapper function for error output.
void PrintError(LPCTSTR errDesc)
{
LPCTSTR errMsg = ErrorMessage(GetLastError());
_tprintf(TEXT("\n** ERROR ** %s: %s\n"),errDesc,errMsg);
LocalFree((LPVOID)errMsg);
}
我希望这段代码对您有用。
谢谢。
,对于它的价值,我意识到我可以像检查其他文件一样检查tmp文件。打开并阅读。以下是适用于from here的代码:
void readTmpFile( FILE* f ){
u_char * buffer = 0;
long length;
//FILE * f = fopen (filename,"rb");
if (f) {
fseek (f,SEEK_END);
length = ftell (f);
fseek (f,SEEK_SET);
buffer = malloc (length);
int dontcare = 0;
if (buffer){
dontcare = fread (buffer,1,length,f);
}
fclose (f);
}
printPCAP_RAW( buffer,1 );
free( buffer );
}