Quick Search for:  in language:    
very,basic,tutorial,will,cover,file,input,out
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 728,035. lines
 Jobs: 113. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for C/ C++.
basic math stat4u
By snehitn on 2/23


Linked List with C
By AsiF Karim on 2/23


ClassDialog
By Bonj on 2/22


[Auto Library
By Mohit Sharma1 on 2/22


Message Digest (Console)
By Vikram Shinde on 2/21


Center a Dialog Box with Respect to its Owner Window
By Ixac on 2/21


Simulation Of Gas Station / Petrol Filling Station
By Sharath AV on 2/20


ffindc
By Bonj on 2/20


Matrix_1
By Norax on 2/20


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Simple File I/O for Beginners

Print
Email
 

Submitted on: 11/22/2003 4:37:09 PM
By: Sasha Slutsker  
Level: Beginner
User Rating: By 2 Users
Compatibility:C++ (general), Microsoft Visual C++, Borland C++

Users have accessed this article 1984 times.
 
(About the author)
 
     This is a very basic tutorial that will cover file input and output for beginners using <fstream>.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
Hello! This article descibes the basics of using < fstream> for file input and output. It won't go into the more advanced aspects of the topic, but is meant as an introduction to file input and output. Some C/C++ experience is expected.

#include < fstream> //This is the library that is included
#include < iostream>
using namespace std;
int main ()
{
fstream f1 ("Sasha.txt");
return 0;
}


What this does is create a variable f1 and sets it to "Sasha.txt." Now, let's set this variable to a string.

#include < fstream>
#include < iostream>
#include < string>
using namespace std;
int main ()
{
string dude = "Sasha.txt";
fstream f1 (dude.c_str);
return 0;
}


It should also be noted that if you want a file ONLY for input or ONLY for output you can do this:

#include < fstream>
#include < iostream>
#include < string>
using namespace std;
int main ()
{
string dude = "Sasha.txt";
ifstream f1 (dude.c_str);
//Only input
ofstream f1 ("someFile.txt"); //Only output
return 0;
}

Now, let's move on to reading and writing files. The following code will read every character from file "someFile.txt" (This code will also output it.)

#include < fstream>
#include < iostream>
#include < string>
using namespace std;
int main ()
{
fstream f1 ("someFile.txt");
char yeah;
while (f1 >> yeah)
cout << yeah;
return 0;
}


The right hand operator works for input. Now for ouput...

#include < fstream>
#include < iostream>
#include < string>
using namespace std;
int main ()
{
fstream f1 ("someFile.txt");
char yeah = 'A';
for (int i = 0; i < 20; i++)
f1 << yeah;
return 0;
}


This will out the character 'A' multiple times. (20 to be exand.) As you can tell, the left hand operator is used for outputing. Now, I will cover another of inputing which will input spaces. I will show this using a simple encryption program. Th line you need to pay attention to is the first for loop with "f1.get(readTo)"

#include < fstream>
#include < iostream>
#include < vector>
using namespace std;
int main ()
{
fstream f1 ("yeah.txt");
vector < char> yeah;
char readTo;
yeah.push_back (1);
for (int i = 0; f1.get(readTo); i++, yeah.push_back (1))
{
yeah [i] = ~(readTo);
cout << yeah[i];
}

cout << endl;

for (int j = 0; j < i; j ++)
{
cout << (char) (~(yeah [j]));
}

return 0;
}


Well, that's it! Hope you enjoyed this!


Other 11 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
11/25/2003 4:58:19 PM:
not bad :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.