cmd控制台遍歷目錄文件名批量寫入TXT


其中.lib為文件名后綴,0.txt為寫入文件

dir /b *.lib >0.txt

附0.txt分離代碼,可將奇數行和偶數行分離,僅供參考

#include <iostream>
#include <string>
#include <fstream> 
#include <iostream>
using namespace std;

int main()
{
	ifstream txtfile;//打開讀取的文件
	ofstream txt01;//保存的文件
	ofstream txt02;//保存的文件
	string temp;
	int index = 0;//用於判斷奇偶

	txtfile.open("0.txt", ios::in);

	while (!txtfile.eof())            // 若未到文件結束一直循環
	{

		getline(txtfile, temp);//一行一行讀取
		if (index % 2 == 0)//判斷除以2的余數,即為奇偶的判斷
		{
			txt01.open("1.txt", ios::app);
			txt01 << temp;
			txt01 << endl;
			txt01.close();
		}
		else
		{
			txt02.open("2.txt", ios::app);
			txt02 << temp;
			txt02 << endl;
			txt02.close();
		}
		index++;
	}
	txtfile.close();   //關閉文件
	txtfile.close();
	txt01.close();
	txt02.close();

	return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM