會嗎?轉成這樣不是比較好看?
111.rar
(3.1 MB)

不要選我當版主



程序代码:
// http://bbs.bccn.net/viewthread.php?tid=44246
// 让分布在几百个 csv 文件中各行连接成单一行
#include "stdafx.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include<direct.h>
#define getcurrentdir _getcwd
#else
#include <unistd.h>
#define getcurrentdir getcwd
#endif
char* getInputfile(char* dir, char* buffer, int i)
{
char currDir[80];
if( !dir ){
getcurrentdir(currDir,sizeof(currDir));
sprintf( buffer, "%s\\datafile\\%04d.csv", currDir, i);
}else
sprintf( buffer, "%s\\datafile\\%04d.csv", dir, i);
return buffer;
}
int lineScan(FILE* f, char* buffer, int Size)
{
int lines = 0;
while(!feof(f)) { fgets(buffer, Size, f); lines++;}
rewind(f);
return lines;
}
int maxLine(int start, int end)
{
const int bufferCount = 1024 * 4;
FILE* f = NULL;
char fullname[260];
int counter = 0;
int result = 0;
char* line = (char*)malloc( bufferCount );
for(int i = start; i <= end; ++i){
memset( fullname, 0, sizeof(fullname)/sizeof(fullname[0]) );
getInputfile(NULL, fullname, i);
f = fopen(fullname, "r");
if( !ferror(f) ) counter = lineScan(f, line, bufferCount);
else counter = 0;
fclose(f);
result = counter > result ? counter:result;
}
free(line);
return result;
}
void preCompile(long* headRec, FILE* destFile, int start, int end)
{
const short empty = 0;
const short feed = 0x0a0d;
const int buffercount = 1024 * 100;
FILE* inputfile = NULL;
char fullname[260];
short two = 0;
size_t byteRead = 0;
size_t byteWrite = 0;
int fileLine = 0;
int maxline = maxLine(start, end);
char* buffer = (char*)malloc( buffercount );
for(int i = start; i <= end; ++i){
memset( fullname, 0, sizeof(fullname)/sizeof(fullname[0]) );
getInputfile(NULL, fullname, i);
memset( buffer, 0, buffercount );
inputfile = fopen( fullname, "rb");
if( !ferror(inputfile) ){
headRec[i] = ftell(destFile);
fileLine = lineScan(inputfile, buffer, buffercount);
byteRead = fread(buffer, sizeof(char), buffercount, inputfile);
while( byteRead && !ferror(inputfile) ){
byteWrite = fwrite(buffer, sizeof(char), byteRead, destFile);
byteRead = fread(buffer, sizeof(char), buffercount, inputfile);
}
fclose(inputfile);
fileLine = maxline - fileLine;
while( fileLine >= 0 ){
fwrite(&feed, sizeof(short), 1, destFile);
fileLine--;
}
fseek(destFile, -((int)sizeof(short)), SEEK_CUR);
fread(&two, sizeof(short), 1, destFile);
if(two == feed){
fseek(destFile, -((int)sizeof(short)), SEEK_CUR);
fwrite(&empty, sizeof(short), 1, destFile);
}else if( two != 0 ){
fseek(destFile, 0, SEEK_END);
fwrite(&empty, sizeof(short), 1, destFile);
}
}
}
free(buffer);
}
int lineCheck(char* buffer, int* len, int isLF)
{
const short empty = 0;
const short feed = 0x0a0d;
short* two = NULL;
char* one = buffer;
int result = 0;
while( (int)one < (int)(buffer + *len) ){
two = (short*)one;
result = *two;
if(*two == feed || *two == empty){
if(!isLF){
*two = feed;
*len = (int)one - (int)buffer + 2;
return result == empty ? 4:2;
}else{
*two = empty;
*len = (int)one - (int)buffer;
return result == empty ? 3:1;
}
}
one++;
}
return 0;
}
void fileAssemble(long* headRec, int fileNum, FILE* dest, FILE* src)
{
const int bufferCount = 1024;
int byteRead = 0;
size_t byteWrite = 0;
char* buffer = (char*)malloc( bufferCount );
int index = 0;
int len = 0;
int loop = 0;
int over = fileNum;
do
{
fseek(src, headRec[index], SEEK_SET);
loop++;
byteRead = fread(buffer, 1, bufferCount, src);
while( byteRead && !ferror(src) ){
int inner = byteRead;
len = lineCheck(buffer, &inner, fileNum - loop);
byteWrite = fwrite(buffer, sizeof(char), inner, dest);
if( len != 0 ){
if( len == 2 || len == 4){
headRec[index] = headRec[index] + inner;
if( len == 4 ) over--;
}
else{
headRec[index] = headRec[index] + inner + sizeof(short);
if( len == 3 ) over--;
}
index++;
break;
}
byteRead = fread(buffer, sizeof(char), bufferCount, src);
}
if( loop == fileNum ){
index = 0; loop = 0;
}
if(!over)break;
} while ( byteRead != 0 );
free(buffer);
}
void fileMerge(char* resultName, int start, int end)
{
FILE* result = fopen(resultName, "wb");
FILE* temp = tmpfile();
long* headRec = (long*)malloc( (end - start + 1) * sizeof(long) );
memset( headRec, 0, (end - start + 1) * sizeof(long) );
preCompile(headRec, temp, start, end);
fileAssemble(headRec, end - start + 1, result, temp);
fclose(temp);
fclose(result);
free(headRec);
}
int _tmain(int argc, _TCHAR* argv[])
{
// 需要把 0000.csv - 0824.csv 存放在当前目录下的 datafile 目录里
// 输出的 result.csv 文件在当前目录
fileMerge("result.csv", 0, 284);
return 0;
}