• C

Best way of returning a substring form a string

Hi all,

I currently have a function named readLine(char* buffer, char* pointValue, int point) with buffer being the entire line value of a file, pointValue being the substring I need to return and point being the value to read. Now I just wanted to know what would be the best way of finding that substring in the buffer ? scanf inside a switch?

buffer= -204.88    -648.67    -560.46
point=2
pointValue= should be -648.67 without spaces

Thanks for your help,

Frank
The_Kingpin08Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulCaswellCommented:
You'd probably be better to use sscanf inside a loop. The loop would count down 'point' while the scanf function 'eats' one number at a time. Use %n to deposit how many characters tha scanf has read so you know where to start each time around the loop.

You may prefer to find spaces in the line instead of using scanf for the numbers.

Please make an attempt at the code and post what you've tried and what goes wrong. We have to be on the lookout for 'Homework'.

Paul
0
Jaime OlivaresSoftware ArchitectCommented:
Is really necessary to read a number at a time, why don't you read all numbers at a time and store values in a numeric variable?

buffer= -204.88    -648.67    -560.46
point=2
pointValue= should be -648.67 without spaces

float a,b,c;
sscanf(buffer, "%f %f %f", &a, &b, &c);
0
sunnycoderCommented:
Hi Frank,

May be strchr if you have fixed number of spaces (say a single space) or strtok if you do not mind the beffer being modified. Both are basically the same approach as Paul highligted "eat one number at a time".

If your line format is fixed, i.e. every field is fixed width and line length is fixed, then you can simply maintain a table to get you the corresponding point value index in the array of chars.

In short, we need to know more about your data ...

Can it be modified ... if yes then strtok may be the easiest to read and cleanest to code way

If no, then are number of spaces fixed? If yes, how much?

Using scanf inside a loop or copying the string should be *avoided* and avoiding them will give you faster code.

cheers
sunnycoder
0
The_Kingpin08Author Commented:
Hi sunnycoder,

the line format isn't fixed because there can be x spaces before reachng the data and x spaces between each number. The returned value format isn't fixed since we can have -2.12 or 14.92. However, there will always be 2 digitts after the '.'

At this point I don't think there would be any problem modifying the buffer, as long as it returns all the values. Any example of a strtok use ?

Alsi I have a struct point declared as

typedef struct point* PtrPoint;
typedef struct point
{
      float x;
      float y;
      float z;
}

could I directly pass the value of PtrPoint to the function or it will be better that the function takes a variable and then assign it to the pointer ?

Thanks for the support,

Frank
0
sunnycoderCommented:
Hi Frank,

>could I directly pass the value of PtrPoint to the function or it will be better that the function takes a variable and then assign
>it to the pointer ?
It is always cleaner if the calling function passes allocated memory to called function. That way called function does not have to bother about freeing up the memory or memory going out of scope. So I recommend the former.

>Any example of a strtok use ?
http://alpha.osnippets.org/?content=snippet&id=318

Also refer to the man page
http://www.die.net/doc/linux/man/man3/strtok.3.html

If you face any problems using strtok, post back.

cheers
sunnycoder
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.