LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ reading from text file query (https://www.linuxquestions.org/questions/programming-9/c-reading-from-text-file-query-181312/)

lrt2003 05-14-2004 02:05 AM

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!!

arvind_sv 05-14-2004 05:29 AM

Hi,

Try something on these lines:

Code:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream msgList;

    msgList.open("msglist.txt");
    if (!msgList)
    {
        return (1);
    }

    char tx[1000];
    while (msgList.getline (tx, 1000, EOF)) {
        cout << tx;
    }

    return 0;
}

Arvind

PS: You said you were starting out. First thing you should learn about is code indentation.

lrt2003 05-14-2004 08:32 AM

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/

arvind_sv 05-14-2004 11:29 AM

Oh, if you were actually so serious about that, you might be better off with this:

Code:

    char tx[1000];
    while (msgList) {
        msgList.get (tx, 1000, EOF);
        cout << tx;
    }

Try the code I sent previously with size set to something like 10. Does it work?

If you want to never worry about buffer overflows and the like, then (as you had originally posted), use "string"s:

Code:

    string str;
    while (1) {
        getline (msgList, str);
        cout << str;
        // Print a newline only if it's not the last line.
        if (msgList) {
            cout << endl;
        }
        else {
            break;
        }
    }

Arvind

lrt2003 05-14-2004 08:23 PM

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 :)

arvind_sv 05-15-2004 04:25 AM

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.