| | |
write() function...
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
good day everyone.. thx for reading..
this is my problem:
i want to use the write() function, in ofstream, to write strings into a file.. but write() requires parameters of const char pointer and streamsize...
do i really need to convert string to char first? any simpler way?
and,, any better idea than using write()??
thx..
this is my problem:
i want to use the write() function, in ofstream, to write strings into a file.. but write() requires parameters of const char pointer and streamsize...
for(unsigned i=0; i != questions.size(); i++)
{
qptr = &questions[i]; //vector element(string)
tempfile.write(qptr, stringsize);
}do i really need to convert string to char first? any simpler way?
and,, any better idea than using write()??
thx..
I am living in a mere program...
If you're talking about std::strings you could use the c_str() method?
(I'm assuming "questions" is a vector of std::strings)
But if tempfile is an ofstream, why not use the << operator?
(I'm assuming "questions" is a vector of std::strings)
C++ Syntax (Toggle Plain Text)
tempfile.write(questions[i].c_str(), questions[i].size());
But if tempfile is an ofstream, why not use the << operator?
C++ Syntax (Toggle Plain Text)
ofstream tempfile("yourfilename_goes_here"); std::string foo = "bar"; tempfile << foo;
•
•
•
•
If you're talking about std::strings you could use the c_str() method?
(I'm assuming "questions" is a vector of std::strings)
C++ Syntax (Toggle Plain Text)
tempfile.write(questions[i].c_str(), questions[i].size());
But if tempfile is an ofstream, why not use the << operator?
C++ Syntax (Toggle Plain Text)
ofstream tempfile("yourfilename_goes_here"); std::string foo = "bar"; tempfile << foo;
my strings in the vectors have white spaces.. i think << stops on white spaces,, correct me if i'm wrong,, so can't use that one..
thx for help..
any other ideas?? i'll try to use write() for now,, converting my strings to char.. but any better ideas will help..
thx
I am living in a mere program...
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <string> int main(int argc, char *argv[]){ std::string example = "Hello, world!\nHow are you?\n\tSuper thanks!"; std::ofstream exfile("test.txt"); if(!exfile.good()) return 0; exfile << example; exfile.close(); return 0; }
C++ Syntax (Toggle Plain Text)
Hello, world! How are you? Super thanks!
Last edited by Freaky_Chris; Jan 28th, 2009 at 5:51 am.
Knowledge is power -- But experience is everything
![]() |
Similar Threads
- calculatecharges function (C++)
- help to write a function (C)
- Write to hex, is it possible? (C)
- How To Write A Function (C++)
- JAVA Expert Help me.. Write a function that accepts an array of integers and returns (Java)
- Function[Array] in combination with cin>> (C++)
- Function that returns void (C++)
Other Threads in the C++ Forum
- Previous Thread: Unordered Hashmap implementation
- Next Thread: Sorting files on creation date
Views: 2092 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






