processing a char pointer/string

This is a discussion on processing a char pointer/string within the C Programming forums, part of the General Programming Boards category; Hello.. Im making a code for processing a data that contains \r\n or just \n at the end of each ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    processing a char pointer/string

    Hello..
    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:

    Code:
    char *tmp, *temp, *ptr;
    tmp = new char[1024];
    sprintf(tmp, "line 1\r\nline 2\r\nline 3\n");
    printf(tmp);
    
    temp = tmp;
    while (1) {
    	ptr = temp;
    	temp = strchr(ptr, '\n');
    	
    	if (temp)
    		*(temp++) = 0;
    	else
    		break;
    
    //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
    Posts
    9,796
    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
    Posts
    1,161
    Code:
    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
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by WaltP
    Code:
    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, 01: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, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21