0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++でテキストファイル操作だ

Last updated at Posted at 2019-09-09

テキストファイルを読み込むのはこいつらを使うぞ

#include <fstream>
using namespace std;

ifstream finL("Loutput190831.dat"); //入力
ofstream foutt("testout.dat");    //出力

実際使おう


#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <iostream>

using namespace std;


int main()
{

   string str1, str2, str3, str4, str5;
   std::string data;
   int nbin = 256;

   ifstream finL("Loutput.dat");
   ofstream foutt("testout.dat");
   //////
   //Loutput は 01 0000 0001 0002 0003 0004 0005 0006 0007 
   //////
   string buf;
   while (!finL.eof()) //ファイルを最後まで読む
   {
      string uni, bin0, bin1, bin2, bin3, bin4, bin5, bin6, bin7;
  
      //cout << buf[1] << endl; buf[1] は 1
      finL >> uni >> bin0 >> bin1 >> bin2 >> bin3 >> bin4 >> bin5 >> bin6 >> bin7;
      //       01    0000    0001    0002    0003    0004    0005    0006    0007 
      foutt << bin2 <<bin5
      //       0002   0005   testout.datのなかみ

    finL.close
   }

小話

eofはファイルの一番最後に存在する見えない文字
eofを読んだ時点でfinL.eof()はtrueを返す
なので最後まで読んで欲しい時は

while (!eof.finL){}

となる。

他にも入出力ストリームはstringstreamなどがある。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?