Need to edge out the competition for your dream job? Train for certifications today.
#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
This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.
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);
CString str = readFile<string>("x.cpp").c_str();
std::cout << str;
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Please enter a first name
Please enter a last name
Must be at least 4 characters long.
Join and Comment
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.