Thread: Read string until find char

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    62

    Read string until find char

    Given the following string 22E:77

    I need to copy the characters from the string into another array up until it reaches the : character

    I couldn't get this working properly with fgetc. Any help very much appreciated.

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Feel like posting your code? It'll make it a bit easier for us to see what you've done.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    fgetc() is for reading from files, not arrays. Just use a simple loop to step through the original array checking each character. If the character is a ':' then terimate your new string and break the loop. Otherwise, put that character in the new array.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    Something along these lines is what I need...of course this does not work!


    Code:
    {
    	const char *name = "LANGUAGE";
    	int i = 0;
    	char *tmp;
    	tmp = getenv (name);
    	char result[8] = {0x0};
    	
    	while ( (strchr (tmp[i],":") == NULL) && i != sizeof *tmp);
    	{
    			strcat (result, putc (i, *tmp));
    			i++;
    	}
    }

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    this is one way of doing. there are better ways of doing. the following is a sample code which gives u an idea on how to do it. note i havn't compiled this code.

    Code:
    desname = malloc(strlen(srcname);
    
    	while(*(srcname+i) != ':')
    	{
    		desname[i] = srcname[i];
    		i++;
    	}
                    printf("The contents of desname string  : %s",desname);
    ssharish2005
    Last edited by ssharish2005; 06-18-2006 at 09:59 PM.

  6. #6

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ssharish2005
    this is one way of doing. there are better ways of doing. the following is a sample code which gives u an idea on how to do it. note i havn't compiled this code.

    Code:
    desname = malloc(strlen(srcname);
    
    	while(*(srcname+i) != ':')
    	{
    		desname[i] = srcname[i];
    		i++;
    	}
                    printf("The contents of desname string  : %s",desname);
    ssharish2005
    By better ways, you mean ones that actually allocate the right amount of space and will stop correctly? Where's the room for your null character? Oh, and where is your boundry check? You know, for when you don't actually find what you're looking for ... for both questions, actually.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    How about this, wrote it up ages ago for reading froma file though
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    
    int main(void) { 
            char c, *buffer; 
            int n = 0, x, size; 
            FILE *file; 
            file = fopen("a_test_file.txt", "rb"); 
            if(file == NULL) perror("Error opening file!"); 
            else { 
                    while(c != EOF) { 
                            c = fgetc(file); 
                            if(c == ';') { 
                                    x = ftell(file); 
                            } 
                    } 
                    fseek(file, 0, SEEK_END); 
                    size = ftell(file); 
                    rewind(file); 
    
                    buffer = (char*) malloc(size-x); 
                    if(buffer == NULL) perror("Error allocating memory!"); 
    
                    fseek(file, x, SEEK_SET); 
                    fread(buffer, 1, size-x, file); 
                    fclose(file); 
                    printf("File Size: %i\nByte: %i\nBytes Left: %i\nText After: %s\n", size, x, size-x, buffer); 
                    free(buffer); 
            } 
            return 0; 
    }
    That will search inside a file, i wrote this ages ago, you can easily substitue the reading from the file to the reading of a string...

    Hope this helps

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    ftell() returns a long. Why are you casting malloc()? Why are you stuffing EOF into a char and not an int? Why do you declare a variable that you don't use (n)? Why don't you leave room for the NULL character as Quzah indicated?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    Becasue, i used this for a short and simple configuration file for an app i made, so i knew how many bytes it was, what to search for, and such... Plus i just ripped this from it after digging around for it, sorry about that (the n)

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by 3saul
    Given the following string 22E:77

    I need to copy the characters from the string into another array up until it reaches the : character

    I couldn't get this working properly with fgetc. Any help very much appreciated.
    Quote Originally Posted by 3saul
    Something along these lines is what I need...of course this does not work!
    Code:
    {
    	const char *name = "LANGUAGE";
    	int i = 0;
    	char *tmp;
    	tmp = getenv (name);
    	char result[8] = {0x0};
    	
    	while ( (strchr (tmp[i],":") == NULL) && i != sizeof *tmp);
    	{
    			strcat (result, putc (i, *tmp));
    			i++;
    	}
    }
    If you're getting your text from getenv, I might try something using strchr and memcpy.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *copy_var(char *dst, size_t size, const char *key, int delimiter)
    {
       const char *var = getenv(key);
       if ( var )
       {
          const char *match = strchr(var, delimiter);
          printf("var = \"%s\"\n", var); /* DEBUG */
          if ( match )
          {
             size_t length = match - var;
             if ( length >= --size )
             {
                length = size;
             }
             memcpy(dst, var, length);
             dst[length] = '\0';
             return dst;
          }
       }
       return NULL;
    }
    
    char *get_language(char *dst, size_t size)
    {
       return copy_var(dst, size, "LANGUAGE", ':');
    }
    
    int main(void)
    {
       char language[10];
       if ( get_language(language, sizeof language) )
       {
          printf("language = \"%s\"\n", language);
       }
       else
       {
          puts("not found");
       }
       return 0;
    }
    
    /* my output
    var = "English:US"
    language = "English"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM