Thread: getting rid of newline?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    486

    getting rid of newline?

    Code:
    #define LINE_MAX 1024
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    
    char *find_param( char *buffer, int buffersize, const char *param_name, 
    		  FILE *stream )
    {
        char *hash;
        char *value;
        char *equal;
        
        // reset stream to beginning of file (with SEEK_SET + 0 offset)
        fseek( stream, 0, SEEK_SET);
        
        while( ( fgets( buffer, buffersize, stream ) ) != (char *)0 ){
    	
    	// get rid of all comments, preceeded by a # symbol
    	hash = index(buffer,'#');
    	if( hash != NULL ){
    	    *hash = '\0';
    	}
    	
    	// check if we find '=' in current line in buffer
    	// if there isn't one in this line, go to next line
    	equal = index(buffer,'=');
    	if( equal == NULL ) 
    	    continue;
    	
    	// set start of value (pointer arithmetic) in case there is a match
    	value = equal + 1;
    	
    	// cut off string at '=' for string comparison
    	*equal = '\0';
    	
    	// compare cut off string in buffer to param_name sought
    	// if they agree, return the pointer
    	if( strcmp( buffer, param_name) == 0 ) {
    	    return(value);
    	}
    	
       }
        
        return(NULL);
    }
    
    
    char *get_str_param(const char *param_name, FILE *stream )
    {
       char *buffer;
       char *value_pointer;
        char *equal;
        int i;
       // set buffer size for input file lines
       if( ( buffer = (char *) calloc( LINE_MAX, sizeof(char))) == NULL ){
           fprintf(stderr,"error in get_str_param: calloc failed");
           exit(1);
       }
       
       // find pointer to parameter value, (pointer to char array)
       if( ( value_pointer = find_param( buffer, LINE_MAX, param_name, stream ) )
           == NULL ){
           fprintf(stderr,"error in get_str_param: ");
           fprintf(stderr,"Didn't find parameter %s in input file\n", param_name );
           exit(1);
       }
       
       
       free(buffer);
       
       return(value_pointer);
    }
    
    int main()
    {
      char *param;
      char input_file_name[100] = "input_test.txt";
      FILE *input;
      input = fopen(input_file_name,"r");
      if (input == NULL)
      {
        printf("file open failed\n");
        abort();
      }
      param = get_str_param("source",input);
      printf("%s",param);
    }

    The functions above are part of a larger code, I just wrote main here to test a new one, that is supposed to read a string from an input file of format:

    parameter=string

    The code is supposed to go through the file, find parameter, and return the string that comes after the equal sign.


    It works! However, for some reason when it prints out, there is a newline being printed (or possible just lots of whitespace, I can't tell. To get rid of it, I tried:

    Code:
    equal = index(value_pointer,'\n');
       if( equal != NULL )
       {
         equal = '\0';
       }
    in get_str_param just before freeing buffer, but it makes no difference. Could someone point me in the direction of my mysterious newline?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    fgets() stores the newline read, if there is space for it. Use strchr() to find it, and if the newline is found, set it to be a null character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    I added this:

    Code:
       char *temp;
       temp = strchr(value_pointer,'\n');
       if(temp!=NULL)
       {
         temp = '\0';
       }
    to no effect

    Manually setting
    Code:
    //value_pointer[(int) strlen(param)-1]='\0';
    works, though. I am really not familiar with manipulation of strings in general, I must be doing something silly here.

  4. #4
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    >temp = '\0';
    Let me guess. It has had no effect because you forgot to dereference a pointer.

    Also,
    >(int)
    is not necessary.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Yeah, I just figured that out ^_^

    Been a while since I used C

    Thanks

    The cast to int made my compiler stop complaining about giving %d a size_t, but yes, it is not strictly necessary
    Last edited by KBriggs; 05-07-2010 at 11:40 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by KBriggs
    The cast to int made my compiler stop complaining about giving %d a size_t, but yes, it is not strictly necessary
    Use %u or %lu instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    I ended up using

    Code:
       for (i = 0; i < strlen(value_pointer); i++)
       {
         temp = value_pointer+i;
         if(isspace(*temp))
         {
           *temp = '\0';
         }
       }
    to get rid of all the whitespace possibilities - will be fine as long as I keep the input parameters to one word, which they are anyway.

    Thanks everyone

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Save the result of strlen instead of calling it in a loop. Once you have dealt with such a whitespace character, you can break from the loop as there is no point setting null characters further down in the string since the string would have already been truncated.

    Incidentally, you could also use strpbrk as a substitute for strchr, though you would then have to explicitly specify the whitespace characters instead of relying on isspace.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Code:
       length = (int) strlen(value_pointer);
       for (i = 0; i < length; i++)
       {
         temp = value_pointer+i;
         if(isspace(*temp))
         {
           *temp = '\0';
           break;
         }
       }
    I don't think it will add anything significant to the speed of the process since the files are typically no more than 100 lines long with two words per line, but I guess it is good practice ^_^

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest writing it this way:
    Code:
    size_t i, length;
    
    length = strlen(value_pointer);
    for (i = 0; i < length; i++)
    {
        if (isspace(value_pointer[i]))
        {
            value_pointer[i] = '\0';
            break;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Then there is this way:
    Code:
    char *st;
    st = value_pointer;
    while (st){
            if (isspace(st[0]){
                    st[0] = '\0';
                    break;
            }
            st++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newline and null terminator
    By dunsta in forum C Programming
    Replies: 2
    Last Post: 04-22-2010, 07:13 AM
  2. Searching for newline on the end of line text
    By LevelOne in forum C Programming
    Replies: 7
    Last Post: 03-03-2010, 04:36 AM
  3. getting rid of newline
    By AngKar in forum C Programming
    Replies: 24
    Last Post: 04-28-2006, 07:52 PM
  4. How to get rid of newline character
    By C++angel in forum C++ Programming
    Replies: 3
    Last Post: 02-07-2006, 07:50 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM