Object and File Stream.Text file(Объекты и файловые потоки.Текстовые файлы)

Object and File Stream.Text file

Input/output stream of text file(Потоковый ввод/вывод дисковых файлов)

C++ Language တြင္ input/output operation မ်ားသည္ stream မွတစ္ဆင့္ လုပ္ေဆာင္ၾကသည္ ျဖစ္သည္။
Stream ဆိုသည္မွာ Logical သေဘာတရားအရသာ တည္ရွိသည္ျဖစ္ျပီး information မ်ားအား လက္ခံရယူရန္ႏွင့္ ျပန္လည္ထုတ္ေပးရန္အတြက္ ျပဳလုပ္ေပးသည္ျဖစ္သည္။
Keyboard ႏွင့္ Screen display မ်ားသည္ basic input/ouput Stream မ်ားျဖစ္ၾကသည္။ ၎တို႕၏ လုပ္ေဆာင္ခ်က္မ်ားသည္ text ဖိုင္မ်ားကဲ့သို႕ပင္ျဖစ္ၾကသည္။
Disk ဖိုင္မ်ားႏွင့္ အလုပ္လုပ္ရန္အတြက္ မျဖစ္မေန <fstream> ဟူေသာ header file ကို declare ျပဳလုပ္ရမည္ျဖစ္သည္။<fstream> ဟူေသာ file တြင္ ေအာက္ေဖာ္ျပပါ class မ်ားပါ၀င္သည္။
ifstream        - for input
ofstream       - for output
fstream         - read and write data in the same file

Disk file မ်ားႏွင့္ အလုပ္လုပ္နိုင္ရန္အတြက္ ၎အား ပထမဦးစြာ open ျပဳလုပ္ရန္လိုသည္။ထိုသို႕ open ျပဳလုပ္ရာတြင္ ios class တြင္ရွိေသာ open_mode အား define ျပဳလုပ္ေပးေသာ access_mode မ်ားကိုအသံုးျပဳရမည္ျဖစ္သည္။

Access_mode
Standard
Action
ate(attend)
Yes
When opening a file sets the file pointer to the end of file
binary (bin)
Yes
Open the file in binary representation
in
Yes
Open the file for reading (input)
out
Yes
Open the file for writing (output)


Text file(
Текстовые файлы)

Creating and write(Создание и запись)

Text file ေတြကိုတည္ေဆာက္ဖို႕အတြက္ ofstream class ရဲ႕ object အေနနဲ႕ တည္ေဆာက္ရမည္ျဖစ္ျပီး class ရဲ႕ constructor ရဲ႕ ပထမ parameter ေနရာမွာ ဖိုင္နာမည္ျဖစ္ျပီး ဒုတိယ parameter ေနရာမွာ access_mode ကိုေရးသားရမည္ျဖစ္သည္။
ofstream out_file(“out.txt”,ios::out);

ဖိုင္ဖြင့္ရန္အတြက္ mode အား const အေနျဖင့္ ေအာက္ေဖာ္ျပပါ ဥပမာအတိုင္း declare ျပဳလုပ္နိုင္သည္ ျဖစ္သည္။
const ios::open_mode=ios::out | ios::app;
အထက္ေဖာ္ျပပါအတိုင္း file တစ္ခုကို ဖြင့္ရန္ျပဳလုပ္ျပီးသည့္ေနာက္ ထိုဖိုင္သည္ write ရန္အတြက္ အမွန္တကယ္ပင္ open ျဖစ္မျဖစ္အား ေသခ်ာေစရန္အတြက္ ေအာက္ေဖာ္ျပပါအတိုင္း စစ္ေဆး နိုင္သည္ျဖစ္သည္။
if( !out_file) {
cerr<<”Error:unable to write to Out.txt”<<endl;
exit(1);
}
အထက္ေဖာ္ျပပါ စစ္ေဆးမွဳ႕သည္ write လုပ္ရန္သာမက read လုပ္ရန္အတြက္ တြင္လည္း အတူတူပင္ ျဖစ္သည္။
ifstream in_file(“Input.txt”,ios::in);
if(!in_file){ cerr<<”Error: unable to open Tnput.txt”<<endl;
exit(1);
}

