Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 설치
- winsock
- c#
- 예제
- MySQL
- PostgreSQL
- Delphi
- 초보
- dll
- 데이터베이스
- 셋업
- 인스톨
- 입문
- 파라미터
- SQL
- 소니
- 델파이
- SDK
- 시리얼 통신
- Visual Studio 2005
- xml
- vb
- 기초
- 문자열
- MFC
- VB.NET
- Firebird
- Visual Basic
- 파이어버드
- WIN32 SDK
Archives
- Today
- Total
프로그래밍 노트
파일 행수 취득 하는 소스 본문
■ 소스1
int n = 0;
HANDLE hFile = CreateFile(sPath,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFile == INVALID_HANDLE_VALUE)
// 에러
else
{
char buffer[65536];
DWORD dwRead;
int n = 0;
while(1)
{
if(!ReadFile(hFile, buffer, sizeof buffer, &dwRead, NULL) || !dwRead)
break;
n += std::count(buffer, buffer + dwRead, '\n');
}
CloseHandle(hFile);
}
■ 소스2
static int count_ln_by_buffer (const char *filename)
{
HANDLE hFile;
char *start, *p;
DWORD len, line_num = 0;
DWORD dwRead;
hFile = CreateFile (
filename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (!hFile)
return -1;
len = GetFileSize (hFile, NULL);
start = p = malloc (len);
ReadFile (hFile, start, len, &dwRead, NULL);
for (; (DWORD) (p - start) < len; p++)
{
if (*p == '\n')
line_num++;
}
CloseHandle (hFile);
return line_num;
}
static int count_ln_by_file_mapping (const char *filename)
{
HANDLE hFile, hMap;
const char *start, *p;
DWORD len, line_num = 0;
hFile = CreateFile (
filename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (!hFile)
return -1;
hMap = CreateFileMapping (
hFile, /* Current file handle */
NULL, /* Default security */
PAGE_READONLY, /* Read/write permission */
0, /* Max. object size. */
0, /* Size of hFile */
NULL); /* Name of mapping object */
if (!hMap)
return -1;
start = p = MapViewOfFile (
hMap, /* Handle to mapping object */
FILE_MAP_READ, /* Read/write permission */
0, /* Max. object size. */
0, /* Size of hFile */
0); /* Map entire file */
if (!p)
return -1;
len = GetFileSize (hFile, NULL);
for (; (DWORD) (p - start) < len; p++)
{
if (*p == '\n')
line_num++;
}
CloseHandle (hMap);
CloseHandle (hFile);
return line_num;
}
int main (int argc, char *argv[])
{
if (argc <= 1)
return -1;
return count_ln_by_buffer (argv[1]);
}
int n = 0;
HANDLE hFile = CreateFile(sPath,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFile == INVALID_HANDLE_VALUE)
// 에러
else
{
char buffer[65536];
DWORD dwRead;
int n = 0;
while(1)
{
if(!ReadFile(hFile, buffer, sizeof buffer, &dwRead, NULL) || !dwRead)
break;
n += std::count(buffer, buffer + dwRead, '\n');
}
CloseHandle(hFile);
}
■ 소스2
static int count_ln_by_buffer (const char *filename)
{
HANDLE hFile;
char *start, *p;
DWORD len, line_num = 0;
DWORD dwRead;
hFile = CreateFile (
filename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (!hFile)
return -1;
len = GetFileSize (hFile, NULL);
start = p = malloc (len);
ReadFile (hFile, start, len, &dwRead, NULL);
for (; (DWORD) (p - start) < len; p++)
{
if (*p == '\n')
line_num++;
}
CloseHandle (hFile);
return line_num;
}
static int count_ln_by_file_mapping (const char *filename)
{
HANDLE hFile, hMap;
const char *start, *p;
DWORD len, line_num = 0;
hFile = CreateFile (
filename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (!hFile)
return -1;
hMap = CreateFileMapping (
hFile, /* Current file handle */
NULL, /* Default security */
PAGE_READONLY, /* Read/write permission */
0, /* Max. object size. */
0, /* Size of hFile */
NULL); /* Name of mapping object */
if (!hMap)
return -1;
start = p = MapViewOfFile (
hMap, /* Handle to mapping object */
FILE_MAP_READ, /* Read/write permission */
0, /* Max. object size. */
0, /* Size of hFile */
0); /* Map entire file */
if (!p)
return -1;
len = GetFileSize (hFile, NULL);
for (; (DWORD) (p - start) < len; p++)
{
if (*p == '\n')
line_num++;
}
CloseHandle (hMap);
CloseHandle (hFile);
return line_num;
}
int main (int argc, char *argv[])
{
if (argc <= 1)
return -1;
return count_ln_by_buffer (argv[1]);
}
Comments