回复 4# haijie1223
用 C 做了一下第一题,瞬间感觉 au3 各种高大上。。。#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int _FileCountLines(const char* strFileName);
int main(int argc, char* argv[])
{
int nCountLine = _FileCountLines("C:/test.txt");
printf("File count lines = %d\n", nCountLine);
FILE* pFile = NULL;
fopen_s(&pFile, "C:/test.txt", "r");
int nSubFileName = 1;
// save new file name "10.txt\n"
char sSubFileName[7] = { 0 };
// save read old file data "100000\n"
char sReadBuffer[7] = { 0 };
for (size_t i = 0; i < nCountLine; i += nCountLine / 10)
{
FILE* pSubFile = NULL;
// new file name append .txt
_itoa_s(nSubFileName, sSubFileName, 10);
strcat_s(sSubFileName, ".txt");
// open new file at current directory
fopen_s(&pSubFile, sSubFileName, "w+");
if (NULL == pSubFile) return -1;
// read old file and write data to new file step nCountLine / 10
for (size_t x = i; x < i + nCountLine / 10; x++)
{
fgets(sReadBuffer, sizeof(sReadBuffer), pFile);
fputs(sReadBuffer, pSubFile);
memset(sReadBuffer, 0, sizeof(sReadBuffer));
}
memset(sSubFileName, 0, sizeof(sSubFileName));
// file name ++
nSubFileName++;
// close new file
fclose(pSubFile);
}
fclose(pFile);
system("pause");
return 0;
}
int _FileCountLines(const char* strFileName)
{
int lines = 0;
int ch;
FILE* pFile = NULL;
fopen_s(&pFile, strFileName, "r");
if (NULL == pFile) return -1;
while ((ch = fgetc(pFile)) != EOF)
{
if (ch == '\n')
{
lines++;
}
}
fclose(pFile);
return lines;
}
|