Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,983 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,361 people online right now. Registration is fast and FREE... Join Now!




text files

 
Reply to this topicStart new topic

text files

sunny123
1 Dec, 2006 - 04:44 AM
Post #1

New D.I.C Head
*

Joined: 19 Oct, 2006
Posts: 11


My Contributions
im about to start a assignment which requires me to write to and display from a text file. a report needs to be diaplyed from the text file

can anyone help me give any tips on how to create the text file ?? so i can write to it and display from it?
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: Text Files
1 Dec, 2006 - 05:11 AM
Post #2

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 549



Thanked: 4 times
Dream Kudos: 125
My Contributions
from the top of my head you would need something like
CODE


#include <iostream>
#include <fstream>

int main (int argc, char * const argv[]) {
    
    ofstream result_file ( "test.txt", ios::app );
    
    result_file << "Hello world! \n";
}


User is offlineProfile CardPM
+Quote Post

ifoam
RE: Text Files
1 Dec, 2006 - 11:48 AM
Post #3

D.I.C Head
**

Joined: 26 Oct, 2006
Posts: 54



Thanked: 1 times
My Contributions
Another example:

CODE


#include <iostream>
#include <fstream>

void main()
{

string strcap;

//sets a stream operator? as file and opens test.txt. you can now use the word file like cout
//to write to a text file.
ofstream file("test.txt")

//writes "im writing to a file" into test.txt.
file << "im writing to a file!";

//closes the file
file.close();

//sets a stream operator? as infile and opens read.txt. you can now use the word infile like
//cin to read information from the text file.
ifstream infile("read.txt");

//get's the first word of the file
infile >> strcap;

//closes the file after your done.
infile.close();
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 07:12PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month