Text file မ်ားႏွင့္ အလုပ္လုပ္ရာတြင္ မၾကာခဏအေနျဖင့္ ေအာက္ေဖာ္ျပပါ လုပ္ေဆာင္ခ်က္ေလးမ်ိဳးအား ေတြ႕ရွိရမည္ျဖစ္သည္။
·         посимвольное чтение,စာလံုးတစ္လံုးခ်င္းစီ အားျဖင့္ read လုပ္ျခင္း
·         посимвольная запись,စာလံုးတစ္လံုးခ်င္းစီ အားျဖင့္ write လုပ္ျခင္း
·         построчное чтение,စာေၾကာင္းအလိုက္ read လုပ္ျခင္း
·         построчная запись,စာေၾကာင္းအလိုက္ write လုပ္ျခင္း


Посимвольное чтение (character by character)
Text file အား စာလံုးတစ္လံုးခ်င္းစီအားျဖင့္ read လုပ္ရန္အတြက္ istream class တြင္ရွိေသာ get() function အားအသံုးျပဳရမည္ျဖစ္သည္။

Example. Character by character read the file and display it on screen.

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{setlocale(LC_ALL, "Russian");       // подключение  русификатора
 char sym;
 ifstream in_file ("d:\\Input.txt", ios::in);
 if (!in_file) { cerr << "Error input file" << endl;
                     exit(1);
                   }
 while (in_file) { in_file.get(sym);
                       cout << sym;
                     }
 cout << endl;
 _getch();  
 return 0;
}

Посимвольная запись(character by character writing file) 
Text ဖိုင္ အတြင္းရွိ data မ်ားအား character by character နည္းျဖင့္ write ျပဳလုပ္ရန္အတြက္ ostream method တြင္ရွိေသာ put() function အားျဖင့္ ျပဳလုပ္နိုင္သည္ျဖစ္သည္။

Example. character by character write data into text file.

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{setlocale(LC_ALL, "Russian");       // подключение  русификатора
 string quote = "Зорко одно лишь сердце. Самого главного глазами не увидишь. А. де
ент-Экзюпери";
/* в ранних версиях объявление строки quote выглядит так:
char *quote = "Зорко одно лишь сердце. Самого главного гла-зами не увидишь. А. де
ент-Экзюпери"; */
 ofstream out_file ("Out_file.txt", ios::out);
 if (!out_file) { cerr << "Error output file" << endl;
                      exit(1);
                    }
 for (unsigned int i = 0; i < quote.size(); i++) 
out_file.put(quote[i]);
/* в ранних версиях длина строки quote определяется по-другому:
 for (int i = 0; i < strlen(quote) + 1; i++)
                       out_file.put(quote[i]); */
 cout << "Конец записи" << endl;
 _getch();  
 return 0;
}

Построчное чтение (line by Line reading the file) 
ပံုမွန္အားျဖင့္ line by line နည္းျဖင့္ text ဖိုင္တစ္ခုအား read and write ျပဳလုပ္ျခင္းသည္ character by character ျပဳလုပ္ျခင္းထက္ပိုမို၍ လွ်င္ျမန္သည္ျဖစ္သည္။ line by line ျဖင့္ text အတြင္းရွိ data မ်ားအား read ျပဳလုပ္ရန္အတြက္ ifstream class ေအာက္တြင္ရွိေသာ getline() function အားအသံုးျပဳရမည္ျဖစ္သည္။
၎ function သည္ line အား read ျပဳလုပ္ရာတြင္ ‘\n’ ဟူေသာ new line တစ္ခုအားမေတြ႕မခ်င္း ၎ data မ်ားအား buffer ေပၚတြင္ေနရာေပးျခင္းျပဳလုပ္မည္ျဖစ္သည္။
Getline function အားအသံုးျပဳရာတြင္ Function argument ေနရာတြင္ buffer name သည္ ပထမေနရာတြင္တည္ရွိမည္ျဖစ္ျပီး ဒုတိယ argument ေနရာတြင္ buffer ၏ အၾကီးဆံုး size ကို ေဖာ္ျပရမည္ျဖစ္သည္။

