473,883 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how can i convert a file stream in a string?

Hi, I need to convert a text file in a string or char*. exists some
function that does it? or how can i do it?

Thanks,
bye

Sep 11 '06 #1
7 23022

Manuel wrote:
Hi, I need to convert a text file in a string or char*. exists some
function that does it? or how can i do it?
I don't understand the question.

Sep 11 '06 #2
Manuel wrote:
Hi, I need to convert a text file in a string or char*. exists some
function that does it? or how can i do it?
To initialize a string from a file, you can do:

#include <fstream>
#include <iostream>
#include <string>
#include <iterator>

int main ( void ) {
std::ifstream in_file ( "data.txt" );
std::string the_str ( std::istreambuf _iterator<char> ( in_file ),
(std::istreambu f_iterator<char >()) );
std::cout << the_str;
}
Best

Kai-Uwe Bux
Sep 12 '06 #3
Manuel wrote:
Hi, I need to convert a text file in a string or char*. exists some
function that does it? or how can i do it?
If you're talking about copying the contents of a istream into a string,
then one way to do it is to use an ostringstream. You can insert the
streambuf pointer from the istream into the ostringstream and then get
the string from that. For example:

#include <fstream>
#include <string>
#include <sstream>

int main()
{
std::ifstream in("textfile.tx t");
std::ostringstr eam out;

out << in.rdbuf();

std::string s = out.str();

return 0;
}

Hope this helps,
Nate
Sep 12 '06 #4
Kai-Uwe Bux wrote:
>
To initialize a string from a file, you can do:

[ snip std::istreambuf _iterator explanation ]
This of course is better than my solution. Thanks for reminding me
about this.

Nate
Sep 12 '06 #5
First, thank you very much by your answers.

I decide to use the solution of Nate because it is more simple and
clearly and it make exactly as i need it.

But, why is kai-uwe's solution better than Nate's solution?

best regard,
Manuel

Sep 12 '06 #6
Manuel wrote:
First, thank you very much by your answers.

I decide to use the solution of Nate because it is more simple and
clearly and it make exactly as i need it.

But, why is kai-uwe's solution better than Nate's solution?
Because it achieves the same without using an additional ostringstream
object. It's also a question of style. Some people just love using
iterators for everything. IMHO use whatever is clearer to you.

Regards,
Bart.

Sep 12 '06 #7
Bart wrote:
Manuel wrote:
First, thank you very much by your answers.

I decide to use the solution of Nate because it is more simple and
clearly and it make exactly as i need it.

But, why is kai-uwe's solution better than Nate's solution?

Because it achieves the same without using an additional ostringstream
object. It's also a question of style. Some people just love using
iterators for everything. IMHO use whatever is clearer to you.
Interestingly, it didn't compile under VC++ 6. Looks like the iterator
constructor that takes an istream isn't implemented.


Brian
Sep 12 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
52701
by: jk | last post by:
I'm having trouble converting a datatable into xml, with resonse.write to aspx. I'm basically converting vb code that saved a recordset into a stream into c#, but the format is wrong. I've tried using streams, datadoc, xmlreader, etc with no success. I need to convert it directly to a string or via a stream, but not in a file. Here's some sample code that gives me the bad format. I'll post the good format below that give me rowsets rather...
7
7130
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done without strtol or s/printf function. Thanks, whatluo.
4
79670
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
4
3311
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any errors. After reading the ole object from db, I saved it to C: as file1.bmp and displayed on the web. But it can not be displayed. After I manually sent the file to wordpad, it shows
6
4006
by: hb | last post by:
Hi, Would you please give me some idea to convert/decode a Base 64 encoded GIF image string to a *.gif file in ASP.Net? Thank you hb
1
5423
by: Daniel | last post by:
I have looked everywhere on the web for an answer to this and the only thing I can find is converting the image format when the file is present on the local filesystem. What I want to do is use a web form to upload a TIFF image (scanned images) and convert it to a JPEG before I insert it into SQL Server. The file upload part works great, but I can;t convert the image. I certainly don't want to upload it the the server filesystem, convert...
10
8128
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
3
7997
by: Ursula | last post by:
Is it possible to convert a string in a file. The problem is this: I have an object string that is a file xml and I want to pass to Deserialize function, but Deserialize function expect an object of FileStream type and the mode access. There is a function Convert.ChangeType(object value, Type conversionType), but I cant write for example: Convert.ChangeType(mystring, FileStream); CAN YOU HELP ME????? thanks
3
9299
by: tparks69 | last post by:
I am getting data from an FTP into a Stream object. Now I need to put this data into a string so I can manipulate it. Can anyone show me some example code?
2
6416
by: Robert Dufour | last post by:
I got a stream from a file, could be any document type like doc or txt. I want to convert it to a string variable so that I can replace some custom tag contents in it's text with text values generated from a database. Then I need to save the stream as another document file. so far I got Dim Fname As String = "C:\Myfile.doc"
0
9777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11109
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10833
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10405
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7957
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5782
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.