read file start from different positions

This is a discussion on read file start from different positions within the C Programming forums, part of the General Programming Boards category; Hi all, I have a file which contains some specials chars to gather one for each line. For example if ...

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

    read file start from different positions

    Hi all,

    I have a file which contains some specials chars to gather one for each line. For example if the special char is $ (dollar) the file may looks like:

    Code:
    hello world $ hello 
    $ hello world
    hello world hello world $
    I have to gather the $ and ignore the rest. I found a solution but not efficient. In fact once I found the $ is there a way to jump directly to the next row without keep reading ?

    The second row in the example contains the $ at the beginning and it is useless to keep reading after the first char.

    Is there any other solution?
    Thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Within a text file, you can only fseek() to the start of a line if you've already read the file at least once, and recorded all the ftell() positions.

    What is your "inefficient" method?
    I would have thought fgets() + strchr() would do the job just fine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    56
    Hi,

    the method I use is a :
    Code:
     while((c=getc(stream)) != '\n'){
    so I read always the entire row

    thanks for reply

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Dedalus View Post
    Hi all,

    I have a file which contains some specials chars to gather one for each line. For example if the special char is $ (dollar) the file may looks like:

    Code:
    hello world $ hello 
    $ hello world
    hello world hello world $
    To what end? So you end up with 3 $ signs... what good is that, except to count them...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
     { int count;
        int fs;
        char *fb;
        char *bp;
        FILE *fp;
     
       fp = fopen("filename.txt")
        if (!fp)
         { printf("Fopen Error\n"); 
            exit (1); }
        
        fseek(fp,0,SEEK_END);
        fs = ftell(fp)
        fb = calloc(fs, sizeof(char));
        fseek(fp,0,SEEK_SET);
        if (!fb)
         { printf("Calloc error\n");
            exit (1); }
    
       fs -= fread(fb,sizeof(vhar),fs,fp); 
       if (fs)
        { fprintf("Fread Error\n");
           exit (1); } 
       fclose(fp);
    
       bp = fb;
       while (bp = strchr(bp,'$'))
          count++;
    
       free(fb);
       printf("I found %d\n", count);
       return 0; }
    That should be pretty close.
    Last edited by CommonTater; 08-21-2011 at 08:13 AM.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    56
    Hello

    thanks for sharing your solution.
    I use it to parse some text files, usually I use bash but wanted to try C too.

    Cheers

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Dedalus View Post
    I have to gather the $ and ignore the rest. I found a solution but not efficient. In fact once I found the $ is there a way to jump directly to the next row without keep reading ?
    Underlying assumption is that there's only one $ per line?
    What if there were more than one $ per line; then what?
    Quote Originally Posted by Dedalus View Post
    The second row in the example contains the $ at the beginning and it is useless to keep reading after the first char.

    Is there any other solution?
    Depends if you can predict where the next $ would be

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by Salem View Post
    I would have thought fgets() + strchr() would do the job just fine.
    Quote Originally Posted by Dedalus View Post
    Hi,

    the method I use is a :
    Code:
     while((c=getc(stream)) != '\n'){
    so I read always the entire row

    thanks for reply
    The easiest and quickest method is what Salem suggested. Just use fgets to grab each line of the file and dump it into a buffer then use strchr on the buffer to get all the occurences of your token. This is just about as sleak as you are going to get. An example of strchr() can be found here.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    The easiest and quickest method is what Salem suggested. Just use fgets to grab each line of the file and dump it into a buffer then use strchr on the buffer to get all the occurences of your token. This is just about as sleak as you are going to get. An example of strchr() can be found here.
    On the thought that $ is a comment indicator like C's // ... he can do fgets() to dump it into a buffer then use strchr() to find his symbol and replace it with a \0 to truncate the string. That's pretty efficient parsing...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 01-25-2011, 04:59 AM
  2. Read a line and go back to start
    By rocketman03 in forum C++ Programming
    Replies: 5
    Last Post: 02-11-2010, 02:24 AM
  3. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  4. Start a file
    By cyberbjorn in forum C Programming
    Replies: 2
    Last Post: 05-16-2004, 01:01 PM
  5. system(); (ps read first then start bout spawn)
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-30-2002, 08:26 PM

Tags for this Thread


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