Monday, May 24, 2010

C++ question; how do you get your program to read inputs from a txt file?

I know that it can be done in the Linux terminal, and using the Cygwin environment for WinXP. All the text file contains are numbers (type double) separated by spaces.





Problem is that Cygwin doesnt work too well on my system.


How do I make it happen on BloodShed Dev-C++ ?





I am a first year Engg. student with no prior programming experience, so I would appreciate if you answered in plain dumbass. Thanks in advance.

C++ question; how do you get your program to read inputs from a txt file?
I have no idea what role Linux or Cygwin plays in your question. You can open and read from a text file in a portable way (i.e. it doesn't matter if you are on Windows, Mac, Linux, BSD, etc.).





I'm not sure where to start explaining. So instead, I'll point you to tutorials, as I have a sinking feeling you haven't done much reading.





http://www.cprogramming.com/tutorial.htm...





Just go through the C++ tutorials please. If you don't understand a specific portion, ask a specific question. Rather than "how do I do file input".





(The answer is open up the file with an fstream. Then acquire each line in one of several ways. Either cin each token on the line, or use getline and break down the line by spaces).
Reply:You can use the ifstream class found in fstream.h.





ifstream input("file.txt");





you can now use input just like you use cin, except you'll be reading a file instead of keyboard input.

flowering plum

No comments:

Post a Comment