writing text to a file...

W

Wodzu

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
 
M

Mike Wahler

Wodzu said:
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::eek:fstream 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
 
T

Thomas Matthews

Wodzu said:
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
 
W

Wodzu

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
 
W

Wodzu

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
 
K

Karl Heinz Buchegger

Wodzu said:
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.
 
W

Wodzu

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
 

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

No members online now.

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top