C++输入输出操作

例。向txt文件中写入25个素数
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	const int max=25;
	long primes[max]={2,3,5};
	int count=3;
	long trial=5;
	bool isprime=true;
	do
	{
		trial+=2;
		int i=0;
		do
		{
			isprime=trial%*(primes+i)>0;
		}while (++i<count&&isprime);
		if(isprime)
			*(primes+count++)=trial;
			
	}while (count<max);
	ofstream outFile("d:\\primes.txt");
	if(!outFile)
	{
		cerr<<"ereor: unable to open file!";
		return 1;
	}
	for(int i=0;i<max;i++)
	{
		if(i%5==0) outFile<<endl;
		outFile<<*(primes+i)<<" ";
	}
	return 0;

}

你可能感兴趣的:(C++)