The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 04-12-2008, 10:42 AM
MrPickle's Avatar
MrPickle MrPickle is offline
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 305
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
Getting the current line in a file

When you're reading a file, is there a way to tell what line you're currently reading.

Example:

0 The quick
1 brown
2 fox jumps
3 over the
4 lazy
5 d
6 o
7 g

If we were reading "lazy" then it would return 4 because that's the line that lazy's on.

I read about a function "tellg()" but I think that returns position in bytes not in lines.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 04-12-2008, 12:03 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,119
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Make a counter per line and add the if statement before read()

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to HelloWorld For This Useful Post:
MrPickle (04-13-2008)
  #3 (permalink)  
Old 04-12-2008, 12:23 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
as HelloWorld said, the way to do it is to keep a counter for each call to getline() that you issue to the fstream.

"lines" as you probably know them are indicated by a '\n' (newline) character. When you open a file, your program has absolutely no idea which characters reside in your file until you actually read them - there's no built-in mechanism within the language which counts the number of '\n' characters automatically, so you have to do it yourself.
(since '\n' isn't really a special character, its just the same as any other character, as far as the language is concerned)

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
MrPickle (04-13-2008)
  #4 (permalink)  
Old 04-13-2008, 05:11 AM
MrPickle's Avatar
MrPickle MrPickle is offline
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 305
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
That's what I was doing I was just wondering if there's a better way, thanks

Also, is there a way to advance to the next line through a function?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by MrPickle : 04-13-2008 at 11:25 AM.
Reply With Quote
  #5 (permalink)  
Old 04-13-2008, 05:35 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Again, getline will do that for you. if you don't care about the rest of a line, then you could also do an ignore, instead of a getline into a dummy string. The principles and methods here are identical to the problem of the common "how do I empty cin", in that you're skipping all the remaining chars up to, and including the next newline character, except that you're reading a file stream (all streams in C++ are basically the same).

Code:
std::ifstream myfile;
//etc..
myfile.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following 2 Users Say Thank You to Bench For This Useful Post:
MrPickle (04-14-2008), TeraTask (04-15-2008)
  #6 (permalink)  
Old 04-14-2008, 03:32 AM
MrPickle's Avatar
MrPickle MrPickle is offline
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 305
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
Thanks once again Bench.

Again another question, is it possible to go backwards 1 line?
I've read about seekg but reading about it just went straight over my head.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by MrPickle : 04-14-2008 at 07:02 AM.
Reply With Quote
  #7 (permalink)  
Old 04-14-2008, 11:54 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,119
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Quote:
Originally Posted by MrPickle View Post
Thanks once again Bench.

Again another question, is it possible to go backwards 1 line?
I've read about seekg but reading about it just went straight over my head.
ummm... what do you mean by going backwards one line? what are you trying to do? if you want to make sure that you get one line back, then each time you read one line, store it in a temp variable before going to the next 2 lines..

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to HelloWorld For This Useful Post:
MrPickle (04-15-2008)
  #8 (permalink)  
Old 04-15-2008, 05:30 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
No, that's not possible. remember that a stream is not a file, but a "pipe" which acts as an input or output interface to/from a file. physical access to a file isn't supported by standard C++, since its O/S dependent (There might be some OS-specific libraries which do this). You're limited to the things which a stream can do - when you read from a stream, the bytes are removed from that stream, there's no 'going back', nor is there any built-in buffer which remembers anything that you've read from the stream (Though you could write one).

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
MrPickle (04-15-2008)
  #9 (permalink)  
Old 04-15-2008, 06:12 PM
MrPickle's Avatar
MrPickle MrPickle is offline
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 305
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
Looks like I'll have to write one!

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #10 (permalink)  
Old 04-18-2008, 12:26 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,119
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Quote:
Originally Posted by Bench View Post
No, that's not possible. remember that a stream is not a file, but a "pipe" which acts as an input or output interface to/from a file. physical access to a file isn't supported by standard C++, since its O/S dependent (There might be some OS-specific libraries which do this). You're limited to the things which a stream can do - when you read from a stream, the bytes are removed from that stream, there's no 'going back', nor is there any built-in buffer which remembers anything that you've read from the stream (Though you could write one).
I'm unfamiliar with C++, but can't you flush the buffer each time you read?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 07:28 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50