Solved

help on writing to a file.

Posted on 2001-08-09
7
175 Views
Last Modified: 2010-04-02
I'm relatively new to C++, and was wondering how it was possible to write to a created temp file, so that my program can read from it when my program loads so it can gain information written to the file.
0
Comment
Question by:bobbygordon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Accepted Solution

by:
andysalih earned 150 total points
ID: 6370548
this explains how to write a file in .txt format, well write information to the file

ofstream fout;


That?s all for that, but to open the file, you must call ofstream::open() like this.


fout.open("output.txt");


You could have also just as well opened the file as you declared the stream by passing the filename as a parameter to the constructor of the object.


ofstream fout("output.txt");


This will be our selected method of declaring objects, since it?s still rather simple to see how to create and open the file. By the way, if the file you are opening for output does not exist, it will be created for you, so there is no need to ever worry about creating the file. Now to output to the file, it works exactly like how you would output to "cout." For those of you that don?t know "cout" for console output, then here?s an example.


int num = 150;
char name[] = "John Doe";
fout << "Here is a number: " << num << "\n";
fout << "Now here is a string: " << name << "\n";


Now to save the file, you must close the file, or flush the buffer to the file. Closing the file will not let you access it anymore, so only call it when you are done using the file and it will automatically save the file for you. Flushing the buffer will write the buffer to the file and still keep the file open, so use this function when necessary. Flushing is done by another output looking call, and closing is done by a method call. Like this:


fout << flush;
fout.close();


Now the contents of the file when you open it in a text editor will read:


Here is a number: 150
Now here is a string: John Doe

cheers
Andy
0
 
LVL 3

Expert Comment

by:andysalih
ID: 6370572
this is how you would read it back in

ifstream fin("filename.txt");
int number;
char letter, word[8];
fin >> number;fin >> word;


hope that helps

andy

0
 

Author Comment

by:bobbygordon
ID: 6370600
What header file would i need to include that contains this ofstream and fout?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:trongnghia
ID: 6370800
This's header file:

#include <fstream.h>

0
 

Author Comment

by:bobbygordon
ID: 6370820
I just increased the points...

Is there a way to write and recieve this data, to and from a database as opposed to just a text file?

Or is there a way to use the text file as a database?
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6371917
A technicality: the header file should be <fstream> - it puts the declarations in the std namespace.

There are many ways to read and write to a database.  It will probably (almost certainly) entail more work than using a textfile.  If you intend to distibute your application, you'd also need to distribute the database. The methods you use will depend on the database you intend to use, and depending on the amount/type of info it might be a complete overkill.

You already are using the text file as a database - it's just a very primitive and non-standard one.

If you still want to go down the database path (which would be a good idea if you're storing lots of info), you'll need to post more details.
0
 

Author Comment

by:bobbygordon
ID: 6373127
For the details that I gave, this helped me understand the concept fairly well.

Thanks,
-Bobby Gordon
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question