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).
#include <iostream>
#include <fstream>
usingnamespace 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?
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.
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.
@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?
@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.
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.