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

C++を使用してファイルを1行ずつ読み取る


これは、ファイルを1行ずつ読み取る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. C++でのラインリフレクション

    2D平面上にn個の点があるとすると、指定された点を対称的に反射するy軸に平行な線があるかどうかを確認する必要があります。つまり、指定された線上にすべての点を反映した後に線が存在するかどうかを確認する必要があります。元のポイントのセットは、反映されたポイントと同じです。 したがって、入力がpoints =[[1,1]、[-1,1]]のような場合 その場合、出力はtrueになります これを解決するには、次の手順に従います- 1つのセットを定義します。 n:=ポイントのサイズ minVal:=inf maxVal:=-inf 初期化i:=0の場合、i <

  2. C#を使用してテキストファイルを1行ずつ読み取る最速の方法は何ですか?

    テキストファイルを1行ずつ読み取る方法はいくつかあります。それらには、StreamReader.ReadLine、File.ReadLinesなどが含まれます。ローカルマシンに存在する、以下のような行を持つテキストファイルについて考えてみましょう。 StreamReader.ReadLineの使用- C#StreamReaderは、指定されたエンコーディングでストリームに文字を読み取るために使用されます。StreamReader.Readメソッドは、入力ストリームから次の文字または次の文字セットを読み取ります。 StreamReaderは、文字、ブロック、行、またはすべてのコンテン