Thread: Read string until find char

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

  3. #3
    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

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