View previous topic :: View next topic |
Author |
Message |
bradcarter n00b
Joined: 12 Nov 2002 Posts: 53
|
Posted: Tue Apr 15, 2003 4:25 am Post subject: reset file pointer in C++ |
|
|
bare with me, I am kinda learning this as I go
What I am planning on doing is reading a textfile with each line an element in an array, as I do not know how many lines this file will have at any given time, I am running through once to count the number of lines, creating a dynamic sized array, then filling said array with each line.
That is what I hope to work towards (just incase anyone can offer a better way), but for now as I am learning I am doing a bit at a time. What I have now is something to count the lines then I am trying to reset the pointer with seekg then write it out to test that it actually did reset. Unfortunatly as it is only writing the contents of the file once it must not be resetting.
Any thoughts as to why? and feel free to pick apart anything I have there as I said I am learning.
Code: |
#include <iostream>
#include <fstream>
#include <stdlib.h>
//define the size of the temp buffer
#define BUF 500
using namespace std;
int main(int argc, char *argv[]){
char temp[BUF];
ifstream ifs("emerge.txt", ios::in);
int num_lines=0;
while (!ifs.eof()){
ifs.getline(temp, sizeof(temp), '\n');
cout <<endl<<num_lines<<": "<<temp;
num_lines++;
}
//reset then run through again to check
ifs.seekg (0, ios::beg);
while (!ifs.eof()){
ifs.getline(temp, sizeof(temp), '\n');
cout <<endl<<num_lines<<": "<<temp;
}
return 0;
}
|
Thanks |
|
Back to top |
|
|
Braempje l33t
Joined: 31 Jan 2003 Posts: 748
|
Posted: Tue Apr 15, 2003 6:24 am Post subject: Re: reset file pointer in C++ |
|
|
bradcarter wrote: | bare with me, I am kinda learning this as I go
What I am planning on doing is reading a textfile with each line an element in an array, as I do not know how many lines this file will have at any given time, I am running through once to count the number of lines, creating a dynamic sized array, then filling said array with each line.
|
That's not really the C++ way, in C++ you have the STL, where you have vector. That's a better and safer container than an array. For information on STL, try for example http://www.sgi.com/tech/stl/. This way you have to open the file only once...
bradcarter wrote: |
That is what I hope to work towards (just incase anyone can offer a better way), but for now as I am learning I am doing a bit at a time. What I have now is something to count the lines then I am trying to reset the pointer with seekg then write it out to test that it actually did reset. Unfortunatly as it is only writing the contents of the file once it must not be resetting.
Any thoughts as to why? and feel free to pick apart anything I have there as I said I am learning.
Code: |
#include <iostream>
#include <fstream>
#include <stdlib.h>
//define the size of the temp buffer
#define BUF 500
using namespace std;
int main(int argc, char *argv[]){
char temp[BUF];
ifstream ifs("emerge.txt", ios::in);
int num_lines=0;
while (!ifs.eof()){
ifs.getline(temp, sizeof(temp), '\n');
cout <<endl<<num_lines<<": "<<temp;
num_lines++;
}
//reset then run through again to check
ifs.seekg (0, ios::beg);
while (!ifs.eof()){
ifs.getline(temp, sizeof(temp), '\n');
cout <<endl<<num_lines<<": "<<temp;
}
return 0;
}
|
Thanks |
There are a lot of improvements in this code, not using arrays is just a start. But keep asking questions, and you'll start learning it! |
|
Back to top |
|
|
bradcarter n00b
Joined: 12 Nov 2002 Posts: 53
|
Posted: Tue Apr 15, 2003 6:27 pm Post subject: |
|
|
Hey thanks I am now reading about Templates. They were chapter 23 and I had not made it that far
We'll see if I can come up with something better |
|
Back to top |
|
|
PK n00b
Joined: 18 Jan 2003 Posts: 45 Location: Vancouver
|
Posted: Tue Apr 15, 2003 7:06 pm Post subject: |
|
|
This is how I would do it.
Code: |
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main( int argc, char **argv )
{
std::ifstream fin( "emerge.txt", std::ios::in );
if ( !fin.is_open() )
{
std::cerr << "Couldn't open emerge.txt\n";
return 1;
}
std::vector<std::string> lines;
std::string str;
while ( getline( fin, str ) )
{
lines.push_back( str );
}
return 0;
}
|
|
|
Back to top |
|
|
bradcarter n00b
Joined: 12 Nov 2002 Posts: 53
|
Posted: Tue Apr 15, 2003 7:30 pm Post subject: |
|
|
That looks a lot simpler as a question what is the difference between saying
using namespace std
and having std in front of things? is one more efficiant or better practice? I am trying to keep good practices.
Thanks a lot |
|
Back to top |
|
|
Attis SH n00b
Joined: 23 Feb 2003 Posts: 62 Location: Budapest, Hungary
|
Posted: Tue Apr 15, 2003 7:40 pm Post subject: |
|
|
bradcarter wrote: | That looks a lot simpler as a question what is the difference between saying
using namespace std
and having std in front of things? is one more efficiant or better practice? I am trying to keep good practices.
Thanks a lot |
Usually it isn't very nice to say using namespace xy in your source, except for educational purposes. It's a bit better to say eg.
Code: | using std::string;
using std::vector;
using std::ifstream; |
Because this way it's clear you use these specific classes.
The rules are different for header files, though. You should avoid using the using directive wherever possible, because you'd force the users of your header file (or module) to use those types. This might be acceptable only inside your own namespace in my opinion. |
|
Back to top |
|
|
vent n00b
Joined: 19 Mar 2003 Posts: 18
|
Posted: Tue Apr 15, 2003 8:34 pm Post subject: |
|
|
the using namespace is useful for saving yourself some typing, since it's use saves you from fully qualifing every function/variable you use that is outside the global namespace.
how you use it is definitely a personal preferrence. i do use it, but limit its scope by placing it inside functions or certain blocks of code.
by the way even if you do use it globally(outside of any functions)
Code: |
using namespace std;
|
you can still fully qualify a function or variable to insure that you are using what you intend to. _________________ Succumb to natural tendencies. Be hateful and boring. |
|
Back to top |
|
|
Braempje l33t
Joined: 31 Jan 2003 Posts: 748
|
Posted: Wed Apr 16, 2003 5:25 am Post subject: |
|
|
vent wrote: | the using namespace is useful for saving yourself some typing, since it's use saves you from fully qualifing every function/variable you use that is outside the global namespace.
how you use it is definitely a personal preferrence. i do use it, but limit its scope by placing it inside functions or certain blocks of code.
by the way even if you do use it globally(outside of any functions)
Code: |
using namespace std;
|
you can still fully qualify a function or variable to insure that you are using what you intend to. |
It is usefull, but you should try to avoid it for bigger programs, it slows down things very much. |
|
Back to top |
|
|
bradcarter n00b
Joined: 12 Nov 2002 Posts: 53
|
Posted: Wed Apr 16, 2003 1:45 pm Post subject: |
|
|
Thanks I will try and use std::XXXX if I get used to typing it it should be no big deal later on |
|
Back to top |
|
|
|