C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

C++を使用してテキストファイルからデータを読み取る


これは、テキストファイルからデータを読み取るためのC++プログラムです。

入力

tpoint.txt is having initial content as
“Tutorials point.”

出力

Tutorials point.

アルゴリズム

Begin
   Create an object newfile against the class fstream.
   Call open() method to open a file “tpoint.txt” to perform write operation using object newfile.
   If file is open then
      Input a string “Tutorials point" in the tpoint.txt file.
      Close the file object newfile using close() method.
   Call open() method to open a file “tpoint.txt” to perform read operation using object newfile.
   If file is open then
      Declare a string “tp”.
      Read all data of file object newfile using getline() method and put it into the string tp.
      Print the data of string tp.
   Close the file object newfile using close() method.
End.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
   fstream newfile;
   newfile.open("tpoint.txt",ios::out);  // open a file to perform write operation using file object
   if(newfile.is_open())     //checking whether the file is open
   {
      newfile<<"Tutorials point \n"; //inserting text
      newfile.close(); //close the file object
   }
   newfile.open("tpoint.txt",ios::in); //open a file to perform read operation using file object
   if (newfile.is_open()){   //checking whether the file is open
      string tp;
      while(getline(newfile, tp)){  //read data from file object and put it into string.
         cout << tp << "\n";   //print the data of the string
      }
      newfile.close();   //close the file object.
   }
}

出力

Tutorials point

  1. matplotlibを使用して.txtファイルからデータをプロットします

    matplotlibを使用して.txtファイルからデータをプロットするには、次の手順を実行できます- 図のサイズを設定し、サブプロット間およびサブプロットの周囲のパディングを調整します。 bar_namesとbar_heightsの空のリストを初期化します。 サンプルの.txtファイルを読み取り「r」モードで開き、バーの名前と高さのリストに追加します。 棒グラフを作成します。 図を表示するには、 show()を使用します メソッド。 例 from matplotlib import pyplot as plt plt.rcParams["figure.figsize"

  2. テキスト ファイルから Excel にデータをインポートする方法 (3 つの方法)

    必要なデータがテキスト ファイルに含まれている場合があります。そのデータを Excel にインポートする必要があります。 さまざまな操作を実行するためのワークブック。この記事では、 データをインポート するためのシンプルで効果的な方法を紹介します。 テキスト ファイル から Excel に . 説明のために、テキスト ファイルに存在する次のデータをソースとして使用します。たとえば、データには Salesman が含まれています 、商品 、および セールス 会社の。この情報を Excel にインポートします 次のワークブックをダウンロードして、自分で練習してください。 テ