22. Fstream(2)
22 Sep 2019 | C++
Comparing content of two files
read 함수
basic_istream& read(char_type* s, std::streamsize count);
- istream::read(istream에서 정의 됨)
- 스트림에서 문자들을 받아온다.
- 이 함수는 sentry (警卫)객체를 먼저 생성한 후, 이를 확인한 다음에 s 가 가리키는 공간에 문자들을 읽어와서 저장합니다. 이 떄 아래 조건을 만족할 때 까지 문자들을 계속 읽어들이게 됩니다.
- count 개의 문자들을 읽었을 때
-
EOF 가 발생하였을 때 (이 경우 setstate(failbit
eofbit) 이 호출되었을 것입니다.) 읽어들인 문자의 수는 gcount() 함수를 호출함으로써 알아낼 수 있습니다.
- 인자들
- s - 읽어들인 문자들을 저장할 문자 배열
- count - 문자 몇 개를 읽을 것인지.
- 리턴값
- (asterisk)this
Example 1
#include <iostream>
#include <fstream>
#include "string.h"
//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
using namespace std;
bool areFilesEqual(fstream *, fstream *); // fstream doesn't have copy constructor, so we use pointer
int sizeOfFile(fstream *);
int main()
{
/*
read(where to read, how many bytes to read);
memcmp it stands memory compare
*/
fstream file1, file2;
file1.open("sample.txt", ios::in | ios::binary | ios::ate);
file2.open("sample2.txt", ios::in | ios::binary | ios::ate);
if (file1.is_open() && file2.is_open())
{
if (areFilesEqual(&file1, &file2))
{
cout << "Files are equal";
}
else
cout << "Files are not the same" << endl;
}
else
cout << "The file couldn't be opened properly" << endl;
return 0;
}
bool areFilesEqual(fstream *a, fstream *b)
{
int fileSize1 = sizeOfFile(a);
int fileSize2 = sizeOfFile(b);
if (fileSize1 == fileSize2)
{
int BUFFER_SIZE;
if(fileSize1 > 1024)
BUFFER_SIZE = 1024;
else
BUFFER_SIZE = fileSize1;
char *file1buffer = new char[BUFFER_SIZE]; //쓸데없는 휘발성 값이니
char *file2buffer = new char[BUFFER_SIZE];
do
{
a->read(file1buffer, BUFFER_SIZE);
b->read(file2buffer, BUFFER_SIZE);
if (memcmp(file1buffer, file2buffer, BUFFER_SIZE) != 0)
{
cout << "Files are not equal, at least one of the byte was different" << endl;
delete [] file1buffer;
delete [] file2buffer;
return false;
}
}while(a->good() && b->good()); //a->good() means a is good() state, and b->good means iit is good() state
delete [] file1buffer;
delete [] file2buffer;
return true;
}
else
{
cout << "Size of Files are not equal" << endl;
return false;
}
}
int sizeOfFile(fstream * file)
{
file->seekg(0, ios::end); //check the from begging to end
int sizeOfFile = file->tellg(); // check the pointor position
file->seekg(0, ios::beg); // and back the pointer of position as origin
return sizeOfFile;
}
Extracting Characters from files
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
/*
getline(where to store the extracted characters, how many characters should be taken unless, separator(delimiter)) - extracts seperators and delete it
get(where to store the extracted characters, how many characters should be taken unless, separator(delimiter)) - doesn't extract seperator
ignore(how_many_characters_to_extracte AND TO IGNORE THEM, separator) - extracts characters
get();
*/
fstream file;
file.open("sample.txt", ios::in | ios::binary);
if (file.is_open())
{
char first, second;
char buffer[50];
cin >> buffer;
// cin >> getline(buffer, 50) it can get all the word how many word i write, but just cin >> buffer if there is 'space' it just stop
cout << buffer << endl;
do
{
file.getline(buffer, 50, ' '); // ' ' <- this is just delimeter , so if we change it to 'a', this function will be reading by a. (예로 나는 사람이다에서 띄어쓰기 부분 ' ' <- 에서 멈춘다)
// 50 means we get 50 character from first sentence of last word
second = file.get(); // 그리고 그 띄어씌기 이후의 문자 하나를 받는다.
file.ignore(40, '\n'); // '\n' means that in there is seperator
cout << buffer << " " << second << ". " << endl;
} while(!file.eof());
}
else
cout << "The file couldn't be opened properly" << endl;
return 0;
}
result
Gcount Counting Characters from last operation
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
/*
gcount - Getcharacter Count - get count of extracted characters from last extraction operation
*/
fstream file;
file.open("sample.txt", ios::in | ios::binary);
if (file.is_open())
{
char buffer[250];
do
{
file.getline(buffer, 250);
cout << buffer << " " << file.gcount() << endl;
}while(!file.eof());
}
else
cout << "The file couldn't be opened properly" << endl;
return 0;
}
result
Comparing content of two files
read 함수
basic_istream& read(char_type* s, std::streamsize count);
- istream::read(istream에서 정의 됨)
- 스트림에서 문자들을 받아온다.
- 이 함수는 sentry (警卫)객체를 먼저 생성한 후, 이를 확인한 다음에 s 가 가리키는 공간에 문자들을 읽어와서 저장합니다. 이 떄 아래 조건을 만족할 때 까지 문자들을 계속 읽어들이게 됩니다.
- count 개의 문자들을 읽었을 때
-
EOF 가 발생하였을 때 (이 경우 setstate(failbit eofbit) 이 호출되었을 것입니다.) 읽어들인 문자의 수는 gcount() 함수를 호출함으로써 알아낼 수 있습니다.
- 인자들
- s - 읽어들인 문자들을 저장할 문자 배열
- count - 문자 몇 개를 읽을 것인지.
- 리턴값
- (asterisk)this
Example 1
#include <iostream>
#include <fstream>
#include "string.h"
//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
using namespace std;
bool areFilesEqual(fstream *, fstream *); // fstream doesn't have copy constructor, so we use pointer
int sizeOfFile(fstream *);
int main()
{
/*
read(where to read, how many bytes to read);
memcmp it stands memory compare
*/
fstream file1, file2;
file1.open("sample.txt", ios::in | ios::binary | ios::ate);
file2.open("sample2.txt", ios::in | ios::binary | ios::ate);
if (file1.is_open() && file2.is_open())
{
if (areFilesEqual(&file1, &file2))
{
cout << "Files are equal";
}
else
cout << "Files are not the same" << endl;
}
else
cout << "The file couldn't be opened properly" << endl;
return 0;
}
bool areFilesEqual(fstream *a, fstream *b)
{
int fileSize1 = sizeOfFile(a);
int fileSize2 = sizeOfFile(b);
if (fileSize1 == fileSize2)
{
int BUFFER_SIZE;
if(fileSize1 > 1024)
BUFFER_SIZE = 1024;
else
BUFFER_SIZE = fileSize1;
char *file1buffer = new char[BUFFER_SIZE]; //쓸데없는 휘발성 값이니
char *file2buffer = new char[BUFFER_SIZE];
do
{
a->read(file1buffer, BUFFER_SIZE);
b->read(file2buffer, BUFFER_SIZE);
if (memcmp(file1buffer, file2buffer, BUFFER_SIZE) != 0)
{
cout << "Files are not equal, at least one of the byte was different" << endl;
delete [] file1buffer;
delete [] file2buffer;
return false;
}
}while(a->good() && b->good()); //a->good() means a is good() state, and b->good means iit is good() state
delete [] file1buffer;
delete [] file2buffer;
return true;
}
else
{
cout << "Size of Files are not equal" << endl;
return false;
}
}
int sizeOfFile(fstream * file)
{
file->seekg(0, ios::end); //check the from begging to end
int sizeOfFile = file->tellg(); // check the pointor position
file->seekg(0, ios::beg); // and back the pointer of position as origin
return sizeOfFile;
}
Extracting Characters from files
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
/*
getline(where to store the extracted characters, how many characters should be taken unless, separator(delimiter)) - extracts seperators and delete it
get(where to store the extracted characters, how many characters should be taken unless, separator(delimiter)) - doesn't extract seperator
ignore(how_many_characters_to_extracte AND TO IGNORE THEM, separator) - extracts characters
get();
*/
fstream file;
file.open("sample.txt", ios::in | ios::binary);
if (file.is_open())
{
char first, second;
char buffer[50];
cin >> buffer;
// cin >> getline(buffer, 50) it can get all the word how many word i write, but just cin >> buffer if there is 'space' it just stop
cout << buffer << endl;
do
{
file.getline(buffer, 50, ' '); // ' ' <- this is just delimeter , so if we change it to 'a', this function will be reading by a. (예로 나는 사람이다에서 띄어쓰기 부분 ' ' <- 에서 멈춘다)
// 50 means we get 50 character from first sentence of last word
second = file.get(); // 그리고 그 띄어씌기 이후의 문자 하나를 받는다.
file.ignore(40, '\n'); // '\n' means that in there is seperator
cout << buffer << " " << second << ". " << endl;
} while(!file.eof());
}
else
cout << "The file couldn't be opened properly" << endl;
return 0;
}
result
Gcount Counting Characters from last operation
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
/*
gcount - Getcharacter Count - get count of extracted characters from last extraction operation
*/
fstream file;
file.open("sample.txt", ios::in | ios::binary);
if (file.is_open())
{
char buffer[250];
do
{
file.getline(buffer, 250);
cout << buffer << " " << file.gcount() << endl;
}while(!file.eof());
}
else
cout << "The file couldn't be opened properly" << endl;
return 0;
}
result
Comments