Newbie - Reading and Parsing from Text File

D

Default User

Mojo said:

So VC++ should have been compiling that as C code. I'd assume the same
of Turbo C++, but it's been a long time since I used that.





Brian
 
K

Keith Thompson

Mojo said:

<snip>
Thank you very much Keith, I noted every point you made..

You're welcome.
I don't want to mess with the _source.. All i want to do return a
value
without changing the _source variable..

You don't change the value of _source, but you do change the data that
_source points to. You do this regardless of whether the value of
_source is copied to tmp or not.

If you want to return a pointer to a distinct string, without
modifying the string to which _source points at all, you'll need to
allocate the new string somehow.
 
C

CBFalconer

Keith said:
.... snip ...


It would make more sense for n to be a size_t rather than an int.

And, if strlen(str) returns 0, this is an unsigned quantity, and n
becomes a large positive number (roughly 2 exp 31 is normal). The
following "while (n > 0)" test may take some time to complete.
 
K

Keith Thompson

CBFalconer said:
And, if strlen(str) returns 0, this is an unsigned quantity, and n
becomes a large positive number (roughly 2 exp 31 is normal). The
following "while (n > 0)" test may take some time to complete.

Good catch!

Using int is still not the best answer, unless you have reason to be
sure that str will never be longer than INT_MAX or so characters.
Using long would be better. Possibly the best solution would be to
re-work the logic so size_t can be used (but I haven't reexamined the
code to confirm this.)
 
S

SM Ryan

#
# > You can read the entire line into a buffer and do things like count
# > the fields (defining a field as non-isspace characters separated by
# > isspace characters and begining/end of line).
# >
# > You can also read a character at a time, validating and accumulating
# > to per-field buffers, with a finite state machine.
#
# Yes, that is exactly what i want to do..
# But i don't know how to write a function that doesn't change
# the parameter's value, it will just return a value..

There's not too many options.
- Change the input string.
- Allocate a new string and copy into it, freeing when no longer needed.
- Copy to static array and don't overwrite early.
- Don't use C strings, but rather a (firstcharacteraddress,strlength) pair.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top