reading a file bytewise

Hi,

newbie in c++: (working some beginners tutorials atm)


trying to read a file header, from which i know its 80 bytes long and each byte is of type char.

how do i start?

thanks alot!

EDIT: after this is done i will try to read more of the file and probably will have to jump to certain positions within the file to extract data of other types (ints, floats, and so on).
Last edited on
Open the file in binary mode and then read the first 80 bytes into an array of 80 chars. See about file streams in this site or some other: http://www.cplusplus.com/reference/iostream/ifstream/ .
meanwhile i have a solution which looks something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
using namespace std;



int main()
{
fstream FileBin("S:\\datei2.dat", ios::in|ios::out|ios::binary); //?
if (FileBin.is_open())
{
    char buffer[80] = {};
    FileBin.read(buffer, 80);
    cout <<"here it is:"<< buffer;
}
else
{
    cout<<"FileBin was not open...";
}

}


however, i am still confused with the fstream part, since the following phenomenon occurs:

for one file i have to write
fstream FileBin("S:\\examplefile2.dat", ios::in|ios::out|ios::binary); //?
for another file i have to write:
fstream FileBin("S:\examplefile.dat", ios::in|ios::out|ios::binary); //?
with only one backslash.

how can i tell which one i have to use? and what causes this behaviour in the first place?

thanks in advance
You must always use 2 backslashes in Windows. Google up escape sequences for C++. Basically, the backslash is the escape sequence initiator. Therefore, if you want an actual backslash in the string, you must use 2.
i swear i have two files in the same folder (directly on S:\) and one is only found with one backslash and the other is only found with 2...

if i move the first one to a subfolder i cannot find it at all anymore no matter if i use one or two backslashes
i made a video of the phenomenon, if you want to look:

http://screencast.com/t/lVxGkL7h
After watching the video, I can only say that your Windows is broken. Or your C++ compiler, or your STL for that compiler. Try other compilers. Try Visual Studio.
"S:\\datei2.dat" -> S:\datei2.dat
"S:\datei2.dat" -> S:datei2.dat

Since \d isn't a valid escape sequence the compiler just omits the backslash.

S:datei2.dat means that 'datei2.dat' is at the current directory of S:. Likely that a file named 'datei2.dat' exsits there.

That you cannot open S:\datei2.dat probably means that you don't have the appropriate rights to do so
@webJose: thanks for taking the time to watch the video, i will try out other compiler and report here
@coder777: i am not exactly sure what you are trying to tell me (excuse me that english is not my mothertongue) did you watch the video?

BTW: my OS is Windows 7 Professional 64bit SP1
@webjose: ok i installed ms vc++ 2010 express and there is a difference. with that i cannot fstream the file datei.dat at all. no matter if i use one or two backslashes. datei2.dat works fine.
the property rights of both files seem identical to me.

is there a way to get a return why an fstream failed? as in "file not found" or "file not pink enough" or something like this?
Use the CreateFileEx() Windows function directly. If the function call fails, then call GetLastError() to obtain the error code. If you want the error message, use FormatMessage() to obtain the error text.

http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
problem solved, coder777 was right, the confusion was initiated because one of the files existed in the project folder too, the other did not and the codeblocks compiler used that file instead of the one directly on S:

microsoft visualc++2010 express does behave different in this case it seems.

thanks for all the help... i'll be back!

if you don't know what 'escape sequence' means this might be helpful: http://de.wikipedia.org/wiki/Escape-Sequenz
Topic archived. No new replies allowed.