demonstrates,example,fstream,File,Very,simple
Quick Search for:  in language:    
demonstrates,example,fstream,File,Very,simple
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 451,578 lines
 Jobs: 558 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for C/ C++.
Url Test
By Richard Creary on 8/27


Nice Console Calc
By Andrew Carter on 8/26


Visual Pi Hex 2
By jo122321323 on 8/26


Cascade Clone v1.0
By Paulo Jorente - aka JungleBoy on 8/26


Bubble Sort Algo
By d1rtyw0rm on 8/26


Copy File I/O
By Olivier Bastien on 8/25


Visual Pi Hex
By jo122321323 on 8/25


Click here to see a screenshot of this code!ShortCutSample
By Massimiliano Tomassetti on 8/25

(Screen Shot)

AnPMoneyManager beta
By Anthony Tristan on 8/24


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



 
 
   

C++ File I/O Examples

Print
Email
 
VB icon
Submitted on: 12/5/2000 11:04:50 AM
By: *LuckY* 
Level: Beginner
User Rating: By 13 Users
Compatibility:C++ (general)

Users have accessed this code 30474 times.
 
(About the author)
 
     This demonstrates an example of C++'s fstream File I/O. Very simple; the basic stuff for beginners to learn from. If you would like to see how to do something more in the direction this code leads, comment on (and rate) it telling me what specifically you want to see and I'll be glad to suffice.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code 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 code (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 code 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 code or code's description.

//**************************************
//     
// Name: C++ File I/O Examples
// Description:This demonstrates an exam
//     ple of C++'s fstream File I/O. Very simp
//     le; the basic stuff for beginners to lea
//     rn from. If you would like to see how to
//     do something more in the direction this 
//     code leads, comment on (and rate) it tel
//     ling me what specifically you want to se
//     e and I'll be glad to suffice.
// By: *LuckY*
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/xq/ASP/txtCod
//     eId.930/lngWId.3/qx/vb/scripts/ShowCode.
//     htm//for details.//**************************************
//     

//
// lucky760@yahoo.com
//
#include <fstream>
const char *FILENAME = "FILE.txt";
    int main() {
    	//create output object associated w/ file
    	ofstream fout(FILENAME);
    	cout << "Enter your secret password: ";
    	char str[60];
    	cin >> str;
    	//write to file
    	fout << "This is your secret password. Don't give it to anyone!\n";
    	fout << str;
    	//close file
    	fout.close();
    	cout << endl;
    	ifstream fin(FILENAME);
    	char ch;
    	while (fin.get(ch))
    		cout << ch;
    	fin.close();	
    	return 0;
}


Other 18 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 code(in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
12/6/2000 10:12:13 PM:Dvad78
Nice code, Could you show a little more 
file input?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/8/2000 11:14:17 PM:Philip Neil
Hey *Lucky* I am a newbie C++ but I 
have a rough idea about whats going on 
here. const char *FILENAME = 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/8/2000 11:19:25 PM:philip neil
ok lets try that again.. In do this and 
edit the const to the path but it 
doesn't write to the file. Can u help 
me man
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/11/2000 7:08:20 PM:Jim
Hi Lucky, I am a moderate programmer 
right now, and I under stand "ofstream" 
and "ifstream", but could you tell me 
how to interactivley take input from 
the file and edit at the same time; for 
example  i am reading a file, and i 
tell the program to stop when it finds 
a space, that is easy, but i want to be 
able to replace that space with a 
comma.  Any Ideas?  Your help would be 
appreciated (jimfinn@marist.net)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/24/2000 6:55:26 PM:Dennis Wrenn
Hello Jim, I think what you want is the 
seekp member of the ofstream class
you 
find the position of a space, however 
you are doing that, then you 
do:
fstream f("example.txt", ios::in | 
ios::out);
//do find 
stuff...
f.seekp(pos);
f << 
",";
f.close()
simple :-)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/21/2001 4:34:00 PM:Liam
hey you forgot to include 
iostream.h
but other wise it's cool, i 
don't suppose you could help me, i've 
been trying to create a program that 
will let you enter a file name for the 
input
thanx Ü
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/23/2001 5:32:45 PM:*LuckY*
Most newer compilers, Liam, don't 
require you to include <iostream> if 
<fstream> is already included. That's 
the reason for its absence.
To enter 
your own name for a file do something 
like this:
char str[20];
cout << 
"Enter filename: 
";
cin.getline(str,20);
ofstream 
f(str);
hth
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/24/2001 4:05:23 PM:Tom
Hey man... good code, I think I 
understand it well enough to utilize 
it.  I was wondering, though, how might 
I output the values of variables to a 
file, then, later on, read those values 
back into the variables?  Example: I 
have two int variables, first and 
second.  first = 32 and second = 12.  I 
want to output these two variables to 
the same file, then be able to read 
them back in (say, if I changed the 
values and wanted to change them back 
to whatever their value in the file 
was).
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/24/2001 4:41:57 PM:*LuckY*
Tom:
do something like the 
following:
//do file stuff first of 
course
int x=32,y=12;
fout << x << 
endl; //don't forget the endl
fout << 
y << endl;
int a,b;
fin >> a;
fin >> 
b;
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/28/2001 8:16:48 PM:Ice man
Sorry about that above.  I don't know 
what went wrong.  Anyways, if someone 
could explain WHY or HOW the fstream 
works, I would greatly apreciate it.  I 
understand how to use it, but I don't 
understand how it works
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/11/2001 3:35:48 PM:Jason
I want to read a file using fstream, 
but my input file is , delimited not 
space delimited. How should I read each 
field in my file with >>? Thanks 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/22/2001 5:43:01 PM:bar
Hi. I am working on a school project, 
and I have to keep some data in binary 
files (names and phone numbers of 
customers, descriptions of classes in a 
gym, etc). Is there any way you could 
explain the major difference between 
I/O of normal text files and I/O of 
binary files?  This is like a sink or 
swim project and I am kind of lost...I 
have only worked with .txt files so 
far.  Thank you.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/26/2001 11:27:48 PM:Charlie
I'd like to write a program that 
displays 10 lines of output from a file 
and then pauses untill the user strikes 
a key to show the next 10 lines of 
code, (etc. etc.) until the it reaches 
the eof? Can you help me?
Thanks 
Charlie  
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/10/2001 8:12:53 AM:toasthouse
I like this beginning code. I'm new to 
C   and am trying to learn how to grab 
a string variable from a dat file and 
then create another dat file with the 
extracted string variable from the 
first dat file. Any suggestions?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/10/2001 8:20:00 AM:mom
I am new to C++ and I am trying to 
write a program that will open a dat 
file(containing name and id on one 
line), read the id and then write the 
name to a new dat file. I've gotten the 
program to read the file but I can't 
figure out how to close the read.dat 
file , move the name info to a new dat 
file and display it or print it. Can 
you offer any suggestions to help me? 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/20/2001 8:59:01 AM:scrump
ok i understand how to use fstream.h. 
but last night i made the switch to 
linux only, windows98 installed and 
wouldnt boot! lol but, the ios modes 
included in the borland package and 
vc++ pack, have alot more options. such 
as if file doesnt exist dont create, 
append, trunc and so on. But the gnu 
compiler, g++'s fstream only allows in 
and out, and no file checking. has any 
one heard of header that i could use 
that would better suit my needs.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/24/2001 12:54:52 AM:Bipin Patel
   Can you help me with me program.  
This what I want to do.  I am writing a 
program in Visual C  .  This is all 
about I/O file. I want to search into 
directory in any drive for string. The 
string will be inserted by user on 
dialog editbox 4 to 5 strings (ie. 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/24/2001 1:24:11 AM:Bipin Patel
continue....(ie. "ACY","00:17:26","sa") 
Something like this.  After the string 
is found it should display only those 
line that contain found string.  
because later on I will have to put 
that lines block in Access Data base.  
If you can't help me with all this 
please atleast help me with reading 
more than one file at time for string.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/10/2001 9:48:45 AM:Homer
Hi
I am writing a program for a school 
which will allow the user to input 
student details and then append the 
records to a student file status.dat, 
it will then read the file and display 
the contents on screen. I'm using 
borland turbo c++........can you please 
give me an example of code that will do 
this ie create the file and display 
contents.........cheers.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/23/2001 6:52:51 PM:willow
HI there.  I need to read a series of 
chars from a file I done know how many 
chars are on each line.  Some may have 
3 others four my problem is I also have 
ints in this file that I want to read. 
How do I do this
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/23/2001 6:59:42 PM:willow
The above is kinda hard to understand 
let me give an example.  I have in my 
file,  an idnum 123, an amount 3 and a 
code rts, so my file looks like this.  
123 3 RTS. The next line in my file may 
look like this.  142 4 GLDA and the 
next, 456 1 P.  how do I read from such 
a file not knowing if the character 
section has 1, 2, or 3 characters?
I 
want to read them individualy.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/5/2002 10:26:44 AM:zoran
It is not <fstream>, it is <fstream.h> 
file that you need to include at the 
top.  Apart from that, the code is cool
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/5/2002 10:44:42 AM:*LuckY*
It depends on your compiler. I compiled 
with borland. In visual c++ you have to 
use 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/15/2002 9:24:16 PM:☻☺KDDFLX☺☻
This all sounds great and all, but 
unfortunatly for me, i don't use 
Borland(not that its bad or anything.), 
I use DJGPP/gcc. It seems that it 
doesnt have fstream.h. is there a clone 
of that or something i can use?BTW, 
this is pretty good
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/22/2002 6:34:54 AM:question
Hi...I'm beginner...I'm doing my 
assignment about wild card, and it 
needs to open a file. Do you know how 
to take information from for example 
data.txt and put it into an array. So 
in the data.txt there are information 
such cat dog fly elephent and after 
opening the data.txt the array will 
contain the info array[1]=cat 
ect...Thank YOuuuuuuu....=p
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/23/2002 11:39:26 AM:Keith
How would I save and load a double 
linked list to my program
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/20/2002 2:37:21 PM:cjr
hey can you email me and tell how all 
of this works
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 code 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 code, 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.