I have a data file that, if I was using C++, I would use the getline function to read its data into the proper variables. However, the getline functions that C++ allows appear to not be available to me in C. I want to be able to specify a stream, specify a delimiter, and have the function be portable. I've found this getline function from GNU:

http://www.gnu.org/s/libc/manual/html_node/Line-Input.html

but it says it is non-standard (so I assume that means I cannot assume it will work on all compilers), plus it does not allow me to specify a delimiter. Basically I am dealing with a comma-separated-value file:

Bob,Tom,Fred,Sam,Billy,Alexande[B]r[/B]

I want to read from the file and parse the data into an array of strings, and throw away the comma delimiters and the white space:

Bob
Tom
Fred
Sam
Billy
Alexande[B]r[/B]

I see no way to specify a comma as a delimiter in the getline function above, nor do I see a pre-written function in C that will allow me to do this.

The strtok function looks somewhat promising for this job, but the data is in a file, not a char*, so I don't see how to use it here. Any ideas on how to do this in C? Do I need to write my own getline function that takes a delimiter and a FILE* as two of the parameters?

Read the file line by line into a buffer, use strtok_r on each subsequent line. Also be careful strtok function modifies the strings passed to it.

Read the file line by line into a buffer, use strtok_r on each subsequent line. Also be careful strtok function modifies the strings passed to it.

What if the file is one huge line with a million names, no line breaks, and I only want the first, say, 10 names? Do I still have to either read a whole line or write my own getline that can accept a delimiter? And by "read the file line by line", do you mean, read it with the getline I posted in post 1 line by line?

line by line means use fgets() to read a line from file. With fgets you can specify buffer size to read(it stops reading when it finds a newline character). You can also use fscanf family to read from file. Read manuals for this.

I have a data file that, if I was using C++, I would use the getline function to read its data into the proper variables. However, the getline functions that C++ allows appear to not be available to me in C. I want to be able to specify a stream, specify a delimiter, and have the function be portable. I've found this getline function from GNU:

http://www.gnu.org/s/libc/manual/html_node/Line-Input.html

but it says it is non-standard (so I assume that means I cannot assume it will work on all compilers), plus it does not allow me to specify a delimiter. Basically I am dealing with a comma-separated-value file:

Bob,Tom,Fred,Sam,Billy,Alexande[B]r[/B]

I want to read from the file and parse the data into an array of strings, and throw away the comma delimiters and the white space:

Bob
Tom
Fred
Sam
Billy
Alexande[B]r[/B]

I see no way to specify a comma as a delimiter in the getline function above, nor do I see a pre-written function in C that will allow me to do this.

The strtok function looks somewhat promising for this job, but the data is in a file, not a char*, so I don't see how to use it here. Any ideas on how to do this in C? Do I need to write my own getline function that takes a delimiter and a FILE* as two of the parameters?

Maybe this snippet by Dave Sinkula might help you. Or perhaps these others by same author might shade more light to the matter.

OK, thanks. Will give it another shot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.