Thread: converting char array to string.

  1. #1
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54

    converting char array to string.

    Hello everybody!
    I have a query regarding the code below. Here within while loop, there is a line buffer[chars_read-1] = '\0'. Now I understand that this is being done in order to make char array buffer a string. My question is that why do we use chars_read - 1 ? won't this overwrite the last character in the array?

    Code:
            if(fp_read != NULL) {
                    chars_read = fread(buffer, sizeof(char), sizeof(buffer), fp_read);
                    while(chars_read > 0) {
                            buffer[chars_read - 1] = '\0';
                            printf("Output:-\n%s\n", buffer);
                            chars_read = fread(buffer, sizeof(char), sizeof(buffer), fp_read);
                    }
            }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, yes.
    Let's say the size of the buffer is 1000 bytes.
    So you do chars_read = fread(buffer, 1, 1000, fp_read) yes?
    So then chars_read = 1000 (since it reads 1000 bytes).
    But index 1000 doesn't exist in the array, only 999 does.
    So chars_read - 1 is the last index.
    But the actual error lies in that you should read sizeof(buffer) - 1, so you could safely append a 0 to the end of the array without overwriting. But then again, why use fread in the first place if you want to read a string?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by jas_atwal View Post
    won't this overwrite the last character in the array?
    No it doesn't. Array indexes are 0-based.
    Kurt

  4. #4
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Quote Originally Posted by ZuK View Post
    No it doesn't. Array indexes are 0-based.
    Kurt
    I agree that Array indexes are 0-based. So that means buffer[chars_read - 1] is the last character and it will be overwritten. Please correct me if I understand wrong.

    Thank you!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It does overwrite the last one since sizeof(buffer) is the full buffer size and that means sizeof(buffer) - 1 is the last index and so if fread reads sizeof(buffer) items, that means chars_read - 1 is the last index.
    But you also fill the whole buffer, the last element also contains data which is overwritten.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by jas_atwal View Post
    I agree that Array indexes are 0-based. So that means buffer[chars_read - 1] is the last character and it will be overwritten. Please correct me if I understand wrong.
    You've just corrected me. Sorry.
    Kurt

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It's easiest to look at the simplest example.
    take chars_read being 1 for example.
    We then enter the while loop and set buffer[0] = '\0'
    Uh oh, it's bye bye to that character we just read!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Are you just reading text into the buffer, or does it hold binary data?
    If it holds binary data, I don't see much point in treating it as a string and terminating it with a NUL char?
    If it holds text, then either read sizeof(buffer) - 1 bytes, or make the buffer 1 char bigger than you plan to read.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or use fgets instead, assuming it's all NUL-terminated text.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Thank you so much for your replies. Your answers have cleared my confusion. Have a great new year all of you!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting string to integer, for further use
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 07-01-2005, 03:12 AM
  2. error converting string to char array
    By COBOL2C++ in forum C++ Programming
    Replies: 6
    Last Post: 07-11-2003, 10:59 AM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. Character arrays
    By PsychoBrat in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2002, 12:02 PM
  5. Converting const char to char array
    By HomerJ in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2002, 03:21 PM