技能大赛,要求将6g的txt文件进行拆分,而且不能截断一行的数据。本身没有作出来,如下是别人的代码:linux
#include<iostream> #include <fstream> #include<string> #include<stdio.h> #include<stdlib.h> using namespace std; int main( ) { double num; double lengthnum; double linesize; string filepath; char strsys[100]; char buffer[512]; cout << "请输入拆分文件个数:" << endl; cin >> num; cout << "请输入拆分文件名称:" << endl; cin >> filepath; ifstream in(filepath.c_str(), ios::in); if(!in) { cout << "文件打开错误!" << endl; return -1; } in.getline (buffer,512); linesize = strlen(buffer); //cout << linesize << endl; in.seekg(0,ios::end); double size_t = in.tellg(); lengthnum = size_t/num/linesize; cout << "拆文件开始!" << endl; sprintf(strsys,"split -l %0.0lf %s outfile",lengthnum,filepath.c_str()); //cout << strsys << endl; system(strsys); cout << "拆文件结束!" << endl; in.close(); return 0; }
能够看到最终仍是用到了linux下的split命令。ios
还有大神直接用shell写了一个脚本:shell
#!/bin/sh if [ -z "$1" ] || [ -z "$2" ] ; then echo "Ussage:fg.sh 文件名 个数" else lines=$(awk '{print NR}' $1 | tail -n 1) mod=$[$lines % $2] if [ $mod>0 ]; then line=$[$lines/$2+1] else line=$[$lines/$2] fi split -$line $1 fi
在想是否有windows版。windows