본문 바로가기

Computer General/C++ development from the beginning

File Input/Output

#include <iostream>

#include <fstream>    //need to include this(stream textfile in and out)

using namespace std;

int main()

{
 ofstream myfile; //create an object that allows outputfiles to create file
 myfile.open("newfile.txt"); //it would create a file for you if not exists...
 myfile << "This will show up in the file \n"; //input to this text file
 myfile.close(); //cuz memory release...
 system("pause");
return 0;
}

 

 

/////////////////////////////////////////////////

#include <iostream>
#include <cstdlib>
#include <fstream>    //need to include this(stream textfile in and out)

using namespace std;

int main()

{
 char filename[50];
 ifstream bucky;
 //give user opportunity a file to open..
 cin.getline(filename,50);
 bucky.open(filename);
 //test if the file is opened..
 if(!bucky.is_open()){
  exit(EXIT_FAILURE); //from cstdlib
 }
 char word[50];
 bucky >>word; //get the value
 while(bucky.good()){ // good => as long as it's not eof
  cout<<word<<" ";
  bucky>>word;
 }
 system("pause");
return 0;
}

'Computer General > C++ development from the beginning' 카테고리의 다른 글

Structures  (0) 2014.12.16
Switch statement  (0) 2014.12.16
Logical OR operator  (0) 2014.12.16
The if Statement  (0) 2014.12.12
Address Operator & Pointer  (0) 2014.12.12