question about newlines

This is a discussion on question about newlines within the C++ Programming forums, part of the General Programming Boards category; There are newline characters at the beginning and end of lines, right?...

  1. #1
    Banned
    Join Date
    Jan 2003
    Posts
    1,708

    question about newlines

    There are newline characters at the beginning and end of lines, right?

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    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.

  3. #3
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    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

    {
    ( 0 0 256 )
    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.

    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);
    	}
    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:

    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
    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?
    Thanks

    EDIT: I don't understand why byte number 3 is nothing, hmm this is weird

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    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.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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]

  6. #6
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    As an aside, mixing old type C I/O routines and newer C++ I/O routines is a notorious source of confusion.
    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).

    Also, print the output in hex to see the value of the non-printable characters:
    std::cout <<std::hex << mych <<std::endl;
    That's cool, I didn't know std::hex exists, thanks.

    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:
    If you're after *every* byte in the file, are you opening it in binary mode?
    Yes I'm opening in binary mode
    ifstream fin(fileName, ios::in | ios::binary);

    EDIT1:
    Code:
    std::cout << "byte number: " << i << " is " << std::hex << temp << std::endl;
    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.

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21