By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,226 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,226 IT Pros & Developers. It's quick & easy.

writing text to a file...

P: n/a
Hello there friends...

Can someone show me how to write some text to file and then read it from
that file?

I will be very greatfull for any help,

Best Regards,

Wodzu
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Wodzu" <br************@wp.pl> wrote in message
news:bq**********@opal.futuro.pl...
Hello there friends...

Can someone show me how to write some text to file and then read it from
that file?


Judging from the nature of your question, I'd really rather
show you some beginner's C++ texts (see www.accu.org book reviews),
but what the heck:

/* (error checking omitted for brevity) */

#include <fstream>
#include <iostream>
#include <string>

int main()
{
std::string data_out("some text");
std::string data_in;

/* write text to a file */
std::ofstream out("file.txt");
out << data_out << '\n';
out.clear();
out.close();

/* read text from file written above */
std::ifstream inp("file.txt");
std::getline(inp, data_in);

if(data_out != data_in)
std::cerr << "Something went wrong\n";

return 0;
}
-Mike

Jul 22 '05 #2

P: n/a
Wodzu wrote:
Hello there friends...

Can someone show me how to write some text to file and then read it from
that file?

I will be very greatfull for any help,

Best Regards,

Wodzu

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main(void)
{
ofstream out_file("my_file.txt");
if (!out_file)
{
cerr << "Error creating output file.";
return EXIT_FAILURE;
}
out_file << "Hello.\n";
out_file.close();
ifstream inp_file("my_file.txt")
if (!inp_file)
{
cerr << "Error opening input file.";
return EXIT_FAILURE;
}
string some_text;
inp_file >> some_text;
cout << "Data read from file: " << some_text << "\n";
inp_file.close();
return EXIT_SUCCESS;
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #3

P: n/a
Hello again,

Thank You very much for your help, unfortunately...i am getting a lot of
errors when i try to compile this two examples.
I am using borland 3.1 compiler and perhaps this is the reason...What should
i do?
I am not a beginner programmer but i am beginner in C ;)

Once again thanks for your future reply...

Wodzu
Jul 22 '05 #4

P: n/a
I solved my problem in little different aproach.(Dont know it is good or
bad).

if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* open file TEST.$$$ */
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
fwrite(&buffer, sizeof(buffer), 1, stream);
fclose(stream); /* close file */
Once again thanks for your help.

Best Regards,

Wodzu
Jul 22 '05 #5

P: n/a
Wodzu wrote:

Hello again,

Thank You very much for your help, unfortunately...i am getting a lot of
errors when i try to compile this two examples.
I am using borland 3.1 compiler and perhaps this is the reason...What should
i do?
I am not a beginner programmer but i am beginner in C ;)


Well. For one, the code posted is not C. It is C++.

Second: Help us to help you. What errors? Just the first 2 or
3 usually are sufficient to better understand what could be wrong.
Not sure, but: Isn't borland 3.1 hopelessly outdated? Try to get
a newer compiler in the first place.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6

P: n/a

Second: Help us to help you. What errors? Just the first 2 or
3 usually are sufficient to better understand what could be wrong.
I would do that but i get 14 errors and most of them were caused by
sufficent of some libaries used in this examples...
So describing this errors here doesnt have much sense...

Not sure, but: Isn't borland 3.1 hopelessly outdated? Try to get
a newer compiler in the first place.


I would do that but i dont have many for oryginal versions;>

Best Regards,

Wodzu
Jul 22 '05 #7

P: n/a

"Wodzu" <br************@wp.pl> wrote in message
news:bq**********@opal.futuro.pl...
Not sure, but: Isn't borland 3.1 hopelessly outdated? Try to get
a newer compiler in the first place.


I would do that but i dont have many for oryginal versions;>

Best Regards,


Borland offers an up-to-date C++ compiler for free at their
web site: www.borland.com

-Mike
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.