4 Replies - 1033 Views - Last Post: 09 October 2012 - 10:10 PM Rate Topic: -----

#1 Crowz  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 09-February 12

Reading a char array one line at a time?

Posted 09 October 2012 - 08:24 PM

I have a char array like this

this is\n
what my\n
char array\n
looks like\n


I want to take it one line at a time and do a few string manipulations and if statements on it, then if they all aren't true, move on to the next line.
Suggestions?
Is This A Good Question/Topic? 0
  • +

Replies To: Reading a char array one line at a time?

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Reading a char array one line at a time?

Posted 09 October 2012 - 08:40 PM

take a look at strtok

http://www.cplusplus...cstring/strtok/
Was This Post Helpful? 0
  • +
  • -

#3 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Reading a char array one line at a time?

Posted 09 October 2012 - 08:40 PM

this code is nonsense
do you have a question?
Was This Post Helpful? 0
  • +
  • -

#4 Crowz  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 09-February 12

Re: Reading a char array one line at a time?

Posted 09 October 2012 - 08:55 PM

View Postjjl, on 09 October 2012 - 08:40 PM, said:


If I were to loop through the whole array, doing strtok(array, "\n") each time, how would it break if the pattern was not found?
Excuse my inability to into C
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Reading a char array one line at a time?

Posted 09 October 2012 - 10:10 PM

A null pointer is returned if their are no more tokens in the string.

here is an example of how to use strtok, note that strtok will append a null character to each token found
char test[] = "abc$def$ghi$jkl$";
char *ptr = strtok(test, "$"); //parse at $
while(ptr) { //while token was found
    printf("%s\n", ptr);
    ptr = strtok(NULL, "$");
}


This post has been edited by jjl: 09 October 2012 - 10:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1