zoukankan      html  css  js  c++  java
  • C++对txt文本进行读写操作

    输入输出,是每一个程序猿的基本功,尤其是对文本的输入和输出。

    近期,自己在这方面做了一些总结,不是非常全面,希望在以后学习和工作的过程中慢慢补充,积累点点滴滴。

    P.S. 今天天气不错。雾霾散了。天空晴朗。惠风和畅,心情不错。

    一、写操作

    // set.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include "iostream"
    #include "string"
    #include "fstream"
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	const string data = (string)"hello"+"+"+"world";
    	ofstream out(data,ofstream::app);
    	out << 3 <<endl;
    	out.close();
    	return 0;
    }



    二、读操作

    // set.cpp : 定义控制台应用程序的入口点。

    // #include "stdafx.h" #include "iostream" #include "string" #include "fstream" #include "vector" #include "sstream" using namespace std; struct personalinfo{ string name; vector<string> phones; }; int _tmain(int argc, _TCHAR* argv[]) { const string data = "C:\Users\helei\Documents\Visual Studio 2010\Projects\set\Release\hello+world"; ifstream out(data,ofstream::app); string line,word; vector<personalinfo> people; while (getline(out,line)){ personalinfo info; istringstream record(line); record >> info.name; while (record >> word) info.phones.push_back(word); people.push_back(info); } cout << people[0].name <<" "<< people[0].phones[0] <<" "<< people[0].phones[1] << endl; cout << people[1].name <<" "<< people[1].phones[0] << endl; cout << people[2].name <<" "<< people[2].phones[0] <<" "<< people[2].phones[1] <<" "<< people[2].phones[2] << endl; out.close(); return 0; }



  • 相关阅读:
    Win10 WSL Ubuntu18.04 编译安装MySQL5.7
    PHP7 深入理解
    php session 测试
    nginx 匹配路由分发php和golang
    composer 库无法提交git
    Win10 1803安装Ubuntu1804子系统
    dhtmlxTreeGrid使用
    win7 64位系统下安装PL/SQL连接Oracle服务器的解决方法
    转载--eclipse快捷键
    JUnit4学习笔记2-Eclipse中使用JUint4进行单元测试
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6841081.html
Copyright © 2011-2022 走看看