strtok and newline character

May 2, 2012 at 12:42am
I have a problem for 2 days now I haven't been able to exactly figure out. I have a txt file that his a list of stuff divided by semicolons (; character) and at the end it has a newline character for the next list, just like so:

...other stuff...;S234;S654;P768;P98
...other stuff...;P4;P654;S123;S432
...other stuff...;P90;S673;S879

As you can see it is random how many items we get in each line and what I want to do is to put each item in a linked list according to its line in an array. It has to look like so:

array[0] -> ...other stuff... S234 -points to-> S654 -points to-> P768 etc.
array[1] -> ...other stuff... P4 -points to-> P654 -points to-> S123 etc.
array[2] -> ......

I've made the fgets loop and all works it reads each line etc, but I'm stuck to the internal loop required in order to read each item separately inside the line given by fgets. Since it is random how many items we have in each line I can't put the strtok a predetermined amount of times inside the fgets loop and I still can't figure out what to do in order to condition the internal loop to understand that it has to exit after it finds the newline character (since this is our queue to go to the next line). Let me remind you that through strtok you can cut the whole string off according to the tokens but that does not mean that the last characters of the string array are the same as what you see after a printf("%s"). There are many junk characters thus I cannot check if the last char is a newline character...
May 2, 2012 at 1:13am
1
2
3
4
5
6
7
8
#include <iostream>

int main(){
    
    getline(<#char **#>, <#size_t *#>, <#FILE *#>);

    getline(filename, your string, ';')
}

basic_istream& getline (char_type* s, streamsize n, char_type delim );

delim
The delimiting character. The operation of extracting successive characters is stopped when this character is read. This parameter is optional, if not specified the function considers '\n' (a newline character) to be the delimiting character.
May 2, 2012 at 11:18am
Yeah sorry forgot to mention this is made using C not C++ :S
May 2, 2012 at 8:14pm
wrong forum for u then.
May 2, 2012 at 8:33pm
Yeah well sure but this is where most of the experts dwell :S
May 3, 2012 at 8:48pm
Yeah well sure but this is where most people come for c++, not c. so you will most likely get c++ answers, like mine.
Topic archived. No new replies allowed.