Solved

Convert Data....???

Posted on 2001-07-06
5
209 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 100 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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
A short film showing how OnPage and Connectwise integration works.

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now