1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Read and search CSV file

Discussion in 'C' started by electroon, May 25, 2013.

  1. electroon

    electroon New Member

    Joined:
    May 25, 2013
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    HI All,

    I was hoping someone could assist with the following coding;

    I am trying to search a very large csv file (approx 200,000 lines) for a array of strings and get the output as the line where there text is found.
    E.g. CSV file:
    123,A,B,C
    456,D,E,F
    123,G,H,I
    Now if I search for 123
    my output array should give me
    A,C
    G,I

    Could anyone please help with the coding for this, I need a pure C code for this? I am concerned with the very large csv I am using which will prob slow everything down.

    Would appreciate any help on this,

    Kind Regards,
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    If each of these is on a single line, then it may not be the most elegant or efficient way of doing things, but have you tried using strstr and a "temp" buffer?

    Code:
    char file_buf[BUFSIZ], *p, out[4];
    int i;
    
    while(fgets(file_buf....) != NULL)
    {
        i = 0;
        memset(out, 0, sizeof(out));
        p = strstr(file_buf, "target");
    
        if(p)
        {
            p += 4; // advance p to A,B,C pattern
    
            out[j] = *p; // set first char
            out[++j] = *(p + 1); // add first ,
            out[++j] = *(p + 4); // skip over middle char and ,
            out[++j] = 0; // add term
    
            puts(out);
        }
    }
    Not really tested, but something like that *may* do or give you an idea of something to try.
     
  3. electroon

    electroon New Member

    Joined:
    May 25, 2013
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thanks sir, what would be better way reading complete file, keeping it in memory and searching or doing line by line search. My concern is over search if you have some sample please share

    Thanks again
     
  4. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    Since you've got to read the input file anyway, I think I'd create a temporary output file for any matches along with a count and then check each line on the fly. If count were not zero, read the temp file back, and then remove() the temp file when finished. With so many lines, it's probably not feasible to create an array or whatever.

    Hope that helps.
     

Share This Page