5-16打卡-输入输出流

#include <iostream>
#include <fstream> // 包含文件操作所需的头文件
#include <iomanip> // 包含格式输出所需的头文件
using namespace std;

int main()
{
    char filename[20]; // 定义一个字符数组存储文件名
    cout << "请输入OFF文件的名称:" << endl;
    cin >> filename; // 输入文件名
    ifstream myfile(filename); // 创建一个名为myfile的文件对象,并打开名为filename的文件
    if (!myfile) // 检查文件是否打开成功
    {
        cout << "无法打开或找到文件" << endl;
        return 1;
    }
    int n; // 定义一个整数变量存储点的个数
    myfile >> n; // 从文件中读取点的个数
    cout << "该OFF文件包含" << n << "个点,它们的坐标为:" << endl;
    double x; // 定义一个浮点数变量存储每个坐标
    int count = 0; // 定义一个整数变量记录当前读取到第几个坐标
    while (myfile >> x) // 循环从文件中读取每个坐标
    {
        if (count == 0) cout << endl; // 如果是第一个坐标,则输出换行符
        cout << setw(10) << x; // 输出每个坐标,并设置位宽为10
        count++; // 增加计数器
        if (count == 3) count = 0; // 如果是第三个坐标,则重置计数器为0
    }
    myfile.close(); // 关闭文件
    return 0;
}
 
实验任务四
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

typedef struct Teacher {
	int id;
	string name;
	char gender;
}Teacher;

int main()
{
	int n;
	cout << "你准备输入几个教师的信息:";
	cin >> n;
	fstream myfile("teacher.txt",ios::binary | ios::out);
	if (!myfile)
	{
		cout << "文件创建或者打开失败!!" << endl;
		return 1;
	}
	for (int i = 0; i < n; i++) // 循环n次
	{
		Teacher a; // 定义一个Teacher类型的变量a
		cout << "请输入第" << i + 1 << "个教师的工号、姓名和性别(用空格隔开):" << endl;
		cin >> a.id >> a.name >> a.gender; // 输入每个教师信息
		cin.get();
		myfile.write((char*)&a, sizeof(a));
		
	}
	myfile.close();
	return 0;
}
 
 
实验任务4
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

typedef struct Teacher {
	int id;
	string name;
	char gender;
}Teacher;
//
//int main()
//{
//	int n;
//	cout << "你准备输入几个教师的信息:";
//	cin >> n;
//	fstream myfile("teacher.txt",ios::binary | ios::out);
//	if (!myfile)
//	{
//		cout << "文件创建或者打开失败!!" << endl;
//		return 1;
//	}
//	for (int i = 0; i < n; i++) // 循环n次
//	{
//		Teacher a; // 定义一个Teacher类型的变量a
//		cout << "请输入第" << i + 1 << "个教师的工号、姓名和性别(用空格隔开):" << endl;
//		cin >> a.id >> a.name >> a.gender; // 输入每个教师信息
//		cin.get();
//		myfile.write((char*)&a, sizeof(a));
//		
//	}
//	myfile.close();
//	return 0;
//}

int main()
{
	ifstream op;
	op.open("teacher.txt", ios::in | ios::binary);
	if (!op)
	{
		cout << "文件打开失败" << endl;
		return 0;
	}

	Teacher p;
	while (!op.eof() && op.read((char*)&p, sizeof(p)))
	{
		cout << "工号:" << p.id << "姓名:" << p.name << "性别:" << p.gender << endl;
	}

	op.close();
	return 0;
}
 
实验任务五
int main() {
	fstream myfile("M99.txt", ios::out);
	if (!myfile)
	{
		cout << "文件创建或打开失败!" << endl;
		return 0;
	}
	for (int i = 1; i <= 9; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			myfile << i << "*" << j << "=" << i * j << " ";
			cout << i << "*" << j << "=" << i * j << " ";
		}
		myfile << endl;
		cout << endl;
	}
	return 0;
}
 
 
实验任务七
#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
using namespace std;

int main()
{
	fstream myfile("cppAdvantage.txt", ios::in);
	if (!myfile)
	{
		cout << "文件打开失败或文件不存在" << endl;
		return 0;
	}
	string s;
	int linecount = 0, wordcount = 0;
	while (getline(myfile,s))
	{
		linecount++;
		wordcount += count(s.begin(), s.end(), ' ');
	}
	myfile.close();
	cout << "line:" << linecount << "  word:" << wordcount << endl;
	return 0;
}

posted @ 2023-05-16 20:24  aallofitisst  阅读(13)  评论(0)    收藏  举报