C++ reading from text file query
Hello there.
I have been following these tutorials: http://www.cplusplus.com/doc/tutorial/tut6-1.html http://cplus.about.com/library/weekly/aa022402a.htm and did a bit of googling, but can't seem to find how to input from a text file and echo exactly what's in it (I can get it to echo without spaces, but that doesn't really help..) here's what I'm using: #include <iostream> #include <fstream> #include <string> using namespace std; #define welcomemsg "Welcome to my Message Keeper!" string tx; int main() { cout << welcomemsg << endl << "Enter the number of the message you wish to read then press enter." << endl; ifstream msgList; msgList.open("msglist.txt"); if (! msgList) { cout << "Error opening input file" << endl; return -1; } while (msgList >> tx) { cout << tx; } return 0; } --- (contents of msglist.txt is "Hello there!" any help would be appreciated, I'm just starting!! |
Hi,
Try something on these lines: Code:
#include <iostream> PS: You said you were starting out. First thing you should learn about is code indentation. |
that works, thanks a lot!
btw, I'm starting up a project for newbies to code this message keeper... where newbies can muck about and learn C++ and get a bit of experience (I'm a real newbie myself, as you can tell) - it's at http://www.geocities.com/latestringtones2003/ |
Oh, if you were actually so serious about that, you might be better off with this:
Code:
char tx[1000]; If you want to never worry about buffer overflows and the like, then (as you had originally posted), use "string"s: Code:
string str; |
Oh, I don't want it exploitable.. I'm doing some research on buffer overflows now..
I'll change the code now, get and getline.. much to learn! thanks for the help, you have a little mention on my site :) |
Ha ha! Very nice. Thanks. Let's see how your project develops.
Arvind |
All times are GMT -5. The time now is 04:02 AM. |