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
-
Note that `strtok` is usually no good for parsing `,`-separated fields, because it treats `,,` as a single separator: it will break if there is an empty field. – Gilles 'SO- stop being evil' Aug 13 '10 at 19:03
-
2It might help to show the code that doing the parsing so people don't have to guess exactly how your handling the various results and corner cases that `strtok()` might be presenting. – Michael Burr Aug 13 '10 at 21:05
4 Answers
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

- 26,321
- 24
- 115
- 201
-
-
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) – Basic Aug 13 '10 at 18:29
-
You may be able to perform slightly simpler validation if your data format is very restrictive - eg if it were
, – Basic Aug 13 '10 at 18:32on a valid line, you could check that it has a comma, and that the values other than the comma were numeric -
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 Aug 13 '10 at 18:42
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);

- 1
- 1

- 3,930
- 19
- 16
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;
}

- 333,147
- 50
- 533
- 760
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
.

- 43,959
- 6
- 69
- 99