Solved

Convert Data....???

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
not able to insert into temp table 68 169
How do you programatically show and hide the Windows 10 On-Screen-Keyboard? 3 813
has77  challenge 9 92
Capture logon name 13 88
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

821 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