Results 1 to 6 of 6

Thread: Quick string question

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Quick string question

    I need to read/write a bunch of string's to a binary file. Now this obviously requires the use of a char array. I want to avoid having to store the length of the char array and instead just store each char array as a length of 255 and just consider the null termination as the end.

    So my question is, if I have a char[255] and lets say it's null terminator is at [10] then will a function like fopen() read the entire char array or will it stop at [10]?
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Quick string question

    fopen() will open the file.

    fread() will read as many bytes as you specify, and not care at all what's in them. But if there are 255 bytes reserved for the string, and that length guaranteed to be in the file, then you can just read the 255 bytes. The string manipulation functions recognize the NUL.

    Is it worth the space waste, though? Just store the length.

    You shouldn't use any other function except perhaps fgetc() (reads exactly one byte) for binary files.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: Quick string question

    if you have a string stored in chars how do you remove the end line identifier ("/n")

    so if i read a text file and it returned the line "Fred smith/n" how do i remove the /n so the string reads "Fred smith" ?

    ... im new to c++ and cant for the life of me figure it out vb has functions like right left and trim etc

    but c++ doesnt im really stuck
    any help appreciated
    cheers john

  4. #4
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    Re: Quick string question

    Why didn't you just start a new thread?

    Anyway... You could overwrite the \n with the string ending character \0, or you could use strcpy() to copy the part of the string you want to another string assuming you're using "char str[]" type strings.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  5. #5
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: Quick string question

    wicked dude the /0 seems to be an option ill try it out

    ... i did start a new thread but it was much easier to explain what i needed to do in this thread .... i edited my existing thread to another because i hit problems reading values from a text file.

    many thanks for your help ill let you know if it works

  6. #6
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Re: Quick string question

    Halsafar, if the length will only ever be at max, 255, just write, in binary, the lenth as a BYTE at the beginning of the file.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width