Input/Output with files

G

gerarcone

Hi to all,
I have only few knowledges about C++, and my programs run only in console; I
haven't studied yet classes. I should create a little program to manage some
numerical data, but I need to store that data in a file, and I don't know
how.
What I know is only how to write text strings in files, but I should write
others data types, such as records or simple integers. My idea of file in
that program is a sort of alternative RAM. Better if later I can transform
some data in strings so they can be written on a txt file.

Hoping in your help, gerarcone.
 
S

Sumit Rajan

gerarcone said:
Hi to all,
I have only few knowledges about C++, and my programs run only in console;
I haven't studied yet classes. I should create a little program to manage
some numerical data, but I need to store that data in a file, and I don't
know how.
What I know is only how to write text strings in files, but I should write
others data types, such as records or simple integers. My idea of file in
that program is a sort of alternative RAM. Better if later I can transform
some data in strings so they can be written on a txt file.

I'm not sure what you are after as it is not very clear from what you have
written. However, writing numbers to a text file can be done in exactly the
same way as you would write a string (a C-style one or a std::string) to
such a file:

#include <fstream>
#include <iostream>

int main()
{
std::eek:fstream ofs("filename.txt");
if (ofs) {
ofs << "a number: " << 34 << std::endl;
}
else {
std::cerr << "Failed to open file.\n";
}
}

Regards,
Sumit.
 
J

John Harrison

gerarcone said:
Hi to all,
I have only few knowledges about C++, and my programs run only in console; I
haven't studied yet classes. I should create a little program to manage some
numerical data, but I need to store that data in a file, and I don't know
how.
What I know is only how to write text strings in files, but I should write
others data types, such as records or simple integers. My idea of file in
that program is a sort of alternative RAM. Better if later I can transform
some data in strings so they can be written on a txt file.

Hoping in your help, gerarcone.

If you want to do binary I/O you should look at the read, seekg and
tellg methods of istream and the write, seekp and tellp methods of
ostream. And you should look up what it really means to open a file in
'binary' mode.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top