---
title: "c++文件按行拆分"
author: "andy136566"
date: "2011-08-18T13:58:36Z"
tags: []
categories: ["编程语言"]
slug: "c"
draft: false
lastmod: "2011-08-18T13:58:36Z"
id: 1152313
url: "https://andy136566.iteye.com/blog/1152313"
---

```
void splitFile()
{
	int filelinenum=FILELINE/MAX;
	ifstream ifs("./test.txt");
	string str;
	int num=1;
	int i=1;
	for(i=1;i<=MAX;i++)
	{
		strstream aa;
		aa<<(i-1);
		string strCount;
		aa>>strCount;
		string filepath = "./test"+strCount+".txt";
		ofstream ofs(filepath.c_str());
		int start = filelinenum*(i-1)+1;
		int end = filelinenum*i;
		while((num!=(end+1))&&getline(ifs,str))
		{
			if(num>=start && num <=end){
				ofs<<str<<endl;
				num++;
			}
			else{
				num++;
				break;
			}
		}
	}
}
```