length of a string

Started by
9 comments, last by hello_there 20 years, 6 months ago
i have a string in a file and i want to know how long it is how would i find it''s length. in c++;
____________________________________________________________How could hell be worse?
Advertisement
If it''s the string class then I assume there is a length function. If it''s an array just loop through it and count until you find the null terminator.
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...
In a C++ string class it's the size() function.

EDIT: Argh, missed the "in a file" part. Sorry.

[edited by - DaTroof on October 5, 2003 12:06:39 AM]
Post Extant Graphical MUD
string in a file?

maybe you''re looking for fstream, string, and getline
Yeah, getline with a ''\0'' terminator ought to work.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
but there might be other stuff on the line that i don''t want to load.
____________________________________________________________How could hell be worse?
i don''t understand the question,you want the size of a string in a file, but before reading it?

quote:Original post by hello_there
but there might be other stuff on the line that i don't want to load.


Have you even read the documentation for that function ?
You obviously don't understand what it does.
Particularly what a 'line' is in this context.

If your string is null-terminated, getline, with a terminator of '\0' will read it. If not, you have to figure out where you want the string to end by yourself : space, tab, newline, comma, whatever.

The computer cannot "guess" when to stop reading if you don't tell it the length (surprise, surprise), or a character that marks the end.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on October 6, 2003 1:35:09 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
oh wait never mind i though of a way to do it. also i now how it works i was meaning if the string started in the middle of the line. is a char always 1 byte on any computer?
____________________________________________________________How could hell be worse?
A string is not necessarily only an array of chars.

Niko Suni

This topic is closed to new replies.

Advertisement