[fstream]学习

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

//Reading Data from a File  读|检测存在//内含指针
int main() {
	//写
	ofstream output;
	output.open("C:\\Users\\17687\\Desktop\\textt.txt");
	output << "John" << " " << "T" << " " << "Smith" << " " << 90 << endl;
	output << "Eric" << " " << "K" << " " << "Jones" << " " << 85 << endl;
	

	//设置宽度<iomanip>
	output << setw(6) << " ww" << setw(6) << " T" << setw(6) << " Smith" << setw(4) << " 90" << endl;
	output.close();
	cout << "Done" << endl;

    //文件夹的存在与否(而非文件内是否有信息
	ifstream input;
	input.open("C:\\Users\\17687\\Desktop\\textt.txt");
	if (input.fail()) {
		cout << "File does not exist" << endl;
		cout << "Exit program" << endl;
		return 0;
	}
	//Read data
	char firstName[80];
	char mi;
	char lastName[80];
	int score;
	while(!input.eof()){
		input >> firstName >> mi >> lastName >> score;
	    cout << firstName <<" " << mi<<" " << lastName<<" " << score<<endl;
	}
	
	input.close();
	cout << "Done" << endl;
	return 0;
}

 感觉挺神奇的hhhhh

(一些注意的点写在注释里了)

//copyFile
#include<iostream>
#include<fstream>
using namespace std;
int main() {
	const int FILENAME_SIZE = 40;
	cout << "Enter a source file name:";
	char inputFilename[FILENAME_SIZE];
	cin >> inputFilename;
	cout << "Enter a target file name: ";
	char outputFilename[FILENAME_SIZE];
	cin >> outputFilename;
	ifstream input;
	ofstream output;
	input.open(inputFilename);
	output.open(outputFilename);
	if (input.fail()) {
		cout << inputFilename << " does not exist" << endl;
		cout << "Exit program" << endl;
		return 0;
	}
	while (!input.eof())
		output.put(input.get());
	input.close();
	output.close();
	cout << "\nCopy Done" << endl;
	return 0;
}

没有创建texttt.txt(target)会自动创建

还有个appendfile(记得学

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值