C++文件读取与写入

经常用到文件读写,图省事,记下来以后直接COPY

先写入文件

/***以下代码为将va的内容写入到4.txt文件
*	格式为:
*		1,2
*		2,4
*		3,6
***/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct mpoint
{
	int x;
	int y;
	mpoint(int _i,int _j)
	{
		x=_i;
		y=_j;
	}

};
int main()
{
	/***初始化va***/
	vector<mpoint>va;
	for (int i=0;i<20;i++)
	{
		va.push_back(mpoint(i,2*i));
	}
	
	ofstream outfile;
	outfile.open("e:\\4.txt",ios::app);
	if(!outfile)
	{
		cout<<"Can not open file"<<endl;
	}
	/***写入数据***/
	for (int i=0;i<va.size();i++)
	{
		outfile<<va[i].x<<" "<<va[i].y<<'\n';
	}

	outfile.close();
	return 0;
}

读取文件
/***以下代码为将文件4.txt的内容读取到vb
*	文件中数据存储格式为:
*		1,2
*		2,4
*		3,6
***/


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

struct mpoint
{
	int x;
	int y;
	mpoint(int _i,int _j)
	{
		x=_i;
		y=_j;
	}
};

int main(int args, char **argv)
{
	vector<mpoint>vb;
	std::ifstream  fin("e:\\4.txt", std::ios::in);
	char  line[1024]={0};
	std::string  x = "";
	std::string  y = "";
	int ix;
	int iy;

	while(fin.getline(line, sizeof(line)))/***按行读取***/
	{
		std::stringstream  word(line);
		word >> x;
		word >> y;
		ix=atoi(x.c_str());/***string转换为int***/
		iy=atoi(y.c_str());
		vb.push_back(mpoint(ix,iy));
	}

	/***打印到控制台***/
	for (int i=0;i<vb.size();i++)
	{
		cout<<vb[i].x<<","<<vb[i].y<<endl;
	}
	
	fin.clear();
	fin.close();
	return 0;
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于c++文件读取写入,可以使用fstream库中的ifstream和ofstream类来实现。其中,ifstream用于读取文件内容,而ofstream则用于写入文件内容。具体使用方法如下: 1. 读取文件内容: ```c++ #include <fstream> #include <iostream> using namespace std; int main() { ifstream infile; // 定义输入文件流对象 infile.open("input.txt"); // 打开文件 if (!infile) // 判断文件是否打开成功 { cout << "文件打开失败!" << endl; return 0; } string line; while (getline(infile, line)) // 逐行读取文件内容 { cout << line << endl; } infile.close(); // 关闭文件 return 0; } ``` 2. 写入文件内容: ```c++ #include <fstream> #include <iostream> using namespace std; int main() { ofstream outfile; // 定义输出文件流对象 outfile.open("output.txt"); // 打开文件 if (!outfile) // 判断文件是否打开成功 { cout << "文件打开失败!" << endl; return 0; } outfile << "Hello, world!" << endl; // 向文件写入内容 outfile.close(); // 关闭文件 return 0; } ``` 关于链表的实现,可以定义一个结构体来表示链表的每个节点,然后使用指针来实现节点之间的连接。具体实现方法如下: ```c++ #include <iostream> using namespace std; struct ListNode { int val; // 节点值 ListNode* next; // 下一个节点指针 ListNode(int x) : val(x), next(NULL) {} // 构造函数 }; int main() { ListNode* head = new ListNode(1); // 创建链表头节点 ListNode* p = head; for (int i = 2; i <= 5; i++) // 构造链表 { ListNode* node = new ListNode(i); p->next = node; p = node; } p = head; while (p != NULL) // 遍历链表 { cout << p->val << " "; p = p->next; } cout << endl; return 0; } ``` 以上就是c++文件读取写入以及链表的实现方法,希望对你有所帮助。如果还有其他问题,请继续提问哦~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值