C++ 文件操作

<pre name="code" class="cpp">ofstream类支持磁盘文件输出

//文件的输出
#include <fstream>
using namespace std;
void main() //程序从这里开始运行
{
	ofstream SaveFile("cpp-home.txt");
	SaveFile <<"Hello World, from www.cpp-home.com and Loobian!";
	SaveFile<<endl;
    SaveFile<<"the end"<<endl;
	SaveFile.close();
}


//文件的读入
#include<iostream>
#include <fstream>
using namespace std;
void main() //程序从这里开始
{
	ifstream OpenFile("cpp-home.txt");
	char ch;
	while(!OpenFile.eof())
	{
		OpenFile.get(ch);
		cout<< ch;
	}
	OpenFile.close();
}



//C语言写入
#include <stdio.h>
int main()
{
	FILE * fp =fp=fopen("cpp-home.txt","w");;
	fprintf(fp,"%s\n","hello!");
	fclose(fp);
	return 0;
}

//c 文件的读取
/*
//cpp-home.txt
1 2 3 4
5 6 7 8
*/

#include "stdio.h"
int main()
{
	FILE *fp;
	int a[2][4]={0};
	int i,j;

	if((fp=fopen("cpp-home.txt","rt"))==NULL)
	{
		printf("cannot open file\n");
		return 1;
	}

	for(i=0;i<2;i++)
	{
		for(j=0;j<4;j++)
			fscanf(fp,"%d",&a[i][j]);
		    fscanf(fp,"\n");
	}

	for(i=0;i<2;i++)
	{
		for(j=0;j<4;j++)
			printf("%d ",a[i][j]);
		printf("\n");
	}
	fclose(fp);
	return 0;
}

使用width控制输出宽度
cout.width(10);
cout.fill('*');
cout<<values[i]<<'\n';

使用setw指定宽度
cout<<setw(6)<<names[i]<<setw(10)<<values[i]<<endl;





                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值