C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 15th, 2007, 02:51 PM
turck3 turck3 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 12 turck3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 50 sec
Reputation Power: 0
File IO - "Reoppenning" a file

Is their a way in the fstream to reopen a file? By this I mean, send the line reader to the top of the file or to a specific line of the file?

Reply With Quote
  #2  
Old December 16th, 2007, 07:56 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
ifstream get;
// open a file, do stuff
get.seekg(0,ios_bas::beg);

ofstream put;
// same
put.seekp(0,ios_bas::beg);

Seek has the p or g postfix, one for the "put" side of things (output files) and one for input files - since you can, after all, be doing both to the same file.

Reply With Quote
  #3  
Old December 16th, 2007, 07:14 PM
turck3 turck3 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 12 turck3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 50 sec
Reputation Power: 0
Ok

Ok, tried that.. works great

Now I need to know if their is a way to jump to a specific line number of a file...

background info on why:

I'm making a program that reads in a coding language that I created for a game where you program robots..

of course like in most coding languages I'm adding in 'goto' and linelabels. To effectively do this, I need to have the ability to skip to a line with out reading the entire file to that point again (efficiency).

Reply With Quote
  #4  
Old December 17th, 2007, 04:14 PM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
To do that, you must first read through the entire file, noting the offset/position of each location you may want to jump to, and then jump according to that.

Reply With Quote
  #5  
Old December 17th, 2007, 07:34 PM
turck3 turck3 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 12 turck3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 50 sec
Reputation Power: 0
Quote:
Originally Posted by MaHuJa
To do that, you must first read through the entire file, noting the offset/position of each location you may want to jump to, and then jump according to that.


I'm well aware of this.. I need to know how to jump to a specific line rather than just the beginning or end of the file....

I need code.. I understand code. (something similar to your first reply would be more than sufficient)


Also just a general explanation of the parameters asociated with seekg would be nice

Reply With Quote
  #6  
Old December 17th, 2007, 07:52 PM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
The seekg parameters are, first of all, int offset - which is the byte position in the file. The second is one of
ios_bas::beg
ios_bas::cur
ios_bas::end
Which is where the value is taken from. Obviously, a negative offset from the beginning, or a positive offset from the end, won't work.

I'm going to write up an example of how to do the label vs position.
There is no way to "skip" lines, you end up reading them even if you don't use them.

Reply With Quote
  #7  
Old December 17th, 2007, 08:16 PM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
cpp Code:
Original - cpp Code
  1.  
  2. #include <fstream>
  3. #include <map>
  4. #include <string>
  5. using namespace std;
  6.  
  7. bool islabel(string s);
  8. string labelstring (string s);
  9.  
  10. void byLabel()
  11. {
  12.     ifstream infile("infile");
  13.     map<string,ifstream::pos_type> offsets;
  14.  
  15.     string s;
  16.     while (!infile.eof()) {
  17.         getline(infile, s);
  18.         if (islabel(s)) {
  19.             offsets[labelstring(s)]=infile.tellg();
  20.         }
  21.     }
  22.  
  23.     // later, when you want to go to a label,
  24.     string label;
  25.     infile.seekg(offsets[label], ios_base::beg);
  26. }
  27.  
  28. void byLinenumber()
  29. {
  30.     ifstream infile("infile");
  31.     map<int,ifstream::pos_type> offsets;
  32.  
  33.     int line = 1;
  34.     while (!infile.eof()) {
  35.         offsets[line]=infile.tellg();
  36.         infile.ignore(-1,'\n');
  37.     }
  38.  
  39.     // going to a specific line
  40.     int lineno;
  41.     infile.seekg(offsets[lineno], ios_base::beg);
  42. }

Reply With Quote
  #8  
Old December 17th, 2007, 09:21 PM
turck3 turck3 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 12 turck3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 50 sec
Reputation Power: 0
I actually didn't need the code for that bit then.. if I still need to read them in.. which really sucks.. then I already have in my head what needs to be done

Reply With Quote
  #9  
Old December 17th, 2007, 09:43 PM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
As far as the operating system is concerned, a file is just a sequence of bytes, not a collection of lines.

If the file is small enough, you can scan through the entire file every time you need to jump around. Basically, I'd WAG the limit for where it might not work anylonger might be around 100 kb or so; especially if the OS keeps a cache of the file contents after it's been read once. Testing/Profiling will show where it's no longer usable and functionality like above has to be added.

Alternatively, you could preload the entire file into memory, and use a stringstream.

Reply With Quote
  #10  
Old December 20th, 2007, 08:43 AM
turck3 turck3 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 12 turck3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 50 sec
Reputation Power: 0
Indeed, I currently have goto's working by rescanning the file.. later ill probably archive the file in the computer.. although I don't expect the files to ever exceed 1000 lines (and that would be pushing it) I'd like the system I use to be efficient.

Reply With Quote
  #11  
Old December 20th, 2007, 10:13 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson

If you have it working like that, leave it like that until you have a very real reason to change it.


"I'd like the system I use to be efficient."

Efficient and fast everywhere is a topic for those who develop core libraries and frameworks (think microsoft's .net developers). The rest of us should stick to making sure it works at all first and foremost. There is far less "clutter code" like this.

Reply With Quote
  #12  
Old December 20th, 2007, 11:27 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
I agree completely with this! Donald Knuth: "Premature optimization is the root of all evil".

And next to that, it is very difficult to guess what part of your program is the bottleneck (small programs excepted, but you only make those while studying it seems..). Programmers (including you! ) are notoriously bad at guessing which part is hogging all the cpu, moreover it is usually only a small part of the program which causes the biggest part of the performance hit.

Moral: go for correctness first, robustness second and only optimize when you really have to, and only after you have profiled your code intensively.
__________________
There is no such thing as C/C++, you either program C or C++

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > File IO - "Reoppenning" a file


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap