*-----------------------------------------------------------------------
* name: simple cat(cat.c)
* Author : maskedman
* For example:cat [options] [files]
*-----------------------------------------------------------------------
*/
#include <windows.h>
#include <stdarg.h>
#include <tchar.h>
#include <process.h>
#include <stdio.h>
#define _memtchr memchr
#define BUF_SIZE 0x200
#define LIBSPEC _declspec (dllexport)
LIBSPEC VOID PrintStrings (HANDLE, ...);
LIBSPEC VOID PrintMsg (HANDLE, LPCTSTR);
LIBSPEC VOID ReportError (LPCTSTR, DWORD, BOOL);
static VOID CatFile (HANDLE,HANDLE);
DWORD Options (int argc, LPCTSTR argv [], LPCTSTR Optstr, ...);
VOID ReportError (LPCTSTR UserMessage, DWORD ExitCode,BOOL PrintErrorMsg);
int _tmain (int argc,LPTSTR argv [])
{
HANDLE hInFile,hStdIn=GetStdHandle (STD_INPUT_HANDLE);
HANDLE hStdOut = GetStdHandle (STD_OUTPUT_HANDLE);
BOOL DashS;
int iArg, iFirstFile;
iFirstFile = Options ( argc, argv, _T("s"), &DashS, NULL);
if (iFirstFile==argc){
CatFile( hStdIn, hStdOut);
return 0;
}
for (iArg = iFirstFile; iArg<argc; iArg++) {
hInFile = CreateFile (argv [iArg],GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (hInFile==INVALID_HANDLE_VALUE && !DashS)
ReportError (_T ("Cat file open error"), 1, TRUE);
CatFile (hInFile, hStdOut);
CloseHandle (hInFile);
}
return 0;
}
static VOID CatFile (HANDLE hInFile, HANDLE hOutFile)
{
DWORD nIn, nOut;
BYTE Buffer [BUF_SIZE];
while (ReadFile (hInFile, Buffer, BUF_SIZE, &nIn, NULL)
&& (nIn != 0)
&& WriteFile (hOutFile, Buffer, nIn, &nOut, NULL));
}
DWORD Options (int argc, LPCTSTR argv [], LPCTSTR OptStr, ...)
{
va_list pFlagList;
LPBOOL pFlag;
int iFlag = 0, iArg;
va_start (pFlagList, OptStr);
while ((pFlag = va_arg (pFlagList, LPBOOL)) != NULL
&& iFlag < (int) _tcslen (OptStr)) {
*pFlag = FALSE;
for (iArg = 1; !(*pFlag) && iArg < argc && argv [iArg] [0] == '-';iArg++)
*pFlag = _memtchr (argv [iArg],OptStr [iFlag],
_tcslen (argv [iArg])) != NULL;
iFlag++;
}
va_end (pFlagList);
for (iArg = 1; iArg < argc && argv [iArg] [0] == '-'; iArg++);
return iArg;
}
VOID ReportError (LPCTSTR UserMessage, DWORD ExitCode,BOOL PrintErrorMsg)
{
DWORD eMsgLen,LastErr = GetLastError ();
LPTSTR lpvSysMsg;
HANDLE hStdErr = GetStdHandle (STD_ERROR_HANDLE);
PrintMsg (hStdErr, UserMessage);
if (PrintErrorMsg) {
eMsgLen = FormatMessage
(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM, NULL, LastErr,
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpvSysMsg, 0, NULL);
PrintStrings (hStdErr, _T ("\n"), lpvSysMsg,
_T ("\n"), NULL);
HeapFree (GetProcessHeap (), 0, lpvSysMsg);
}
if (ExitCode >0 )
ExitProcess (ExitCode);
else
return;
}
出现
cat.obj : error LNK2001: unresolved external symbol _PrintStrings
cat.obj : error LNK2001: unresolved external symbol _PrintMsg
希望老大们帮帮我
4 个解决方案
#1
PrintStrings
PrintMsg
没有这两个函数体
PrintMsg
没有这两个函数体
#2
有函数的定义,但是没有lib
#3
PrintStrings
PrintMsg
没有实现的函数体?
仔细找找,或者自己根据程序意思,写个函数替代.
鉴于只不过是错误信息输出 没什么用,
不如在main()中把erroo什么之类的全删除吧..
错误输出就自己用printf()输出就好了.
PrintMsg
没有实现的函数体?
仔细找找,或者自己根据程序意思,写个函数替代.
鉴于只不过是错误信息输出 没什么用,
不如在main()中把erroo什么之类的全删除吧..
错误输出就自己用printf()输出就好了.
#4
谢谢各位,发分结贴!
#1
PrintStrings
PrintMsg
没有这两个函数体
PrintMsg
没有这两个函数体
#2
有函数的定义,但是没有lib
#3
PrintStrings
PrintMsg
没有实现的函数体?
仔细找找,或者自己根据程序意思,写个函数替代.
鉴于只不过是错误信息输出 没什么用,
不如在main()中把erroo什么之类的全删除吧..
错误输出就自己用printf()输出就好了.
PrintMsg
没有实现的函数体?
仔细找找,或者自己根据程序意思,写个函数替代.
鉴于只不过是错误信息输出 没什么用,
不如在main()中把erroo什么之类的全删除吧..
错误输出就自己用printf()输出就好了.
#4
谢谢各位,发分结贴!