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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 518
  • Last Modified:

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

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
JonMny
Asked:
JonMny
1 Solution
 
jkrCommented:
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
 
RyallaCommented:

#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
 
Deepu AbrahamR & D Engineering ManagerCommented:

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
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!

 
Jaime OlivaresCommented:
0
 
JonMnyAuthor Commented:
Thanks for the fast response!
0
 
rstaveleyCommented:
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
 
rstaveleyCommented:
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

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now