473,804 Members | 4,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

seeekg/tellg not working properly?

I'm using mingw/g++ 3.3.3. When I use pos = tellg(), getline(), setg(pos), then
the next getline() does NOT get from the original position. I've tried doing
a clear() before the seekg() to no avail. In essence I've made my own "GetLine"
and "PeekLine" functions for my class but due to this problem its not working
properly

void BOOK::GetLine(s tring &line)
{
char linechar[MAX_LINE_SIZE];

line.erase();
inpFile.getline (linechar, MAX_LINE_SIZE);
line.assign(lin echar);
lineNo++;
};

void BOOK::PeekLine( string &line)
{
char linechar[MAX_LINE_SIZE];
streampos startPos;

startPos = inpFile.tellg() ;
inpFile.getline (linechar, MAX_LINE_SIZE);
line.erase();
line.assign(lin echar);
inpFile.seekg(s tartPos);
};

Am I doing something stupid, or am I justified in getting pissed off? This should
have taken about 30 seconds to code and test instead of 1hour+.
--
% Randy Yates % "Ticket to the moon, flight leaves here today
%% Fuquay-Varina, NC % from Satellite 2"
%%% 919-577-9882 % 'Ticket To The Moon'
%%%% <ya***@ieee.org > % *Time*, Electric Light Orchestra
http://home.earthlink.net/~yatescr
Jul 22 '05 #1
8 8072
"Randy Yates" <ya***@ieee.org > wrote in message
news:ac******** **@ieee.org...
I'm using mingw/g++ 3.3.3. When I use pos = tellg(), getline(), setg(pos),
then
the next getline() does NOT get from the original position. I've tried
doing
a clear() before the seekg() to no avail. In essence I've made my own
"GetLine"
and "PeekLine" functions for my class but due to this problem its not
working
properly

inpFile.seekg(s tartPos);


How's about using:
inpFile.seekg(s tartPos, ios_base::beg);

--
Unforgiven
Jul 22 '05 #2
"Unforgiven " <ja*******@hotm ail.com> writes:
"Randy Yates" <ya***@ieee.org > wrote in message
news:ac******** **@ieee.org...
I'm using mingw/g++ 3.3.3. When I use pos = tellg(), getline(), setg(pos),
then
the next getline() does NOT get from the original position. I've tried
doing
a clear() before the seekg() to no avail. In essence I've made my own
"GetLine"
and "PeekLine" functions for my class but due to this problem its not
working
properly

inpFile.seekg(s tartPos);
How's about using:
inpFile.seekg(s tartPos, ios_base::beg);


Thanks, but still no joy.
--
Unforgiven


Metallica?
--
% Randy Yates % "With time with what you've learned,
%% Fuquay-Varina, NC % they'll kiss the ground you walk
%%% 919-577-9882 % upon."
%%%% <ya***@ieee.org > % '21st Century Man', *Time*, ELO
http://home.earthlink.net/~yatescr
Jul 22 '05 #3
Randy Yates <ya***@ieee.org > writes:
"Unforgiven " <ja*******@hotm ail.com> writes:
"Randy Yates" <ya***@ieee.org > wrote in message
news:ac******** **@ieee.org...
I'm using mingw/g++ 3.3.3. When I use pos = tellg(), getline(), setg(pos),
then
the next getline() does NOT get from the original position. I've tried
doing
a clear() before the seekg() to no avail. In essence I've made my own
"GetLine"
and "PeekLine" functions for my class but due to this problem its not
working
properly

inpFile.seekg(s tartPos);


How's about using:
inpFile.seekg(s tartPos, ios_base::beg);


Thanks, but still no joy.


I have found since posting that this is apparently a bug in gcc. See gcc bug #13333.
--
% Randy Yates % "Watching all the days go by...
%% Fuquay-Varina, NC % Who are you and who am I?"
%%% 919-577-9882 % 'Mission (A World Record)',
%%%% <ya***@ieee.org > % *A New World Record*, ELO
http://home.earthlink.net/~yatescr
Jul 22 '05 #4
Randy Yates schrieb:
void BOOK::GetLine(s tring &line)
{
char linechar[MAX_LINE_SIZE];

line.erase();
inpFile.getline (linechar, MAX_LINE_SIZE);
line.assign(lin echar);
lineNo++;
};


