Multi-line File streaming

This is a discussion on Multi-line File streaming within the C++ Programming forums, part of the General Programming Boards category; Okay, here is the problem: I want to open a file with the code: Code: #include <fstream.h> int main() { ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    18

    Multi-line File streaming

    Okay, here is the problem:
    I want to open a file with the code:
    Code:
    #include <fstream.h> 
    
    int main() 
    
    { 
    	ofstream print; // stream variable declaration 
    	print.open("LPT1"); // open stream 
    	
    	// Print Text (the character \f will produce a form feed) 
    	print << "This text will print on the printer.\f"; 
    	print.close(); // close stream 
    	return 0; 
    }
    (written from another Post here, cant remember from who, sorry ILL FIND OUT NOW!)
    and I want to print it onto the screen
    The file is called text.txt
    Here is the code I use now:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    int main()
    {
    	char str[300];
    	ifstream a_file("text.txt");
    	a_file>>str;
    	cout<<str<<endl;
    	a_file.close();
    	return 0;
    }
    When I execute the program all it has is
    #include

    Someone plz help me
    HACKERS MANIFESTO (NOTE MOST WAS CUT OFF) (PEOPLE SAID IT WAS TOO LONG)
    This is our world now... the world of the electron and the switch, the beauty of the baud.
    We make use of a service already existing withoutpaying for what could be dirt-cheap if it wasn't run by profiteering gluttons, and you call us criminals.
    We explore... and you call us criminals.
    We seek after knowledge... and you call us criminals.
    We exist without skin color, without nationality, without religious bias... and you call us criminals.
    You build atomic bombs, you wage wars, you murder, cheat, and lie to us and try to make us believe it's for our own good, yet we're the criminals.

    Yes, I am a criminal. My crime is that of curiosity. My crime is that of judging people by what they say and think, not what they look like. My crime is that of outsmarting you, something that you will never forgive me for.

    I am a hacker, and this is my manifesto. You may stop this individual,but you can't stop us all... after all, we're all alike.
    +++The Mentor+++

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    When I execute the program all it has is
    #include
    That's because the extraction operator (>>) will read up until whitespace is encountered. You'll either have to read the file into one large string using ifstream::get(), or loop through it reading one word at a time until you reach eof.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21