Text file IO  
Author Message
stuartsmithz





PostPosted: Mon Sep 29 11:09:49 CDT 2003 Top

MFC >> Text file IO I have a class that inputs data from a text file. The files can be very
large, and I'd like to display a progress bar to the user. Of course, to
determine this I need to know the total number of lines in the file. What's
the most efficient (i.e. fastest) way to get it? Right now I use brute
force: I read through the entire file, counting the lines but not processing
the data (code below). Then I reset the ifstream to prepare for reading the
data. Is there a better way?

Thanks,
Chip

// Construct ifstrem object for file input
ifstream File(Filename, ios::nocreate);

********
// Count total lines in file
streampos StartPos = File.tellg(); // Get file start position
FileLines = 0;
while(!File.eof())
{
File.ignore(1000, '\n'); // Get/ignore line
FileLines++;
}

// Prepare File for reading/processing
File.seekg(StartPos); // Return istream ptr to start position
File.clear(); // Reset eof bit
TRACE("File has %i lines\n", FileLines);

******
// Read data...

Visual Studio133  
 
 
steve





PostPosted: Mon Sep 29 11:09:49 CDT 2003 Top

MFC >> Text file IO On Mon, 29 Sep 2003 15:04:22 GMT, "Chip Pulitzer"
<EMail@HideDomain.com> wrote:

>I have a class that inputs data from a text file. The files can be very
>large, and I'd like to display a progress bar to the user. Of course, to
>determine this I need to know the total number of lines in the file. What's
>the most efficient (i.e. fastest) way to get it? Right now I use brute
>force: I read through the entire file, counting the lines but not processing
>the data (code below). Then I reset the ifstream to prepare for reading the
>data. Is there a better way?
>
>Thanks,
>Chip

I use the CFile class and the member function GetLength to calculate a
suitable bar size. The bar is stepped as the file is processed in
steps of the length / 100. It's probably no faster than your method
but the initial count of lines is not needed.


Steve
--
EasyNN-plus. The easy way to build neural networks.
Build networks from numeric, text and image files.
http://www.easynn.com
 
 
Ajay





PostPosted: Mon Sep 29 11:09:08 CDT 2003 Top

MFC >> Text file IO Couple of alternate ways:

- Why not show progress as the number of bytes read compared to total bytes
as you know the total bytes(size of the file).
- If you wanted to show number of lines, you could save this information in
the file itself (possibly the first thing) and simply read it when you open
the file.

--
Ajay Kalra [MVP - VC++]
EMail@HideDomain.com


"Chip Pulitzer" <EMail@HideDomain.com> wrote in message
news:WRXdb.718$EMail@HideDomain.com...
> I have a class that inputs data from a text file. The files can be very
> large, and I'd like to display a progress bar to the user. Of course, to
> determine this I need to know the total number of lines in the file.
What's
> the most efficient (i.e. fastest) way to get it? Right now I use brute
> force: I read through the entire file, counting the lines but not
processing
> the data (code below). Then I reset the ifstream to prepare for reading
the
> data. Is there a better way?
>
> Thanks,
> Chip
>
> // Construct ifstrem object for file input
> ifstream File(Filename, ios::nocreate);
>
> ********
> // Count total lines in file
> streampos StartPos = File.tellg(); // Get file start position
> FileLines = 0;
> while(!File.eof())
> {
> File.ignore(1000, '\n'); // Get/ignore line
> FileLines++;
> }
>
> // Prepare File for reading/processing
> File.seekg(StartPos); // Return istream ptr to start position
> File.clear(); // Reset eof bit
> TRACE("File has %i lines\n", FileLines);
>
> ******
> // Read data...
>
>

 
 
David





PostPosted: Mon Sep 29 12:46:39 CDT 2003 Top

MFC >> Text file IO You don't need to know the number of lines in the file, only the file
pointer's position. Set your progress bar from 0 to the file's size. Then
after each line is read, update the progress bar with file pointer's current
position. It's very simple.

"Chip Pulitzer" <EMail@HideDomain.com> wrote in message
news:WRXdb.718$EMail@HideDomain.com...
> I have a class that inputs data from a text file. The files can be very
> large, and I'd like to display a progress bar to the user. Of course, to
> determine this I need to know the total number of lines in the file.
What's
> the most efficient (i.e. fastest) way to get it? Right now I use brute
> force: I read through the entire file, counting the lines but not
processing
> the data (code below). Then I reset the ifstream to prepare for reading
the
> data. Is there a better way?
>
> Thanks,
> Chip
>
> // Construct ifstrem object for file input
> ifstream File(Filename, ios::nocreate);
>
> ********
> // Count total lines in file
> streampos StartPos = File.tellg(); // Get file start position
> FileLines = 0;
> while(!File.eof())
> {
> File.ignore(1000, '\n'); // Get/ignore line
> FileLines++;
> }
>
> // Prepare File for reading/processing
> File.seekg(StartPos); // Return istream ptr to start position
> File.clear(); // Reset eof bit
> TRACE("File has %i lines\n", FileLines);
>
> ******
> // Read data...
>
>