一次C++作業 C++的I/O流類庫2 [文本文件和二進制文件]

1.採用預定義的輸入輸出操縱符格式控制改造12.2.2中第一題,使其達到相同的輸出效果。(接1)

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

void showflags(long f)
{
	long i = 0x8000;
	for (; i; i = i >> 1)
	{
		if (i & f)
			cout << "1";
		else
			cout << "0";
	}
	cout << endl;
}
void main()
{
	cout << "----------------------------" << endl;
	cout << "* * * x_width=10,x_fill= ,x_precision=4 * * *" << endl;
	cout << setw(10) << setprecision(4) << 123 << " " << 123.45678 << " " << 234.567 << endl;
}

2.定義一複數類CComplex(類中含有實部real和虛部imag私有成員變量,都爲double類型),重載輸入運算符>>和輸出運算符<<,使得該類可以直接通過輸入輸出運算符進行輸入輸出操作。

#include <iostream>
using namespace std;

class CComplex  //定義複數類
{
public:
	CComplex(double r = 0.0, double i = 0.0)  //構造函數
	{
		real = r;
		imag = i;
	}
	friend istream& operator >> (istream& in, CComplex & z);  //友元重載輸入 
	friend ostream& operator << (ostream& out, const CComplex& z);  //友元重載輸出 
private:
	double real,imag;
};

istream& operator >> (istream& cin, CComplex& c)  //實現重載輸入 
{
	cout << "請分別輸入複數的實部和虛部:" << endl;
	cin >> c.real>>c.imag;
	return cin;
}

ostream& operator << (ostream& cout, const CComplex & c)  //實現重載輸出 
{
	if (c.imag > 0)  //虛部大於0時,輸出爲複數類,且有加號 
	{
		cout  <<"您輸入的複數是:"<< c.real;
		cout <<"+"<< c.imag << "i";
		return cout;
	}
	else if (c.imag < 0)  //虛部小於0時,輸出爲複數類,沒有加號
	{
		cout <<"您輸入的複數是:"<< c.real;
		cout << c.imag<< "i" ;
		return cout;
	}
	else if (c.imag == 0)  //虛部爲0時,直接輸出實部 
	{
		cout <<"您輸入的數是:"<< c.real;
		return cout;
	}
}

void C()   
{
	CComplex c1;
	std::cin >> c1;
	std::cout << c1;
}

int main()  //主函數
{
	C();
	return 0;
}

3.有一名爲test.txt的文件,裏面分兩行分別存儲了整數12345678和字符串abcdefg,設計兩個完整的程序,第一個程序以文本文件的方式存取該文件,第二個程序以二進制的方式存取該文件,並比較以這兩種方式存取該文件對文件本身有什麼影響。
文本文件:

#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
void input()
{
	int m = 12345678;
	char n[8]="abcdefg";
	ofstream file;
	file.open("test.txt");//文件默認在工程目錄文件夾下
	file << m << endl << n;
	file.close();
	system("PAUSE");
}
void output()
{
	char buffer[100];
	ifstream file("test.txt");
	while (!file.eof())//當到達文本末返回true
	{
		file.getline(buffer, 100);
		cout << buffer << endl;
	}
	system("PAUSE");
}
int main()
{
	input();
	output();
}


二進制文件:

#include <iostream>
#include <fstream>
using namespace std;
void input()
{
	char m[] = "12345678";
	char n[] = "abcdefg";
	ofstream file;
	file.open("test.txt", ios::binary); //> _path means path of files  
	if (file.is_open())
	{
		file.write((const char*)&m, 8);
		file.write("\n", 1);
		file.write((const char*)&n,8);  //> out put from _img to _path  
	}
	file.close();
}

void output()
{
	const char* filename = "test.txt";
	char* buffer;
	long size;
	ifstream file(filename, ios::in | ios::binary | ios::ate);//binary 二進制操作文件
	size = file.tellg();//用於輸入流,返回流中‘get’指針當前的位置
	file.seekg(0, ios::beg);
	buffer = new char[size];
	file.read(buffer, size);
	cout << buffer << endl;
	file.close();
	delete[] buffer;
}
int main()
{
	input();
	output();
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章