Hi everyone,

I'm working with a text file. I want to read a part of it into a string. Here is a sample:

<MEAN method="jackknife">3.97e-06</MEAN>

In the above line i want to put the number 3.97e-06 into a string.

The code that i wrote can find the position of the first digit ( 3 in this case) and the position of the last one in the File. Which means that i have this search function that returns the position of the file pointer. So it returns an int value which is the position of the desired string in the whole file.

Now i don't know how i can read the number using the position of its both ends.

I tried to use getline and substr but they didn't work but i guess they need a different type of the argument .( for example getline needs a pointer to the starting position.)

I can start reading the file line by line and that way i probably be able to simply use getline but i prefer to work with my current search function if it's possible.

Edited by YasaminKh: n/a

6 Years
Discussion Span
Last Post by Kanoisa

Have you tried something like

FILE *h_file;
int  numberStartPos;
int  numberEndPos;
char temp;
string result;

/*loop from start pos to end pos reading bytes (im assuming ascii chars) and appending them to the string result*/

for(int x = numberStartPos; x<numberEndPos; x++)
    fseek(h_file, x ,SEEK_SET); //seek to the next needed byte
    fread(temp, sizeof(temp), 1, h_file ); //reads one char(byte) of data from file
    result += temp//appends character

you might want to use the c++ version of files using streams im sure the two are very similar but i only know the normal c way unfortunately.

hopefully this helps :)

Edited by Kanoisa: made an oopsie in fseek


Thanks for your response. That was really helpful. But there is a problem. fread needs a pointer for first argument. What can I use instead of temp?


sorry try using &temp to pass the address of temp, the code i took that from i had temp as an array so it was an address already. &temp should do the job

also i missed a semi colon as shown below ,..sorry

result += temp; //appends character <-- added semicolon now :)

let me know if this works for you, i hope it does


Thanks a million! it did solve the problem i was challenging with whole day.
I really appreciate your help. :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.