前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ 读写TXT文件

C++ 读写TXT文件

作者头像
全栈程序员站长
发布2022-08-22 13:04:00
7940
发布2022-08-22 13:04:00
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

一、文件的输入输出

二、从txt文件中读取二维数组(int 以及string)

三、从txt文件读取的数据存到struct中


参考博客:https://blog.csdn.net/u013749068/article/details/78761553

http://www.cnblogs.com/helinsen/archive/2012/07/26/2609251.html

一、文件的输入输出

1.fstream提供了三个类,用来实现c++对文件的操作。(文件的创建、读、写)。

ifstream :从已有的文件读入

ofstream : 向文件写内容

fstream : 打开文件供读写

2.文件打开模式:

ios::in 只读

ios::out 只写

ios::app 从文件末尾开始写,防止丢失文件中原来就有的内容

ios::binary 二进制模式

ios::nocreate 打开一个文件时,如果文件不存在,不创建文件

ios::noreplace 打开一个文件时,如果文件不存在,创建该文件

ios::trunc 打开一个文件,然后清空内容

ios::ate 打开一个文件时,将位置移动到文件尾

3.文件指针位置的用法

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个字节的位置

注意:移动的单位是字节,而不是行

二、从txt文件中读取二维数组(int以及string)

首先选取“世界女排大奖赛历届前三“”数据作为data.txt

其中第1、2列是读入成int数据,第2~6列数据读入成string;

C++ 读写TXT文件
C++ 读写TXT文件

由于本人在读入该数据遇到了些问题,故将data.txt改成如下形式:

文件由两部分组成,1~25以及26~50行。

C++ 读写TXT文件
C++ 读写TXT文件

代码如下:

代码语言:javascript
复制
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    //创建二维数组分别存储int及string数据
	string str[25][4];
	int I[25][2] = {0};
	ifstream myfile("data1.txt");
	ofstream outfile("out.txt", ios::trunc);

	if (!myfile.is_open())
	{
		cout << "can not open this file" << endl;
		return 0;
	}
    //从data1文件中读入int数据
	for (int i = 0; i < 25; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			myfile >> I[i][j];
		}
	}
    //读入string数据
	for (int i = 25; i < 50; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			myfile >> str[i-25][j];
		}
	}
    //将数据输出至out.txt文件中
	for (int i = 0; i < 25; i++)
	{
		outfile  << I[i][0] << "    " << I[i][1] << "    " 
            << str[i][0] << "    " << str[i][1] << "    "
			<< str[i][2] << "    " << str[i][3]<< endl;
	};

	myfile.close();
	outfile.close();
	return 0;
}

最终在当前目录下生成一个out.txt文件。打开如下:

C++ 读写TXT文件
C++ 读写TXT文件
C++ 读写TXT文件
C++ 读写TXT文件

三、从txt文件读取的数据存到struct中

文件读取的过程一样,多了以下几步:定义struct Game,定义一个元素 个数为25的结构数组,写一个for循环初始化结构数组。

代码如下:

代码语言:javascript
复制
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//定义一个结构体
struct Game
{
	int num;
	int year;
	string location;
	string first;
	string second;
	string third;
};

int main()
{
	string str[25][4];
	int I[25][2] = {0};
	ifstream myfile("data1.txt");
	ofstream outfile("out.txt", ios::trunc);
	//定义一个结构数组
    Game game[25];

    //打开并读取data1.txt
	if (!myfile.is_open())
	{
		cout << "can not open this file" << endl;
		return 0;
	}
	for (int i = 0; i < 25; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			myfile >> I[i][j];
		}
	}
	for (int i = 25; i < 50; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			myfile >> str[i-25][j];
		}
	}

    //初始化结构数组元素
	for (int i = 0; i < 25; i++)
	{
		game[i].num = I[i][0];
		game[i].year = I[i][1];
		game[i].location = str[i][0];
		game[i].first = str[i][1];
		game[i].second = str[i][2];
		game[i].third = str[i][3];
      
        //写入outfile对象并控制台输出结果
		outfile  << game[i].num << "    " << game[i].year << "    " << game[i].location << "    " << game[i].first << "    "
			<< game[i].second << "    " << game[i].third << endl;
		cout << game[i].num << "    " << game[i].year << "    " << game[i].location << "    " << game[i].first << "    "
				<<game[i].second<<"    "<<game[i].third<<endl;
	};

	myfile.close();
	outfile.close();
	return 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/137159.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月5,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、文件的输入输出
  • 二、从txt文件中读取二维数组(int 以及string)
  • 三、从txt文件读取的数据存到struct中
  • 一、文件的输入输出
  • 二、从txt文件中读取二维数组(int以及string)
  • 三、从txt文件读取的数据存到struct中
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档