473,320 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

New member to group qustion

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.

Aug 10 '05 #1
4 1218
mr******@gmail.com wrote:
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
Aug 10 '05 #2
mr******@gmail.com wrote:
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::out |
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;
}
Aug 10 '05 #3
Larry I Smith wrote:
mr******@gmail.com wrote:
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::out |
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.
// 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;
}

Aug 11 '05 #4
Problem solved. Thanks for the insight guys.

Aug 11 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Tim Martin | last post by:
Hi, I came across a detail in "C++ Gotchas" by Stephen Dewhurst that confused me a bit. The author states: 'C++ has no "methods." Java and Smalltalk have methods. When you talk about an...
12
by: Anthony Jones | last post by:
Just a bit of background: I'm one of a group of FORTRAN programmers, looking to switch to C++. We are trying to write a few simple examples to demonstrate the power of the language to our manager,...
2
by: Brad D. | last post by:
In my secure database, I have the admins group and a DeptUser group. I am trying to set up a new user in the admins group and the DeptUser group. However, this new administrator receives the error...
33
by: Tony Toews | last post by:
Folks David Fenton does a fine job in helping folks in this newsgroup. I have learned from his postings and have enjoyed our discussions here. He belongs in this newsgroup more than many. ...
4
by: MLH | last post by:
A97: If I create a new user with CreateUser Method, is she automatically a member of Users group? Or, do I have to append her to that group?
9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
1
by: feng | last post by:
Hi, I am trying to debug my asp.net project in VS.Net. What I am getting is a error message that says "Error when trying to run project: Unable to start debugger on the web server. you do not...
5
by: Richard Smits | last post by:
Hello, We have a big problem. I created a Visual Studio with VB setup. On this computer everything works fine. But now I've cloned this image to 200 pc's and debugging does not work anymore. ...
9
by: Sameh Ahmed | last post by:
Hello there Is there a way through dotNet to check if a certain user is a member of a specific group? I use ADSI to get the memberships of the user then compare them to the group I want to check,...
7
by: Yen Kwoon | last post by:
Note: This problem is related to gcc but after some back and forth in group gnu.gcc.help it seems to have morph into more of a c++ specificiation question, hence the transplanting to this group. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.