i know how to read a file into array, however i want to perform some search operations which is difficult to do on array.
if someway i am able to read a file to string then it becomes easier. i have 2 methods of reading a file into wstring but i don't get all the contents of a file. the file reading is stopped upon the occurence of first null character. how can i stop this from happening?
please note that i am reading a file that consits of all characters (ascii 0-255) therefore i must use wstring instead of string..
here is what i have tried so far..

#include <sstream>
#include <fstream>
#include<iostream>
using namespace std;
wstring readfile(const char* filename)
{
    wifstream wif(filename,ios::binary);
    wstringstream wss;
    wss << wif.rdbuf();
    return wss.str();
//is there a way i can perform search operations on rdbuf()?
}

void main()
{
    wstring wstr = readfile("hello.exe");
    wcout<<wstr;//here i get less than 10 characters, while file consists of 200 bytes =(

}

please note that i am reading a file that consits of all characters (ascii 0-255) therefore i must use wstring instead of string..

I doubt that this is true. A normal character (char) is one byte in size, and it can represent 256 characters (many of which don't actually result in any printed character). To read the bytes correctly (and in order) from the file, you should not use the wide-char versions of fstream, stringstream and string.

the file reading is stopped upon the occurence of first null character. how can i stop this from happening?

You read the values using the rdbuf which is a raw reading method. So, I doubt that the actual reading of the file stops at the first null-character (or zero byte). My best guess is that the content of the stringstream get cut-off either during the conversion to a string or during the printing of the string to cout. In any case, you shouldn't use a string to store and manipulate an array of binary data (non-characters). Try to use a vector of chars instead:

#include <sstream>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<char> readfile(const char* filename)
{
    ifstream infile(filename,ios::binary);
    vector<char> result;
    copy(istream_iterator<char>(infile), 
         istream_iterator<char>(), 
         back_inserter(result));
    return result;
}

int main()
{
    vector<char> data = readfile("hello.exe");
    cout << data.size() << endl;
    return 0;
}

BTW, the main function must return an integer (zero if all went well), this is required by the standard.

Comments
very well explained =) thanks

The code you just shared is really helpful, thanks. Please tell me is it possible to edit or print the data contents? if this is possible..
then It would be great if you share with me a source(website/document/article) where i can learn more about vectors. i am not very familiar with them.. so it would be better if i first learn more about them.

This article has been dead for over six months. Start a new discussion instead.