Need help on file reading problem

Hi, I'm not a C++ programmer but I do know simple C++. I'm creating simple console application using Code::Blocks to allow me to pass parameters from other application to replace string within text/registry file before execute the registry merge. Passing parameters to console already success. Now I only have problem with reading file. Example of first line in the registry file is as below.

Windows Registry Editor Version 5.00


However when read into string and output to console using 'cout', it will be show as below with spaces in between.

■W i n d o w s R e g i s t r y E d i t o r V e r s i o n 5 . 0 0


Below is my code, hope that someone can help me solve and tell me why is it like that.

ifstream f("install.reg");
string s((istreambuf_iterator<char>(f)), istreambuf_iterator<char>());
cout << s;
It's a unicode file, you'll need to read into a wide string.
Hi thanks for the advice, I had tried the 2 following code. First code which is failed and the second code is success, but I do have question.

1st code (Failed):
1
2
3
wifstream f("install.reg");
wstring s((istreambuf_iterator<wchar_t>(f)),(istreambuf_iterator<wchar_t>()));
wcout << s;


2nd code (Success):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
wstring ReadOneLine(FILE *File, wstring Line) {
	wchar_t LineOfChars[512];
	fgetws(LineOfChars, 512, File);
	Line.clear();
	Line.append(LineOfChars);
	return Line;
}

int main() {
	FILE* f = _wfopen(L"install.reg", L"r,ccs=UTF-16LE");
	wstring t, r;
	while(!feof(f) && !ferror(f)) {
		r = r + ReadOneLine(f, t);
	}
	fclose(f);
	wcout << r;
	return 0;
}


And my next question is: I had use length of 512 for 'wchar_t' and 'fgetws' which from example code found in this site. May I ask what is the maximum length of these 2 and how will it affect on reading line which may have more than 512?
Hi, below is my simple solution just in case if anyone need it.

1
2
3
4
5
6
7
8
9
10
11
12
13
bool ReadFile(string path, string encoding, string& output) {
    string s = "", e = "r,ccs=" + encoding;
    wstring wp, we, wr;
    wp.assign(path.begin(), path.end()), we.assign(e.begin(), e.end());
    FILE* f = _wfopen(wp.c_str(), we.c_str());
    if (f == NULL) return false;
    while (!feof(f) && !ferror(f)) {
        wchar_t wc[512];
        fgetws(wc, 512, f), wr.append(wc);
    }
    fclose(f), output.assign(wr.begin(),wr.end());
    return true;
}
Just found out another problem. There is additional duplicated last line after read.

Example original last few lines:
1
2
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\myapp]
@="VersionBuild"=dword:00000000

Example loaded last few lines:
1
2
3
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\myapp]
@="VersionBuild"=dword:00000000
@="VersionBuild"=dword:00000000

Can I know what was the problem?
Can I know what was the problem?

You're looping on eof instead of checking to see if the file extraction was successful.

So, on line 9 the call to fgetws fails and you act as if it didn't and append the last input that was extracted again... Check return values.

1
2
3
    wchar_t wc[512] ;
    while ( fgetws(wc, 512, f) )
        wr.append(wc); 
Last edited on
Thanks cire! It works!! so basically 'feof' is only useful after it has been read and it will only return true if it has not been read right? My new code is as below just in case anyone need it.

1
2
3
4
5
6
7
8
9
10
11
bool ReadFile(string path, string encoding, string& output) {
    string s = "", e = "r,ccs=" + encoding;
    wstring wp, we, wr;
    wchar_t wc[512];
    wp.assign(path.begin(), path.end()), we.assign(e.begin(), e.end());
    FILE* f = _wfopen(wp.c_str(), we.c_str());
    if (f == NULL) return false;
    while(fgetws(wc, 512, f)) wr.append(wc);
    fclose(f), output.assign(wr.begin(),wr.end());
    return true;
}
Topic archived. No new replies allowed.