Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert Data....???

Posted on 2001-07-06
5
Medium Priority
?
217 Views
Last Modified: 2013-11-20
#include <fstream>
//Get data from text file (test.txt);
//Can I use CString to replace text file?
//And how?

ifstream m("test.txt");
CalcLexer lexer(m);

How to convert  data CString ->ifstream
(but I don't want to save CString ->textfile).
Thanks
0
Comment
Question by:pbtdanh
5 Comments
 
LVL 12

Accepted Solution

by:
migel earned 400 total points
ID: 6259020
Hi!
to read data from file:
CString strBuffer;
// get size of the file
m.seekp(0, ios::end);
streampos lpos = m.tellp();
m.seekp(0, ios::beg);
// lpos now is the size of the stream
char* pszBuff = strBuffer.GetBufferSetLength(lpos + 1);
if (pszBuffer)
{
// read it in the buffer
ofs.read(pszBuffer, lpos);
strBuffer.ReleaseBuffer();
}

But if you already have data not in file
you can use stringstream intead ifstream  but iif your function uses ifstreamas parameter not basic_ios or basic_istream then you need rewrite it to use basic stream classes instead particular derived.
0
 
LVL 3

Expert Comment

by:pamboo
ID: 6259023
>>CalcLexer lexer(m);

will  u please explain ur problem elaborately

I think u want to read a file and store datas in a CString

0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 400 total points
ID: 6262544
pbtdanh,

You're using the wrong class to store/write data.
You need to use ostream or ofstream to write/ouput data.
You use istream or ifstream to read/input data.

Example:
#include <fstream>
#include <iostream>
#include <string>

void SomeFunction1(void)
{
std::ofstream m("test.txt");
CString data = "test 1";
m << (LPCTSTR)data;
m.close();
}

void SomeFunction2(void)
{
std::ofstream m("test.txt");
std::string data = "test 2";
m << data;
m.close();
}

If you want to read and write data, you can use fstream.

And as I previously stated, to conver a std::string to CString you can use the following method:
string szdata1 = "hello world";
CString szdata2 = szdata1.c_str();
0
 
LVL 30

Expert Comment

by:Axter
ID: 6262600
If you need to write to the stream, here are some examples:

void SomeFunction3(void)
{
     std::ifstream m("test.txt");
     std::string data;//Read complete line
     std::getline(m,data);
     m.close();
}

void SomeFunction4(void)
{
     std::ifstream m("test.txt");
     std::string data;
     m >> data;//Read until reaches first space
     m.close();
}

void SomeFunction5(void)
{
     std::ifstream m("test.txt");
     std::string data;
     for(;;) //Read everything
    {
          std::string tmp = "";
          std::getline(m,tmp);
          if (m.eof()) break;
          data += tmp + "\n";
    }
     m.close();
}
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9490246
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: Axter, migel (points to be split)

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question