FWIW, there's std::getline that takes a std::string as an argument, no
need for hardcoded length limits and a char array:

using namespace std;

void BOOK::GetLine(s tring &line)
{
getline(inpFile , line);
++lineNo;
}

Regards,
Malte
Jul 22 '05 #5
Malte Starostik <ma************ *@t-online.de> writes:
Randy Yates schrieb:
void BOOK::GetLine(s tring &line)
{
char linechar[MAX_LINE_SIZE];
line.erase();
inpFile.getline (linechar, MAX_LINE_SIZE);
line.assign(lin echar);
lineNo++;
};


FWIW, there's std::getline that takes a std::string as an argument, no
need for hardcoded length limits and a char array:

using namespace std;

void BOOK::GetLine(s tring &line)
{
getline(inpFile , line);
++lineNo;
}

Regards,
Malte


Thanks Malte, I'll keep that possibility in mind.
--
% Randy Yates % "And all that I can do
%% Fuquay-Varina, NC % is say I'm sorry,
%%% 919-577-9882 % that's the way it goes..."
%%%% <ya***@ieee.org > % Getting To The Point', *Balance of Power*, ELO
http://home.earthlink.net/~yatescr
Jul 22 '05 #6

"Randy Yates" <ya***@ieee.org > wrote in message
news:8y******** **@ieee.org...
Malte Starostik <ma************ *@t-online.de> writes:
Randy Yates schrieb:
void BOOK::GetLine(s tring &line)
{
char linechar[MAX_LINE_SIZE];
line.erase();
inpFile.getline (linechar, MAX_LINE_SIZE);
line.assign(lin echar);
lineNo++;
};


FWIW, there's std::getline that takes a std::string as an argument, no
need for hardcoded length limits and a char array:

using namespace std;

void BOOK::GetLine(s tring &line)
{
getline(inpFile , line);
++lineNo;
}

Regards,
Malte


Thanks Malte, I'll keep that possibility in mind.


But note that 'getline()' depends upon newline characters,
which can require translation (via a 'text mode' stream)(*),
and that in 'text mode', 'seek()' and 'tell()' functions
can never be guaranteed to have a byte-level precision.

