How can i read/save from txt?

Jun 25, 2015 at 3:25pm
Greetings cplusplus users!
This is my first question on this site, and please, forgive me if my english is not perfect!!

Actually i'm having some trouble with an exercise i've got from my teacher.

In the exercise i have a couple of strings on a txt file:
"Hello, my name is, Nathan;
It's, a pleasure, to meet you;"

I have to extraxt the 'Nathan' and the 'to meet you' and write them in another txt but it's way too hard for me.
I know how to read the first string, but i don't know what i have to do for select the 'Nathan' and the 'to meet you'.

Would you please suggest me what function do i have to use??

thank you guys ^^
Jun 25, 2015 at 3:41pm
You do need an exact rule. To me it seems that the rule might be:

Find the last comma on the line. Skip over whitespace characters. Store the rest of the line, not including the trailing semi-colon.

std::getline() can read an entire line into a std::string. The string has members find*() and substr().
Last edited on Jun 25, 2015 at 3:41pm
Jun 25, 2015 at 8:44pm
Jun 25, 2015 at 9:42pm
I think you can do the same thing for both lines.

read the line
use string.erase on
"Hello, my name is,

Then erase the last char
;



http://www.cplusplus.com/reference/string/string/erase/
Jun 26, 2015 at 6:20am
Thank you guys for the answers ^^
@keskiverto I was thinking on something like "put every part of the string on an array, then i can print the array[3] because is the unique constant we have over here".
I don't know if you can help me with this "rule" .

@CodeWriter Yeah this seems a quite good function to test!

@SamuelAdams sorry but I don't think this is the best way to face the problem, the teacher might give me a txt more "long" to examinate.

By the way, thank you I really appreciate your help ^^

EDIT1: Do you think there's a way to delete every character before the 2nd ','??
Last edited on Jun 26, 2015 at 7:27am
Jun 26, 2015 at 8:01am
Siberius,

Essentially keskiverto has given you the rule I didn't notice it myself.

The segments you need to "copy out" are between a comma/space and a semicolon.
The example given happens to have two such strings.

Scan until you find a comma followed by a space, then keep scanning and saving the characters in another string until you either find another comma at which point you clear the saved characters or you find a semi colon at which point the string with the saved characters is the extracted string which you can then add to a list of extracted strings.

You have to figure out how to extract a sub string from a given string where the sub string is defined as starting with a comma/space and ending with a semicolon. If a comma is found before the semicolon the string has to be discarded.

You don't actually have to delete anything.



Last edited on Jun 26, 2015 at 8:48am
Jun 26, 2015 at 8:43am
Ok so now it's the big question!
assuming that i have this text:
 
TY,R13,Exec,4.476,25.757,90.000,1 - Mg5-A-H,,2,021011049R9S1,4,0,0,,0,24,8mmTape,,,

Is there a way to set every string into an array?
Something like:
1
2
3
4
example[TY]
example[R12]
example[Exec]
...

I don't really know how to set every string into an array space.
Jun 26, 2015 at 8:52am
Yes you use strtok,

http://www.cplusplus.com/reference/cstring/strtok/

Instead of just printing the strings as in the example given you copy them into a string list.

Last edited on Jun 26, 2015 at 8:58am
Jun 26, 2015 at 9:01am
Again, the description of the syntax is what the code must be based on.

The wild guess is that you have comma separated values. The std::getline() can be told to read up to a separator (which by default is newline). If comma is the separator ...

A fixed-sized array is useful only if you have fixed number of values. If the number can differ depending on the input file, then one is better off with std::vector than array.
Jun 26, 2015 at 9:42am
Yeah the character separation is always the ","
Topic archived. No new replies allowed.