LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-01-2004, 10:13 PM   #1
xemous
Member
 
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80

Rep: Reputation: 15
c++ writing to a .txt file


i need to write charactors and intergers to a txt file, then read back of the txt file

im new to c++ and know some basics

but i need some example of this, or a url linking to an example would be appreciated
 
Old 09-01-2004, 10:15 PM   #2
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 545

Rep: Reputation: 30
I don't know of any good URLs to link, but did you try looking yourself? I just googled for "C++ File IO" and found a plethora of links to examples. I recommend you use the power of self-searching
 
Old 09-01-2004, 10:18 PM   #3
xemous
Member
 
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80

Original Poster
Rep: Reputation: 15
http://www.cplusplus.com/doc/tutorial/tut6-1.html

im currently looking at this atm


im looking for a simple tutorial that explains how to read and write charactors and intergers of a txt file, and the ones iv found are difficult to follow

programming is not my thing, but i have not choice here i need to figure out how this portion works
 
Old 09-01-2004, 10:18 PM   #4
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
Text writing is very simple.

Code:
#include <iostream>
#include <fstream>

int main()
{
   ofstream out( "textfile.txt" );
   if( !out )
   {
      cout << "Couldn't open file."  << endl;
      return 1;
   }

   out << "This is output to the file.\n";

   out.close();

   return 0;
}
This is pretty basic but you get the idea.

Last edited by leonscape; 09-01-2004 at 10:19 PM.
 
Old 09-01-2004, 10:20 PM   #5
xemous
Member
 
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80

Original Poster
Rep: Reputation: 15
what compiler did you use?
 
Old 09-01-2004, 10:23 PM   #6
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
I used GCC.

Edit: I missed out "using namespace std;" also GCC is misleading as you call g++.

Last edited by leonscape; 09-01-2004 at 10:27 PM.
 
Old 09-01-2004, 10:49 PM   #7
xemous
Member
 
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80

Original Poster
Rep: Reputation: 15
hi again

i played with some examples and i have new question

#include <iostream>
main()
{

char ch[120];
int u;

std::cin >> ch;
std::cin >> u;

std::cout << ch << u;

int x;
std::cin >>x;
return 0;
}

here is a simple program which takes some char and int

#include <fstream.h>
#include <iostream>
int main () {
ofstream examplefile ("customer.txt");
if (examplefile.is_open())
{

examplefile << "This is a line.\n";
examplefile << "This is another line.\n";
examplefile.close();
}
return 0;
}

here is a program which just writes to text file

how would you express, if the programs were merged, to write the char ch, and the int u , into the example text file

i tried the obvious newbie

examplefile << ch;
examplefile << u;

but i got a compile errors

is there another way?
 
Old 09-01-2004, 10:53 PM   #8
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
What where the compiler errors?
 
Old 09-01-2004, 10:57 PM   #9
xemous
Member
 
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80

Original Poster
Rep: Reputation: 15
leonscape
thanks for help mate
sorry about that, it rewrote the program more carefully and it compiled correctly

here is the new verison

#include <iostream>
#include <fstream.h>
int main()
{
char ch[120];
int u;

std::cin >> ch;
std::cin >> u;

std::cout << ch << u;
ofstream examplefile ("customer.txt");
if (examplefile.is_open())
{

examplefile << ch;
examplefile << u;
examplefile.close();
}
return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading from a txt file into a two dimension array in C kponenation Programming 3 11-26-2005 07:04 PM
Txt extension file translation UltraSoul Solaris / OpenSolaris 3 05-21-2005 11:11 AM
How to convert a txt file to be a db file in Redhat linux 9? winnie Linux - Newbie 3 06-27-2003 08:33 AM
how to edit a txt file through console safrout Linux - Newbie 5 01-31-2003 07:39 PM
txt file to html > right forum ? saavik Programming 7 04-16-2002 08:42 AM


All times are GMT -5. The time now is 09:09 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration