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();