Solved

Can anyone show me how to read an entire text fine into a Cstring?

Posted on 2007-11-20
7
508 Views
Last Modified: 2013-11-20
I am trying to read a file that contains xml that is required for a function I am using.  The xml needs to be passed to the function as a CSTRING.

0
Comment
Question by:JonMny
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 20320004
You could e.g.

CString strXml;
CFile file("test.xml",CFile::modeRead);

DWORD dwLen = file.GetLength();

char* pBuf = new char[dwLen + 1];

file.Read(buf,dwLen);

pBuf[dwLen + 1] = '\0';

strXml = pBuf;

delete[] pBuf;

Then, you will have the whole file in strXml.
0
 
LVL 1

Expert Comment

by:Ryalla
ID: 20320116

#include <stdio.h> // contains file i/o declarations

#include <malloc.h> // contains malloc's declaration
 
 

...

FILE* f; // pointer to a file

long fsize; // long that will contain file size
 

f = fopen("myfile.xml", "rb"); // opens the file "./myfile.xml" for reading in binary mode
 

fseek(f , 0 , SEEK_END); // goes to the end of the file

fsize = ftell(f); // tells length of file from start to current position (end)

rewind(f); // goes to the start of the file
 

char* cstr = malloc(sizeof(char) * fsize); // allocates memory for a c string equal to the length of the file
 

fread(cstr, 1, fsize, f); // reads the entire file into cstr
 

fclose(f); // closes file

...

Open in new window

0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 20320167

Rather than using CString I would recommend some thing like this,
This link may be of some help:
http://www.codeproject.com/vcpp/stl/simple_xmlwriter.asp
Best Regards,
DeepuAbrahamK
0
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.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20320395
0
 
LVL 9

Author Closing Comment

by:JonMny
ID: 31410123
Thanks for the fast response!
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 20321015
Converting from std::string is a snip, and the following readFile template function is something that I like to use to load standard library char containers like std::string with file contents.
#include "atlstr.h"

#include <iostream>

#include <fstream>

#include <iterator>

#include <string>

#include <vector>
 

template <typename T>

T readFile(const std::string& filename)

{

	typedef std::istreambuf_iterator<char> Citr;

	std::ifstream fin(filename.c_str(),std::ios::binary | std::ios::in);

	return T(Citr(fin),Citr());

}
 

int main()

{

	using std::string;

	CString str = readFile<string>("x.cpp").c_str();

	std::cout << str;

}

Open in new window

0
 
LVL 17

Expert Comment

by:rstaveley
ID: 20321037
Looks like my code snippet was pre-empted. Here it is the old fashioned way, if anyone cares...

#include "atlstr.h"
#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>

template <typename T>
T readFile(const std::string& filename)
{
      typedef std::istreambuf_iterator<char> Citr;
      std::ifstream fin(filename.c_str(),std::ios::binary | std::ios::in);
      return T(Citr(fin),Citr());
}

int main()
{
      using std::string;
      CString str = readFile<string>("x.cpp").c_str();
      std::cout << str;
}
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: 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…
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
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.

919 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

17 Experts available now in Live!

Get 1:1 Help Now