LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-14-2004, 02:05 AM   #1
lrt2003
Member
 
Registered: Mar 2004
Distribution: Fedora 10
Posts: 182

Rep: Reputation: 30
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!!
 
Old 05-14-2004, 05:29 AM   #2
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
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.
 
Old 05-14-2004, 08:32 AM   #3
lrt2003
Member
 
Registered: Mar 2004
Distribution: Fedora 10
Posts: 182

Original Poster
Rep: Reputation: 30
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/
 
Old 05-14-2004, 11:29 AM   #4
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
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
 
Old 05-14-2004, 08:23 PM   #5
lrt2003
Member
 
Registered: Mar 2004
Distribution: Fedora 10
Posts: 182

Original Poster
Rep: Reputation: 30
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

Last edited by lrt2003; 05-14-2004 at 08:26 PM.
 
Old 05-15-2004, 04:25 AM   #6
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
Ha ha! Very nice. Thanks. Let's see how your project develops.

Arvind
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
reading and printing out a text file in html mrobertson Programming 1 07-08-2005 11:47 PM
Reading text from a file using shell scripting. mrobertson Programming 11 06-29-2005 12:12 PM
reading text from a file mrobertson Programming 16 06-28-2005 12:39 PM
reading a text file and outputting to another. Hardw1re Programming 28 11-03-2003 08:51 AM
reading a single line of a text file davi_cabral Linux - Software 1 10-29-2003 12:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration