[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

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
0
The_Kingpin08
Asked:
The_Kingpin08
1 Solution
 
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 OlivaresCommented:
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now