C++实现对文件的读写操作(详细)

上一次用到了从c++中输出文本文档,用到了头文件fstream,下面详细介绍一下头文件fstream。

fstream包含三个类,分别是:

ifstream—从已有的文件中读。

ofstream—从已有的文件中写。

fstream—打开文件供读写。

对于这三个类的公共函数可以参考c++ reference。先介绍一部分:

ios::beg   文件头
ios::end   文件尾
ios::cur   当前位置
例子:
file.seekg(0,ios::beg);   //让文件指针定位到文件开头
file.seekg(0,ios::end);   //让文件指针定位到文件末尾
file.seekg(10,ios::cur);   //让文件指针从当前位置向文件末方向移动10个字节
file.seekg(-10,ios::cur);   //让文件指针从当前位置向文件开始方向移动10个字节
file.seekg(10,ios::beg);   //让文件指针定位到离文件开头10个字节的位置

常用的判断错误的方法:

good()   如果文件打开成功
bad()   打开文件时发生错误
eof()    到达文件尾

读取文件:对于程序来说,从外部读取数据吗,因此要定义输入流,即定义输入流对象,这个对象中存放从文件中读入的输入流。

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;

int main()
{
ifstream infile;
string t1;
string t2;
vector<string> id;
vector<string> time;


infile.open("C:\\Users\\CiCi\\Desktop\\数学建模\\id.txt");
	if (!infile.is_open())
		cout << "open file failure" << endl;
	while (!infile.eof())
	{
		infile >> t1;
		id.push_back(t1);
	}
	infile.close();
	infile.open("C:\\Users\\CiCi\\Desktop\\数学建模\\time.txt");
	if (!infile.is_open())
		cout << "open file failure" << endl;
	while (!infile.eof())
	{
		infile >> t2;
		time.push_back(t2);
	}
	infile.close();

for (auto it = id.begin(); it != id.end(); it++)
	{
		id_index.push_back(it - id.begin());
	}
	for (auto it = time.begin(); it != time.end(); it++)
	{
		time_index.push_back(it - time.begin());
	}
	for (auto it = time.begin(); it != time.end(); it++)
	{
		string t = *it;
		if (t.substr(2, 1) != ":")
		{

			time_h.push_back(atoi(t.substr(0, 1).c_str()));
			time_m.push_back(atoi(t.substr(2, 2).c_str()));
		}
		else
		{
			time_h.push_back(atoi(t.substr(0, 2).c_str()));
			time_m.push_back(atoi(t.substr(3, 2).c_str()));
		}
	}
	for (auto it = time_h.begin(); it != time_h.end(); it++)
	{
		auto t3 = time_h.at(it - time_h.begin()) * 60 + time_m.at(it - time_h.begin());
		time_t.push_back(t3);
	}

return 0;

}

PS:这里介绍一个把string型的对象转换为int型的函数atoi。

int atoi(const char*str);

由于所需要的参数是c风格的字符串,所以还需要把string对象转换为c中的字符串样式。在c语言中没有string类型,故必须通过string类对象的成员函数c_str()来实现。

time_h.push_back(atoi(t.substr(0, 2).c_str()));

 

  • 46
    点赞
  • 265
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C中,可以使用几种不同的方法进行文件读写操作。一种常见的方法是使用文件指针来打开、读取和写入文件内容。另一种方法是使用Windows API来处理文件。 在使用文件指针进行文件读写操作时,可以使用`fopen`函数打开文件,并使用`fread`和`fwrite`函数来读取和写入文件内容。下面是一个示例代码: ```c #include<stdio.h> int main() { FILE* file; file = fopen("1.txt", "w"); //以写入模式打开文件 if (file == NULL) { return -1; //打开文件失败 } fwrite("hello world", sizeof(char), 12, file); //写入字符串 fputc('c', file); //写入单个字符 fclose(file); //关闭文件 return 0; } ``` 另一种方法是使用Windows API来处理文件。可以使用`CreateFileA`函数来创建或打开文件,并使用`ReadFile`和`WriteFile`函数来读取和写入文件内容。下面是一个示例代码: ```c #include<Windows.h> int main() { HANDLE hFile = CreateFileA("1.txt", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); //以读写模式打开文件 if (hFile == INVALID_HANDLE_VALUE) { return -1; //打开文件失败 } DWORD len; WriteFile(hFile, "test", sizeof("test"), &len, NULL); //写入字符串 char buf<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C/C++文件读写(最全方法,多种实现)](https://blog.csdn.net/weixin_50964512/article/details/123240393)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值