cpp_fstream - 8BitsCoding/RobotMentor GitHub Wiki
fileinandout
ํ์ผ ์ ์ถ๋ ฅ(fstream)
-
ifstream
- ํ์ผ ์ ๋ ฅ
-
ofstream
- ํ์ผ ์ถ๋ ฅ
-
fstream
- ํ์ผ ์ ๋ ฅ ๋ฐ ์ถ๋ ฅ
ํ์ผ ์ด๊ธฐ
// C
FILE* fp;
// ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ํ์ผ ์คํ
fp = fopen("helloWorld.txt", "r");
// ์ฐ๊ธฐ ์ ์ฉ์ผ๋ก ํ์ผ์ ์คํ(ํ์ผ์ด ์๋ค๋ฉด ๋ง๋ ๋ค.)
fp = fopen("helloWorld.txt", "w+");
// ์ฝ๊ธฐ์ ์ฐ๊ธฐ ๋ฒ์ฉ์ผ๋ก ํ์ผ์ ์คํ
fp = fopen("helloWorld.txt", "r+");
// C++
// ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ํ์ผ์ ์คํ
ifstream fin;
fin.open("helloWorld.txt");
// ์ฐ๊ธฐ ์ ์ฉ์ผ๋ก ํ์ผ์ ์คํ
ofstream fout;
fout.open("helloWorld.txt");
// ์ฝ๊ธฐ์ ์ฐ๊ธฐ ๋ฒ์ฉ์ผ๋ก ํ์ผ์ ์คํ
fstream fs;
fs.open("helloWorld.txt");
ํ์ผ ๋ซ๊ธฐ
// C
FILE* fp;
fclose(fp);
// C++
ifstream fin;
fin.close();
์คํธ๋ฆผ ์ํ ํ์ธํ๊ธฐ
// C
FILE* fp;
fp = fopen("helloWorld.txt", "r+");
if(fp!=NULL)
{
}
// C++
fstream fs;
fs.open("HellowWorld.txt");
if(fs.is_open())
{
}
ํ์ผ์์ ๋ฌธ์ ํ๋์ฉ ์ฝ๊ธฐ
// C
FILE* fp;
fp = fopen("HelloWorld.txt", "r");
char character;
do
{
character = getc(fp);
printf("%c", character);
} while(character != EOF);
fclose(fp);
// C++
ifstream fin;
fin.open("HelloWorld.txt");
char character;
while(true)
{
fin.get(character);
if(fin.fail())
{
break;
}
cout << character;
}
fin.close();
ํ์ผ์์ ํ ์ค์ฉ ์ฝ๊ธฐ
// C++
ifstream fin;
fin.open("Hellow World.txt");
string line;
while(!fin.eof())
{
getLine(fin, line);
cout << line << endl;
}
fin.close();
์ ์ฝ๋์ ๋ฌธ์ ์ ์ ํ์ผ์ด ๋น์ด์๋๊ฒฝ์ฐ(์์๋ถํฐ EOF์ธ๊ฒฝ์ฐ) 1๋ฒ์ ๋น์ค์ด ์ถ๋ ฅ์ด ๋๊ฒ ๋๋ค.
ํด๊ฒฐ์
ํ์ผ์์ ํ ๋จ์ด์ฉ ์ฝ๊ธฐ
ifstream fin;
fin.open("Hellow World.txt");
string name;
float balance;
while(!fin.eof())
{
fin >> name >> balance; // ๋ค์๊ณผ ๊ฐ์ด ์ฝ์ผ๋ฉด space๋ฅผ ๊ธฐ์ค์ผ๋ก ์ฝ์.
cout << name << ": $" << balance << endl;
}
fin.close();
๋จ, ์๋ชป๋ ์ ๋ ฅ์ด ์์ฌ์๋ค๋ฉด??
// ์ ๋๋ก ์ฝ์ ๊ฒ๋ง ์ถ๋ ฅํ์ while(!fin.eof()) { fin >> number; if(!fin.fail()) { cout << number << endl; } } // ์ค๊ฐ์ ์๋ชป๋ ์ฝ๋๊ฐ ์์๋ ๋ฒ๋ฆด ์ ์๋ ์ฝ๋๋ ํ์ํ๋ค.!
while(!fin.eof()) { fin >> number; if(fin.fail()) { fin.clear(); fin.ignore(LLONG_MAX, ' '); // ignore์ ํ ๊ฒฝ์ฐ ' ' ๋์ฌ๋๊น์ง ๋ฒ๋ฆผ. ๋จ, ์ค์ง ' '์๋ง ๋์... ํ๊ณ์ธ๊ฐ? } else { cout << number << endl; } }
// ํด๊ฒฐ์ฑ ifstream fin; fin.open("HelloWorld.txt"); int number; string trash; while(!fin.eof()) { fin >> number; if(fin.fail()) { fin.clear(); fin >> trash; // ์คํธ๋ง์ ๊ทธ๋ฅ ์ฝ์ด๋ฒ๋ฆฌ๊ณ ๋ฒ๋ฆฐ๋ค. } else { cout << number << endl; } }
์๋ ์ฝ๋์ ๋ฌธ์ ์ ์?
ifstream fin;
fin.open("HelloWorld.txt");
int number;
string trash;
while(!fin.eof())
{
fin >> number;
if(fin.fail())
{
fin >> trash;
fin.clear();
}
else
{
cout << number << endl;
}
}
clearํ๊ธฐ ์ ์ trash๋ก ์ฝ์ด์ค๋๋ฐ... failbit์ด true๋ผ๋ฉด ์ฝ์ด์ค์ง ๋ชปํ๋ค. -> ๋ฌดํ๋ฃจํ์ ๊ฑธ๋ฆฌ๊ฒ ๋๋ค.
ํ์ผ์ ์ฐ๊ธฐ
// C
FILE* fp;
fp = fopen("HelloWorld", "w");
char line[512];
if(fgets(line, 512, stdin)!=NULL)
{
fprintf(fp, "%s\n", line);
}
fclose(fp);
// C++
ofstream fout;
fout.open("HelloWorld.txt");
string line;
getline(cin, line);
if(!cin.fail())
{
fout << line << endl;
}
fout.close();
๋ฐ์ด๋๋ฆฌ ํ์ผ ์ฝ๊ธฐ
// C
struct Record{
char name[20];
int count;
};
FILE* fp;
fp = fopen("studentRecords.dat", "rb");
if(fp!=NULL)
{
Record record;
fread(&record, sizeof(Record), 1, fp);
// ๋ฐ์ดํธ ๋จ์๋ก ์ฝ๊ฒ ๋๋ค.
}
fclose(fp);
// C++
ifstream fin("studentRecords.dat", ios_base::in | ios_base::binary);
if(fin.is_open())
{
Record record;
fin.read((char*)&record, sizeof(Record));
}
fin.close();
๋ฐ์ด๋๋ฆฌ ํ์ผ์ ์ฐ๊ธฐ
FILE* fp;
fp = fopen("studentRecords.dat", "w");
if(fp!=NULL)
{
char bugger[20] = "Pope Kim";
fwrite(buffer, 20, 1, fp);
}
fclose(fp);
ofstream fout("studentRecord.dat", ios_base::out | ios_base::binary);
if(fout.is_open())
{
char bugger[20] = "Pope Kim";
fout.write(buffer, 20);
}
fout.close();
ํ์ผ ์์์์ ํ์
FILE* fp;
fp = fopen("studentRecords.dat", "w");
if(fp!=NULL)
{
if(fseek(fp, 20, SEEK_SET)==0)
{
// 21๋ฒ์งธ ์์น์์๋ถํฐ ๋ฎ์ด์ฐ๊ธฐ
}
}
fclose(fp);
// C++
fstream fs("helloWorld.dat", ios_base::in | ios_base::out | ios_base::binary);
if(fs.is_open())
{
fs.seekp(20, ios_base::beg);
if(!fs.fail())
{
// 21๋ฒ์งธ ์์น์์๋ถํฐ ๋ฎ์ด์ฐ๊ธฐ
}
}
fs.close();