文件操作:向文件追加内容

向文件写入:

#include <stdio.h>

int main(){
	int i=4;
	FILE *fp;
	fp=fopen("intfile.txt","w");
	if(!fp){
		printf("Failed to creat file!\n");
		exit(0);
	}

	for(i=1;i<101;i+=5)
		fprintf(fp,"%d %d %d %d %d\n",i,i+1,i+2,i+3,i+4);
	fclose(fp);
	return 0;
}

写入再追加:

#include<fstream>
#include<iomanip>
using namespace std;
int main(){
    int i,j;
    ofstream ofile;
    ofile.open("myfile.txt");
    ofile<<"==乘法口诀表=="<<endl; 
    for(j=1;j<10;j++)
        for(i=1;i<10;i++){
            ofile<<setw(2)<<i<<setw(2)<<"X"<<setw(2)<<j<<setw(2)<<"="<<setw(2)<<i*j<<"  ";
	    if(i==j){
		ofile<<endl;
		break;
	    }
        }
    ofile.close();

    ofstream ofileAgain;
    ofileAgain.open("myfile.txt",ios::app);
    ofileAgain<<"这是追加的内容。"<<endl;
    ofileAgain.close();

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_26697045/article/details/84874774
今日推荐