zoukankan      html  css  js  c++  java
  • c++读写文件

    一、文件的输入输出
    二、从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;

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

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

    代码如下:

    #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文件。打开如下:

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

    代码如下:

    #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;

    ————————————————
    版权声明:本文为CSDN博主「渐进的冰山」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_29406323/article/details/81261926

  • 相关阅读:
    重要网址(工具的使用)
    navicat的使用(测试库和正式库同步)以及用plsql改表字段属性
    linux
    linux一些基本命令
    LINUX的一些基本概念和操作
    LINUX简介
    Redis系统管理
    Redis之数据类型
    Redis的简介与安装
    论文阅读笔记(二十七)【AAAI2019】:A Bottom-Up Clustering Approach to Unsupervised Person Re-Identification
  • 原文地址:https://www.cnblogs.com/xzh1993/p/15686427.html
Copyright © 2011-2022 走看看