• Introducing Dark Mode! Switch by clicking on the lightbulb icon next to Search or by clicking on Default style at the bottom left of the page!

Read One Line From Flat File


Legendary Poster
Hi List/Forum,

I would like to read one line at a time from a flat file. I created a C++ BSFN using B34A1010 BSFN as a sample.

I have just one unresolved problem:
How to read one line at a time with "fscanf" standard C function.
The input line can contain space and tab characters and "fscanf" always read up to the first space, tab or newline and not only up to the new line only.
Here is my basic line:
fscanf(fpFile, "%s", lpDS->szRecord);

Can I accompish this task with fscanf somehow or do I have to create a while cycle reading the file char by char up to new-line and concatenate the characters in the output parameter?

Please help!
Any solution, sample code, advise will be highly appreciated!


PS1: Once upon a time (many many years ago) I have written a bunch of C codes under very old version of C but never used standard C functions because we have written our own to be able to be independent (mainly for critical error handling). Do not ask how many years ago!

PS2: I could be back to the Forum earlier on 24 or 25 in October.

B7332 SP11, ESU 4116422, Intel NT4, SQL 7 SP1
(working with B7321, B7331, XE too)

Christian Audet

Staff member

Ah ! 'C' language that's nice I like that !

Ok, fscanf will not be able to handle this!

The loop to read one character at a time is also a nice idea but if you don't realy need to process each character then it's not mandatory.

Try fgets instead, it will read a string up to a cariage return WITHOUT stoping for TAB or space character.

look at this TEST function that read the first three line from my test file. I have tried it under OneWorld and it's working fine.

-- Begin Code ------------------------
* Declare pointers
FILE *flatfile = NULL;
char text[501];

* Main Processing
//open file in read mode
flatfile = fopen("c:\\test.txt", "r");
if (!flatfile)
//error can't open file
fgets(text, sizeof(text)-1,flatfile); // read Line 1
fgets(text, sizeof(text)-1,flatfile); // read Line 2
fgets(text, sizeof(text)-1,flatfile); // read Line 3
//This part should be replace with a loop to read the entire flat file
* Function Clean Up

return (ER_SUCCESS);
-- End Code ------------------------

Christian Audet

Implementing B7333 (Xe) SP14.1, SQL
(Support B732, B7331 and B7332)<P ID="edit"><FONT SIZE=-1>Edited by christian_audet on 10/24/01 08:08 AM.</FONT></P>


Legendary Poster
Hi Christian,

First of all thank you for your help.
After I surfed a bit on the Microsoft pages to find an appropriate Stream I/O function, I also decided that I will try the "fgets".

I have already tried it and works fine.
There is only a really minor problem: "fgets" includes the newline character into the string. It is minor problem because I can easily "rtrim" it in OneWorld.

If the description of "fgets" is right then it is not necessary to decrease the size with 1 making room for the terminator null character. Please, see the description of fgets from the web:
The fgets function reads a string from the input stream argument and stores it in string. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in string is appended with a null character. The newline character, if read, is included in the string.
I have made an experiment and it seems to me that the -1 is really not necessary in the second parameter.
Please, let me know if you have different experience. Thanks.

Thanks your reply again,

B7332 SP11, ESU 4116422, Intel NT4, SQL 7 SP1
(working with B7321, B7331, XE too)