tags:

views:

51

answers:

4

hello i am progremming in c with linux enviroment and facing a difficulty with blank rows while reading from afile. i am using strtok function for seperating the string with delimiter "," and getting segmentation error whenever the file that i am reading from contains blank lines thanks in advance

+1  A: 

You seem to be getting the error because you're passing an invalid parameter to strtok - Try checking that the line isn't empty before passing it to strtok.

A more robust solution would be to check that the line read from the file complies with your data format before parsing it - eg with a regex

Basiclife
i am checking if the line equals to " " but it doesnt seem to work
Nadav Stern
You can't check it exactly - you'll miss too many possibilities. What if it's not a single space but 2 or 3? at a minimum you need to check the length>0 but more properly you should validate it as closely as possible - with a regex (see http://www.gnu.org/s/libc/manual/html_node/Regular-Expressions.html for the GNU C regex lib)
Basiclife
You may be able to perform slightly simpler validation if your data format is very restrictive - eg if it were <integer>,<integer> on a valid line, you could check that it has a comma, and that the values other than the comma were numeric
Basiclife
i know the format that the lines should get the thing is that because i am reading from a file all of the lines are represented as a string, i cant really understand your solution thx anyway if u can be alittle bit more spesific i would gledly appriciate it
Nadav Stern
A: 

I think, regex is to big for this little homework; you have only 2 parts to do:

  • File/String I/O: e.g. take "size_t getline(char *s,size_t lim)" from here: link text
  • data processing: ',' separated columns e.g. here link text

and now its short and easy, or?

char line[100];
FILE *f=fopen(...,"rt");
if(!f) ...
while( fgets(line,sizeof line,f) )
  if( getline(line,sizeof line) ) 
  { /* no empty lines here */
    char *p,*t;
    for(puts("new line"),t=mystrtok(&p,line,',');t;t=mystrtok(&p,0,','))
      puts(*t?t:"empty column"); /* also empty columns here */
  }
fclose(f);
A: 

Here's a small program that uses strtok() to parse lines with comma separated values. It may help you see what's going on (such as the problem with empty fields that Gilles brought up). It'll also help you see what happens with blank lines.

Compile it and feed it example data, either with the keyboard or by redirecting a data file to it:

#include <stdio.h>
#include <string.h>

char* myGetLine( char* buf, size_t bufSize, FILE* strm)
{
    char* result = fgets( buf, bufSize, strm);

    int len = result ? strlen(result) : 0;

    if (len && result[len - 1] == '\n') {
        // get rid of the pesky newline
        result[len - 1] = '\0';
    }

    return result;
}

int main(void)
{
    char line[80];

    while (myGetLine( line, sizeof(line), stdin)) {
        int i = 0;
        char* token = NULL;

        printf( "%s\n", line);

        token = strtok( line, ",");
        while (token != NULL) {
            printf( "token %d: \"%s\"\n", i, token);
            ++i;
            token = strtok( NULL, ",");
        }

        printf( "%s\n", "enter a new line (or EOF)");
    }

    return 0;
}
Michael Burr
A: 

My suggestion is to write a line stripping function that removes any whitespace from the beginning and end of a line. Run every line through this function before processing it. Now, any blank lines should just end up being a single '\0' character.

Line stripping functions are easy to write, all you need is a pair of pointers and isspace() from ctype.h.

bta