(*) Some platforms externally represent '\n' with more than
a single character. In the C++ memory model, '\n' is
always a single character. (Ain't I/O fun? :-) )

-Mike
Jul 22 '05 #7

"Randy Yates" <ya***@ieee.org > wrote in message
news:ac******** **@ieee.org...
I'm using mingw/g++ 3.3.3. When I use pos = tellg(), getline(), setg(pos),
then
the next getline() does NOT get from the original position. I've tried
doing
a clear() before the seekg() to no avail. In essence I've made my own
"GetLine"
and "PeekLine" functions for my class but due to this problem its not
working
properly

void BOOK::GetLine(s tring &line)
{
char linechar[MAX_LINE_SIZE];

line.erase();
inpFile.getline (linechar, MAX_LINE_SIZE);
line.assign(lin echar);
lineNo++;
};

void BOOK::PeekLine( string &line)
{
char linechar[MAX_LINE_SIZE];
streampos startPos;

startPos = inpFile.tellg() ;
inpFile.getline (linechar, MAX_LINE_SIZE);
line.erase();
line.assign(lin echar);
inpFile.seekg(s tartPos);
};

Am I doing something stupid, or am I justified in getting pissed off? This
should
have taken about 30 seconds to code and test instead of 1hour+.


There's nothing I can see wrong with the code above. At least nothing
relevant to the problem you are describing. The problem is somewhere in the
code you didn't post.

If you can post a complete program that demonstrates this problem then
you'll get whatever it is sorted out very quickly.

Doing a clear() before the seekg() is a good idea however. It will make your
function work when the getline() happens to cause an end of file condition.

john
Jul 22 '05 #8

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:aN******** *********@newsr ead3.news.pas.e arthlink.net...

"Randy Yates" <ya***@ieee.org > wrote in message
news:8y******** **@ieee.org...
Malte Starostik <ma************ *@t-online.de> writes:
> Randy Yates schrieb:
>> void BOOK::GetLine(s tring &line)
>> {
>> char linechar[MAX_LINE_SIZE];
>> line.erase();
>> inpFile.getline (linechar, MAX_LINE_SIZE);
>> line.assign(lin echar);
>> lineNo++;
>> };
>
> FWIW, there's std::getline that takes a std::string as an argument, no
> need for hardcoded length limits and a char array:
>
> using namespace std;
>
> void BOOK::GetLine(s tring &line)
> {
> getline(inpFile , line);
> ++lineNo;
> }
>
> Regards,
> Malte


Thanks Malte, I'll keep that possibility in mind.


But note that 'getline()' depends upon newline characters,
which can require translation (via a 'text mode' stream)(*),
and that in 'text mode', 'seek()' and 'tell()' functions
can never be guaranteed to have a byte-level precision.

(*) Some platforms externally represent '\n' with more than
a single character. In the C++ memory model, '\n' is
always a single character. (Ain't I/O fun? :-) )


I had that problem once. I solved it by using the "extended" overload of
getline. I opened the input stream as binary stream and I explicitly
specified the eol character ('\x0A'). For what I had to solve, it was ok.

Catalin
Jul 22 '05 #9

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

Similar topics

2
2695
by: Fred Ma | last post by:
I'm using the stringstreams to get the numerical values of string tokens (the strings result from tokenizing a line of input elsewhere): #include <iostream> #include <sstream> using namespace std; int main(void) { istringstream iss("1.23DOG");
6
8349
by: Eivind Grimsby Haarr | last post by:
I know that this has been posted before on several other newsgroups, but I need to make sure I got this right, so I hope you can forgive me for posting this. In MVSC6.0, and also in several Borland c++ compilers from what I can see from newsgroup postings, ifstream::tellg() alters the position of the file reading pointer when reading UNIX files (only LF character, not CRLF) in text mode. I can see why it does this, keeping consistency...
16
667
by: kate | last post by:
salve. per favore rispondete alla mia domanda: Come faccio a ottenere le dimensioni di un file?(con c/c++) risp presto grazie
2
3605
by: philippe.deneve | last post by:
Hi all, I'm trying to retrieve the filesize of a file which size is larger then 4G. Old C functions are insufficient for they return a long. The standard c++ methods tellg() and seekg() also doesn't seem to work for these sizes. Do you know how I can resolve these kind of problems? regards, Philippe.
9
5449
by: wizofaus | last post by:
Is the any reason according to the standard that calling tellg() on an std::ifstream after a call to peek() could place the filebuf in an inconsistent state? I think it's a bug in the VC7 dinkumware implementation (and I've reported to them as such), but the following code std::ofstream ofs("test.txt"); ofs << "0123456789"; ofs.close(); std::wifstream ifs("test.txt");
0
1798
by: Chris | last post by:
I am reading in image files in a program and I read in the header in ascii mode. The problem is, sometimes tellg () gives me a completely incorrect result and sometimes it is just fine. Here is an example: char input; std::ifstream fin("blocks.pgm", std::ios::in); std::ifstream::pos_type position = fin.tellg(); fin>>input; position = fin.tellg();
1
4355
by: Chris | last post by:
I am reading in image files in a program and I read in the header in ascii mode and the data in binary mode. The problem is, sometimes tellg() gives me a completely incorrect result and sometimes it is just fine. It is quite annoying because there is no other good way to read these file and I think it is a problem with Visual C++, since it works just fine when I compile it using g++ on a Linux system. Here is an example: char input;
1
8224
by: Jacek Dziedzic | last post by:
Hello! This is my first time dealing with Very Large Files. I have vector of strings representing numbers and I need to extract bytes in binary mode from a Large File that correspond to ranges specified by the strings. For example for an input of "0", "100", "500", "700" I need to create three files, the first would contain
12
4798
by: toton | last post by:
Hi, I am reading a big file , and need to have a flag for current file position so that I can store the positions for later direct access. However it looks tellg is a very costly function ! But it's code says it should just return the current buffer position , thus should be a very low cost function. To explain, { boost::progress_timer t; std::ifstream in("Y:/Data/workspaces/tob4f/tob4f.dat");
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.