Example.Line by Line read text file.

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{setlocale(LC_ALL, "Russian");// подключение  русификатора
 const int LEN = 80;
 char BUF[LEN];
 ifstream in_file("d:\\Input.txt", ios::in);
 if (!in_file) { cerr << "Error input file"
                      << endl;
                      exit(1);
               }
 while (in_file)
        { in_file.getline(BUF,LEN);
          cout << BUF << endl;
        }
 _getch();  
 return 0;
}

Построчнная запись (line by Line writing the file) 
Example. line by line write a text file.

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{setlocale(LC_ALL, "Russian");       // подключение  русификатора
 ofstream out_file ("Out_file.txt", ios::out);
 if (!out_file){ cerr << "Error output file"
                           << endl;
                          exit(1);
                                }
 out_file << "Я не знаю, где встретиться\n";
 out_file << "Нам придется с тобой,\n";
 out_file << "Глобус крутится-вертится,\n";
 out_file << "Словно шар голубой\n";
 _getch();  
 return 0;
}


End of file
ဖိုင္တစ္ခုအား open ျပဳလုပ္ျပီး read ျပဳလုပ္ရန္အတြင္ ၎ဖိုင္အတြင္းတြင္ ၎ဖိုင္၏ ေနာက္ဆံုးေနရာအား ရွာေဖြျခင္းကို ပံုမွန္အားျဖင့္ ျပဳလုပ္ရေလ့ရွိသည္။ ဖိုင္တစ္ခု၏ ေနာက္ဆံုးေနရာဆိုသည္မွာ ထိုဖိုင္အတြင္းတြင္ ထပ္မံ၍ read ျပဳလုပ္ရန္ data မ်ားမက်န္ရွိေတာ့္ေသာ ေနရာကိုဆိုလိုျခင္းျဖစ္သည္။
End of file အားရွာေဖြရန္အတြက္ ေအာက္ေဖာ္ျပပါ ေဖာ္ျပခ်က္အတိုင္း အသံုးျပဳနိုင္သည္။
while ( !in_file.eof()) {……}
အထက္ေဖာ္ျပပါ ေနရာတြင္ do .. while loop အားအသံုးျပဳရန္မသင့္ေတာ့္ေပ။
do{…..} while (!in_file.eof())
တစ္ခ်ိဳ႕ေသာ ဖိုင္မ်ားသည္ read ျပဳလုပ္စရာမရွိေသာ empty_data ဖိုင္မ်ားျဖစ္ေနတက္သည္။ အဆိုပါ အေျခအေနမ်ိဳးတြင္ do.. while loop သည္ အနည္းဆံုးတစ္ၾကိမ္ျပဳလုပ္ရန္လိုအပ္သျဖင့္ error ျဖစ္ေပၚနိုင္သည္ျဖစ္သည္။
End of file စစ္ေဆးျခင္းသည္ ဖိုင္၏ ေနာက္ဆံုးေနရာအား စစ္ေဆးျခင္းျဖစ္ျပီး read လုပ္ေနခ်ိန္ အတြင္းတြင္ မည္သည့္ အမွားကိုမွ analysis ျပဳလုပ္မည္မဟုတ္ေပ။
ထိုကဲ့သို႕ end of file အထိ data မ်ားအား ေကာင္းမြန္စြာ read လုပ္ရန္အတြက္ while(in_file.good()) {….} အားအသံုးျပဳျခင္းျဖင့္ data မ်ား read မလုပ္နိုင္ေသာအေျခအေနအတြင္ exit ျပဳလုပ္နိုင္သည္ ျဖစ္သည္။
Loop operator ျဖစ္ေသာ while(in_file) {….}  loop သည္ error မေတြ႕မခ်င္းဖိုင္အား read ျပဳလုပ္သြားမည္ျဖစ္သည္။၎သည္ eof အား အဆိုပါအေျခအေနတြင္ error အျဖစ္သတ္မွတ္ျပီး interpreted ျပဳလုပ္သြားမည္ျဖစ္သည္။ ( ထိုသို႕ eof() ကိုေတြ႕ရွိျပီး error အျဖစ္ သတ္မွတ္လိုက္ျခင္း အားျဖင့္ ၎လုပ္ေဆာင္ေနေသာ loop မွ exit ျပဳလုပ္လိမ့္မည္ျဖစ္သည္။ )


translated by zmk@miet51




No comments:

Post a Comment