4 Replies - 949 Views - Last Post: 28 March 2011 - 12:57 PM Rate Topic: -----

#1 dastick  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 18-March 11

How to do assign file output from a stream to a variable

Posted 27 March 2011 - 06:43 AM

How do assign file output to a variable like this

ofstream file;
file.open("file.txt", ios::out);
std::string str = std::cout << filel;

Is This A Good Question/Topic? 0
  • +

Replies To: How to do assign file output from a stream to a variable

#2 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: How to do assign file output from a stream to a variable

Posted 27 March 2011 - 07:48 AM

ofstrem output; // Output to file
ifstream input; // Input from file

input.open("file.txt", ifstream::in)

string str;

input >> str; // Copy a string from the file into your variable

input.close; // Close the file when done



Try these tutorials
Basic C++ File I/O, by bluesuus
C++ BEGINNER FILE PROCESSING TUTORIAL, by Elcric
EOF And Reading Text Files (C++), by Bench
File IO With Numeric Data, by
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,663
  • Joined: 23-August 08

Re: How to do assign file output from a stream to a variable

Posted 27 March 2011 - 07:54 AM

Please do not post help questions in the C++ Programmers Forum!!!

Moved to C and C++.
Was This Post Helpful? -1
  • +
  • -

#4 dastick  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 18-March 11

Re: How to do assign file output from a stream to a variable

Posted 27 March 2011 - 08:12 AM

thanks
Was This Post Helpful? 0
  • +
  • -

#5 dastick  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 18-March 11

Re: How to do assign file output from a stream to a variable

Posted 28 March 2011 - 12:57 PM

Hi i tried your method but when I use it in this code it doesn't set the value of the file output to the var. the file contains the number 3.

int player::set_stats()
{
	stats pstats;
	
	ifstream playerstats;
	playerstats.open("player stats.txt", ifstream::in);
	playerstats >> pstats.atk;
	playerstats.close();
	return pstats.atk;

}


By the way, ignore the classes they are working fine

This post has been edited by dastick: 28 March 2011 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1