LinuxQuestions.org
Help answer threads with 0 replies.
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 10-10-2005, 05:12 PM   #1
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Rep: Reputation: 15
Converting a steam from an input file to a String


I have some bigass input stream from a file....

string filename;
cout << "Enter the file name to decrypt : ";
cin >> filename;
ifstream input(filename.c_str());

Ill be needing many of the various functions from the string library to help me work through the text, so I'd like to take the stream input and put it into a new string. I can then work more comfortably from there. Is their anyway to do this? I've been having trouble with alot of other ideas, and my knowledge of streams is pretty weak.

But please help if you can, thank you very much.
 
Old 10-10-2005, 06:18 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Use An Output String Stream

I would use an output string stream. That way you can stream data character-by-character from the input file, and into the ostrstream. When you're done, you can "freeze" the ostrstream into a standard string. Here's an example that reads everything from a file, then puts it into an ostrstream, and prints it out as a string:
Code:
#include <fstream>
#include <iostream>
#include <strstream>

int main(int argc, char** argv) {
    std::string filename;
    std::ostrstream* oss = new std::ostrstream();
    std::cout << "FILENAME:";
    std::cin >> filename;
    std::ifstream input(filename.c_str());
    char c;
    while(input.get(c)) {
        *oss << c;
    }
    std::cout << "FILE CONTENTS: " << oss->str();
    return 0;
}
The str() method returns the standard string form of what's in the ostrstream. Calling str() on an ostrstream freezes it. If you want to start putting more data back into oss after you call the str() method on it, you have "thaw" it first, by calling this method:
Code:
oss->freeze(false);
 
Old 10-10-2005, 06:46 PM   #3
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Original Poster
Rep: Reputation: 15
So how do I make a new string and put the stuff in it?
 
Old 10-10-2005, 07:10 PM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
ostrstream Use

You don't have to read data into a string; instead, you can read it into a stream, and then convert that into a string when you're finished getting the data from the file. An ostrstream is a special type of stream that you can write stuff into (just like you would cout) and when you're done, get a string back. Just use the put-to operator (<<) to put stuff into the stream. When you're finished, calling str() on it will return a string. Thus, to get a string object for use later on, use:
Code:
std::string* s = new std::string(oss->str());
If you want to continue to put into an ostrstream after you use str(), you have to call freeze(false) on it first, but everything you had put into it up until that point will still be there.
 
Old 10-10-2005, 07:32 PM   #5
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Original Poster
Rep: Reputation: 15
I see. What is * and why do I need it in my string declaration, and whats ->?
 
Old 10-10-2005, 07:57 PM   #6
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Pointer Fun!

The result of using the "new" operator is a pointer, or (in Java-speak) a reference. It's a memory address where the real data can be found. The star (*) means, "follow the pointer". In other words, using "*oss" means, "go directly to the data represented by oss". It's the only way to get at what was created when you use the "new" operator.

The arrow operator does something similar. Normally you access an object with the dot, like "foo.print()"; but if foo is a pointer, you'd have to do "(*foo).print()". [You need the star inside parenthesis because of operator precedence.] The arrow is a shortcut: you use it instead of a dot, and the object is automatically dereferenced. So, "(*foo).print()" is the same as "foo->print()".

I was just looking into the use of ostrstream a little more in one of my own projects, and realized that it's actually not supposed to be used anymore. Instead, the preferred method uses a "ostringstream" object instead (found in the "sstream" header). You can just substitute that into my code above, in lieu of "ostrstream".

I guess since the default constructor has no parameters, you could just avoid using "new" altogether. Then you wouldn't have access to a pointer, but directly to the object itself. Then you can use just a plain dot, and no star. Expanding my above code, you would get:
Code:
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

int main(int argc, char** argv) {
    std::string filename;
    std::ostringstream oss;
    std::string s;
    std::cout << "FILENAME:";
    std::cin >> filename;
    std::ifstream input(filename.c_str());
    char c;
    while(input.get(c)) {
        oss << c;
    }
    s = oss.str();
    std::cout << "FILE CONTENTS: " << s;
    return 0;
}
Pretty much any book about C or C++ should have more information about pointers, if you're interested. They can be complicated, but they're absolutely vital to do even a little bit of advanced C programming.
 
Old 10-10-2005, 08:08 PM   #7
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Original Poster
Rep: Reputation: 15
Thank you I should be able to use this information.
 
  


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
converting a int to string irfanhab Programming 6 07-30-2005 09:40 PM
converting string to asterisks pantera Programming 2 09-13-2004 01:27 PM
Converting a string from /etc/passwd liguorir Linux - Software 3 04-13-2004 02:32 PM
C# trouble converting from string to double. exodist Programming 2 02-23-2004 11:38 PM
Converting a paragraph into a one-line string ganninu Linux - General 4 12-03-2003 07:50 AM

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

All times are GMT -5. The time now is 03:54 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