473,385 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

writing text to a file...

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
7 2283
"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
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
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
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
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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Neil Trigger | last post by:
Is there a way of creating a seperate text file on a server every time a form is sent? -- ¿ Trigger ? http://www.magic2k.com/ http://www.oddmap.co.uk
2
by: sp | last post by:
Hello everybody, I have an xml doc and I am trying to write the values from xml file to a tab delimited text file. Currently, what I am doing is I am reading xml file through XpathNavigaotr and...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
3
by: CsharpNewcommer | last post by:
Hi Can someone tell me how to write the data from a TextBox (txt) or Label (lbl) to a text file to be printed. I have read the info from msdn.microsoft.com/library on "Writing Text to a File",...
2
by: Tony | last post by:
Yes, I need to specify a font type so that the characters will be evenly spaced when I write to a tab delimited text file. So how does one specify a font type to write/print and which font is...
0
by: Yunus's Group | last post by:
Yunus's Group May 23, 3:36 pm show options Newsgroups: microsoft.public.dotnet.languages.vb From: "Yunus's Group" <yunusasm...@gmail.com> - Find messages by this author Date: 23 May 2005...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
3
by: sethuganesh | last post by:
hi, i am not able to write a text properly in a text file in vb.net.below is the code that i have written. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As...
5
by: grinder | last post by:
first off, i am an extreme newbie to C. i am an undergrad research assistant and i have been shifted to a project that involves building a fairly involved c program. The part that i am stuck on now...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.