for Robot Artificial Inteligence

21. Fstream

|

Fstream diagram

File Opening mode

  • Library Fstream

Example 1

Openning_flags.cpp

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream file;

    file.open("sample.txt", ios::out | ios::app);

    /*
        ios::in - INPUT - READING
        ios::out - OUTPUT - WRITE TO FILE, if there is no file then create it, if there is a file then truncate it (remove content) unless it occurs with ios::in flag
        ios::trunc - TRUNCATE - it is truncating the file (cutting everything inside)(截短)
        ios::ate - At The End - sets pointer at the end of file - the place of pointer can be changed in that mode, it's possible to read and write in that mode
        ios::app - Append - the content is added at the end of file (it's not possible to remove content nor adding something in other place than the end of file)
        ios::binary - opens the file as a binary file. to open image or some thing it is useful

    */

    /*
        DEFAULT MODE (FLAGS)
        fstream - ios::out | ios::in
        ifstream - ios::in
        ofstream - ios::out
    */


    if (file.is_open())
    {
        file << "sample text\n";
        file << "sample text\n"; //those are we can write and it save in the file

    }
    else
        cout << "The file hasn't been opened properly";


    return 0;
}

Example 2

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream myFileHandler;

    myFileHandler.open("test.txt");

    if (myFileHandler.is_open())
    {
        cout << "The file has been opened properly";

        myFileHandler << "this is a sample text //input contents into txt file

        myFileHandler.close();
    }

    return 0;
}

Example 3

  • ios::rdstate
    • 현재 스트림의 오류 상태 플래그를 리턴한다.
    • 오류 상태 플래그(error state flag)를 얻어온다.
    • 오류 상태 플래그는 입출력 함수를 호출할 때 발생하는 오류에 따라 자동으로 설정되는 플래그이다.
  • Bitwise XOR - ^ (caret) eXclusive OR.
  • Clear(): Flage들을 초기화 한다

예제

/*

test.txt 를 in 형식으로 open 하였으므로 읽기만 가능한다. 따라서 쓰기를 하면
오류가 발생하므로 myfile.fail() 이 true 가 되고 입출력 작업은 중지되지만 오류
상태 플래그를 초기화함으로써 나중에 getline 을 수행할 수 있게 된다.

이 예제는
http://www.cplusplus.com/reference/iostream/ios/clear/
에서 가져왔습니다.

*/
#include <fstream>
#include <iostream>
using namespace std;

int main() {
  char buffer[80];
  fstream myfile;

  myfile.open("test.txt", fstream::in);

  myfile << "test";
  if (myfile.fail()) {
    cout << "Error writing to test.txt\n";
    myfile.clear();
  }

  myfile.getline(buffer, 80);
  cout << buffer << " successfully read from file.\n";

  return 0;
}

결과

Example 4

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    /*
        tellg - tell get - tell where is the reading pointer
        seekg - seek get - set reading pointer at specified position


        seekg(how_many_bytes_from_the_flag_place, flag);

        possible flags:
        ios::beg - (begin) set from the begin (default)
        ios::end - set from the end
        ios::cur - (current) set from current place
    */

    fstream file;

    file.open("sample.txt", ios::in | ios::binary);

    if (file.is_open())
    {
        string buffer;

        file.seekg(0, ios::end); // this is where the word position from start

        streampos sizeOfFile = file.tellg(); // stream pos, to read

        file.seekg(0); // it is set the zero as begging of pointer so it can read it again from start point, and if
        // we dont use it, it just top the next cout and not execute the reading inside do while function


        cout << "The size of the file is " << sizeOfFile << " bytes" << endl;
        do
        {
            file >> buffer;

            cout << buffer << endl;
        }while (!file.eof());

        if ((file.rdstate() ^ ifstream::eofbit) == 0)
        {
            file.clear();
            cout << file.tellg() << endl; // it results means that binary position that showing last word of pointer position in binary
            file >> buffer;

            cout << buffer << endl;
            //set indicator of place in file to some other place
            // some other operations on file
        }

    }
    else
        cout << "The file couldn't be opened properly" << endl;

    return 0;
}

result

Example 5

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    /*
        tellp - tell put - tells where is the putting pointer
        seekp - seek put - sets writing (putting) pointer at specified position


        seekp(how_many_bytes_from_the_flag_place, flag);

        possible flags:
        ios::beg - (begin) set from the begin (default)
        ios::end - set from the end
        ios::cur - (current) set from current place
    */

    fstream file;

    file.open("sample.txt", ios::out | ios::binary);

    if (file.is_open())
    {
        string tmp = "this is text about nothing";

        file << tmp;

        cout << file.tellp() << endl;

        file.seekp(0, ios::beg);

        file << "T";
    }
    else
        cout << "The file couldn't be opened properly" << endl;

    return 0;
}

result

Comments