New member to group qustion

M

mross462

Hello,

At my work we are implementing a progress bar status display which
increments a percentage bar by the number of bytes read of a file and
then comparing that to the full filesisze.
We are using the getline function in fstream and I have seen examples
that obtain the number of bytes reading a character at a time, but not
a line at a time.

I'm needing to be able to determine the number of bytes read in by
getline and then compare a sum of those values to the size of the
complete file. If anyone could help it would be greatly appreciated.
 
V

Victor Bazarov

At my work we are implementing a progress bar status display which
increments a percentage bar by the number of bytes read of a file and
then comparing that to the full filesisze.
We are using the getline function in fstream and I have seen examples
that obtain the number of bytes reading a character at a time, but not
a line at a time.

I'm needing to be able to determine the number of bytes read in by
getline and then compare a sum of those values to the size of the
complete file. If anyone could help it would be greatly appreciated.

I don't have a code example for you, but if you can obtain "current file
position", then you can calculate how far you are from the end, right?

V
 
L

Larry I Smith

Hello,

At my work we are implementing a progress bar status display which
increments a percentage bar by the number of bytes read of a file and
then comparing that to the full filesisze.
We are using the getline function in fstream and I have seen examples
that obtain the number of bytes reading a character at a time, but not
a line at a time.

I'm needing to be able to determine the number of bytes read in by
getline and then compare a sum of those values to the size of the
complete file. If anyone could help it would be greatly appreciated.

Here's an example that demonstrates some possibilities.

// mross.cpp - one way to get total file size, then
// read the file one 'line' at a time, keeping
// track of the number of bytes read.

#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char * argv[])
{
if (argc < 2)
{
std::cout << "usage: mross file_to_read"
<< std::endl;
return 1;
}

std::fstream::pos_type len = 0;
std::fstream::pos_type tot = 0;
std::fstream f;

f.open(argv[1], std::ios_base::in |
std::ios_base::eek:ut |
std::ios_base::binary);

if (f)
{
// seek to the end of the file
f.seekg(0, std::ios_base::end);

if (f)
{
// get curr position (i.e. the file length)
len = f.tellg();

// seek back to the beginning of the file
f.seekg(0, std::ios_base::beg);
}
}
else
{
std::cerr << "failed to open " << argv[1]
<< std::endl;
return 2;
}

if (f.fail())
{
std::cerr << "seek/tell failed" << std::endl;
return 3;
}

std::cout << "size of file " << argv[1] << " is "
<< len << std::endl;

while (f)
{
std::string buf;

// read a line (up to the next newline) into 'buf'.
// the newline is discarded (not placed into buf)
// Note: use 'f.read()' rather than 'getline()' for
// truly binary files (jpegs, word docs, etc).
getline(f, buf);

if (f)
{
// add one for the newline read, but not saved,
// plus the length of the line read to 'tot'
tot += (1 + buf.length());

std::cout << "tot read: " << tot << std::endl;
}
}

f.close();

return 0;
}
 
L

Larry I Smith

Larry said:
Hello,

At my work we are implementing a progress bar status display which
increments a percentage bar by the number of bytes read of a file and
then comparing that to the full filesisze.
We are using the getline function in fstream and I have seen examples
that obtain the number of bytes reading a character at a time, but not
a line at a time.

I'm needing to be able to determine the number of bytes read in by
getline and then compare a sum of those values to the size of the
complete file. If anyone could help it would be greatly appreciated.

Here's an example that demonstrates some possibilities.

// mross.cpp - one way to get total file size, then
// read the file one 'line' at a time, keeping
// track of the number of bytes read.

#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char * argv[])
{
if (argc < 2)
{
std::cout << "usage: mross file_to_read"
<< std::endl;
return 1;
}

std::fstream::pos_type len = 0;
std::fstream::pos_type tot = 0;
std::fstream f;

If the OS is Windows -and- the file is a plain text file,
then omit the 'std::ios_base::binary' flag from the
following f.open() call.
f.open(argv[1], std::ios_base::in |
std::ios_base::eek:ut |
std::ios_base::binary);

if (f)
{
// seek to the end of the file
f.seekg(0, std::ios_base::end);

if (f)
{
// get curr position (i.e. the file length)
len = f.tellg();

// seek back to the beginning of the file
f.seekg(0, std::ios_base::beg);
}
}
else
{
std::cerr << "failed to open " << argv[1]
<< std::endl;
return 2;
}

if (f.fail())
{
std::cerr << "seek/tell failed" << std::endl;
return 3;
}

std::cout << "size of file " << argv[1] << " is "
<< len << std::endl;

while (f)
{
std::string buf;

// read a line (up to the next newline) into 'buf'.
// the newline is discarded (not placed into buf)
// Note: use 'f.read()' rather than 'getline()' for
// truly binary files (jpegs, word docs, etc).
getline(f, buf);

if (f)
{

If the OS is Windows -and- the file is a plain text file,
then the next line of code should actually be:

tot += (2 + buf.length());

Windows converts each "\r\n" line terminator to '\n'
as the file is read, and getline() dropped the '\n'.
So, 2 needs to be added to buf.length() to keep 'tot'
correct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top