Thread: processing a char pointer/string

  1. #1
    Registered User
    Join Date
    May 2006

    processing a char pointer/string

    Im making a code for processing a data that contains \r\n or just \n at the end of each line. Heres what I have written so far:

    char *tmp, *temp, *ptr;
    tmp = new char[1024];
    sprintf(tmp, "line 1\r\nline 2\r\nline 3\n");
    temp = tmp;
    while (1) {
    	ptr = temp;
    	temp = strchr(ptr, '\n');
    	if (temp)
    		*(temp++) = 0;
    //something missing
    	printf("line: %s\n", ptr);
    this will print:

    line 1\r
    line 2\r
    line 3

    Now the problem is I dont know how to check if the last character/letter in the line is \r set it the *pointer to 0.

    What would be the best way to do this?

    Thanks a lot for help

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    It sounds like you really want strtok instead of whatever ad hoc thing you're doing now.
    My best code is written with the delete key.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    l = strlen(ptr) - 1;
    if (ptr[l] == '\r') ptr[l] = '\0';
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    May 2006
    Quote Originally Posted by WaltP
    l = strlen(ptr) - 1;
    if (ptr[l] == '\r') ptr[l] = '\0';

    Thank you, thats what I needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  5. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM