|
-
Feb 12th, 2006, 02:35 PM
#1
Thread Starter
PowerPoster
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
-
Feb 12th, 2006, 05:48 PM
#2
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.
-
Apr 2nd, 2006, 04:48 PM
#3
Lively Member
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
-
Apr 3rd, 2006, 01:46 AM
#4
Fanatic Member
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
-
Apr 3rd, 2006, 05:31 AM
#5
Lively Member
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
-
Apr 3rd, 2006, 06:10 AM
#6
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|