question about readfile() and cout

This is a discussion on question about readfile() and cout within the C++ Programming forums, part of the General Programming Boards category; i have use the function readfile() and put the data into a char buff[256], but i found that there are ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    question about readfile() and cout

    i have use the function readfile() and put the data into a char buff[256],
    but i found that there are values in buff[0] to buff[255] while it is different from what we did normally,
    example:

    char buff[6] ="hello";
    cout << buff;

    for the code above, if you define buff[6] = "hello?", it will have error right? why cant i define buff[6] to "hello?"

    how to explain this? thank you

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    char arrays, when used as strings, need to be nul terminated, meaning there's a trailing \0 character. Your compiler shouldn't let you initialise an array incorrectly, but during execution of the program, it is possible to assign too much data to an array. In this case, the data overruns into the memory beyond the array bounds, and things will go wrong at some point, but no guarantee as to when.

    Also, when you use things like this in your code:
    "hello"
    the compiler will add a trailing \0 for you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    thanks hammer....

    yes you are right!, when i debug the program, i found that buff[5] is equal to 0.

    adn when i check the char array(let say buff2[256]), which are the data got from the com port, i found that buff2[256] is a unknown charater too.

    but why buff2[255] have value also but not for buff[5]? is it a bug for buff2[255] have a value inside?

    or.....i should ask, if the data i put in the char array is from com port, does it mean that it is not a string and no need to be nul terminated?
    Last edited by Jasonymk; 03-09-2003 at 07:28 PM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> does it mean that it is not a string and no need to be nul terminated?
    Possibly not, read up on the function you are using to get the data. The manual will be able to tell you for sure.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    how about cout?

    thank you hammer, another question.

    for example :

    char buff[6] = "hello";
    char buff2[6];

    cout << buff << "\n";;
    cout << buff2;

    for buff2 is the data i got from com1 port,
    and the result is:

    hello
    helloY? // assuming the com1 port receive "helloY"

    what i would like to ask is.......is it because buff have been nul terminated, so what i can see is "hello" only?

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I'm going to assume that it would give you an error because it would be looking for a null-terminated string. That's why most functions which store text in an array you create will also have the size of the array as a parameter, and will truncate the data that it puts into the string if it is longer than the maximum length.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    will there be a problem?

    mmmm i think i know what you mean, but the point is will there be a very serious problem for buff2 having no nul terminated character?

    i am afraid that it will have problem related to the memory resources.....

    will you advice me to put a nul terminated character to the buff2[5] manually??

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    to codec

    mmmm i know what you mean....but when i debug that, buff[6] will be shown as a unknown character,.....what is it then?

    thank you for helping

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Just to be safe, I would initialize all of the slots in the array to null, and then after recieving the data, set the last slot to null again.

    Code:
    char buff2[6] = { '\0' };
    
    //...
    //get data
    //...
    
    buff2[5] = '\0';
    cout<<buff2;
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    sorry all

    sorry for causing the incovenient to all........i should not open a new thread for this.....

    i will post my reply here......
    thank you

    to Stoned_coder:

    what do you think about having no nul terminated character in the readfile buff? is it a very serious problem?

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    look its easy enough to fix. if you want a null terminator there for whatever reason then bloody put 1 there. It dont take a genius to work it out. and its not hard to do. Just make your array 1 char bigger and set its last element to '\0' .
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    what i mean is....

    yeah i can do it but....

    will it cause any memory / resources problem to my computer if i do not add a nul terminated character to the buff?

  13. #13
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    It will cause a problem if you try to write it to a file stream or the standard output stream if there isn't a null terminator on the end.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

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