There are newline characters at the beginning and end of lines, right?
There are newline characters at the beginning and end of lines, right?
How lines are represented tends to depend on the operating system, but I am not aware of an OS that would put a newline character at the start and the end of a line.
Normally you would see either a carriage return, linefeed or both at the end of a line.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
Hmm, ok, well I'm having a problem then. I'm trying to read something from a file, and in order to traverse through I have to traverse through all newline characters and whitespace characters
That's what is in a file i'm trying to read from, here is the code I am using to loop through and the subsequent output from that.{
( 0 0 256 )
That just makes sure the pointer is already past the first part of the file, that means that the 'cursor' would be positioned before the first bracket that I showed above. The actual output is this:Code:ptr += ( sizeof(byte) * mheaderoffset); for(i = 1; i < 7; i++) { temp = *(char*)ptr; cout << "byte number: " << i << " is " << temp << endl; getch(); ptr += sizeof(byte); }
It would make sense that there is some kind of whitespace or newline character at the beginning of the line, because in order to get from the bracket to the first integer I have to traverse throguh 6 bytes from the file. Does this make sense? Do you have any suggestions as to how I could keep this same setup but actually do something correctly?byte number: 1 is {
byte number: 2 is //nothing, a whitespace or newline character?
byte number: 2 is //again, nothing
byte number: 3 is //nothing
//note: skips to next line in output, important?
byte number: 4 is (
byte number: 5 is //nothing
byte number: 6 is 0 //ahh, finally data
Thanks
EDIT: I don't understand why byte number 3 is nothing, hmm this is weird
The easiest way to find out what is in your source file is to look at it with a hex editor, you will be able to see how the lines are terminated. I don't know what you are using now, but how do you know, for example, that the first line is not "{", " ", i.e. open brace, space...
As an aside, mixing old type C I/O routines and newer C++ I/O routines is a notorious source of confusion.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
If you're after *every* byte in the file, are you opening it in binary mode?
Also, print the output in hex to see the value of the non-printable characters:
std::cout <<std::hex << mych <<std::endl;
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I think I am using an older version of the newer C++ I/O routines, i.e I'm not using the standard but I am using fstream.h. I'll change it soon because it makes more sense to just stick with the standard. It's almost a moot point because everything is read into a byte array (byte is typedef'd as an unsigned char), and then the file stream is closed and then that array is parsed (hence the pointer to the array and stuff).As an aside, mixing old type C I/O routines and newer C++ I/O routines is a notorious source of confusion.
That's cool, I didn't know std::hex exists, thanks.Also, print the output in hex to see the value of the non-printable characters:
std::cout <<std::hex << mych <<std::endl;
I've been reading tutorials, and to be honest I've seen way too many ways to do more or less the same I/O with file streams, i.e the older C style IO fread, fgets, fscanf, and the newer C++ IO ifstream>>, ifstream::read, and there are probably way more that I don't know about. What methods do you two choose to use when reading from files, and why? I'm going to go mess around with this app some more.
Thanks for all the help thusfar
EDIT:
Yes I'm opening in binary modeIf you're after *every* byte in the file, are you opening it in binary mode?
ifstream fin(fileName, ios::in | ios::binary);
EDIT1:
This isn't working, the non-printable characters are still not being printed. I had to change <iostream.h> to <iostream>, and i'll change everything else to the standard soon.Code:std::cout << "byte number: " << i << " is " << std::hex << temp << std::endl;
EDIT2:
I'm sorry I'm being such a hassle but I realized why I decided NOT to use the standard. The ios::read function of the old standard doesn't absolutely require that the buffer being read into that it be a signed char array, but the new standard flags an error when you pass an unsigned char (byte) array pointer to it. I've seen things things done using the old standard for this reason. I guess I'm really in a pickle right now
Last edited by Silvercord; 01-22-2003 at 10:42 AM.