reading lines from file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • felixnielsen@hotmail.com

    reading lines from file

    The question is pretty simple, i have a file called "primes.txt " and
    funny enough it contains alot of primes (one per line)
    Besides that i have an empty vector:
    vector<__int64> P(0);

    How do i fill that with the contents of the file?
    P.push_back(lin e 1);
    P.push_back(lin e 2);
    ect.

    I have tryed different stuff with getline() but it just wont work.
    Hope someone can help ASAP.

    Regards
    Zacariaz

  • Pete Becker

    #2
    Re: reading lines from file

    felixnielsen@ho tmail.com wrote:[color=blue]
    > The question is pretty simple, i have a file called "primes.txt " and
    > funny enough it contains alot of primes (one per line)
    > Besides that i have an empty vector:
    > vector<__int64> P(0);
    >
    > How do i fill that with the contents of the file?
    > P.push_back(lin e 1);
    > P.push_back(lin e 2);
    > ect.
    >
    > I have tryed different stuff with getline() but it just wont work.
    > Hope someone can help ASAP.
    >[/color]

    The file provides a sequence of values, and you need to copy that
    sequence into a vector:

    ifstream input("primes.t x");
    copy(istream_it erator<__int64> (input), istream_iterato r<__int64>(),
    back_inserter(p ));

    This is a fundamental idiom. Learn it.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)

    Comment

    • felixnielsen@hotmail.com

      #3
      Re: reading lines from file

      Im trying to learn, thats why im asking...
      anyway thank for the answer...

      Comment

      • Pete Becker

        #4
        Re: reading lines from file

        felixnielsen@ho tmail.com wrote:[color=blue]
        > Im trying to learn, thats why im asking...
        >[/color]

        Sorry, didn't mean that to sound like a put-down.

        --

        Pete Becker
        Dinkumware, Ltd. (http://www.dinkumware.com)

        Comment

        • Kai-Uwe Bux

          #5
          Re: reading lines from file

          felixnielsen@ho tmail.com wrote:
          [color=blue]
          > The question is pretty simple, i have a file called "primes.txt " and
          > funny enough it contains alot of primes (one per line)
          > Besides that i have an empty vector:
          > vector<__int64> P(0);
          >
          > How do i fill that with the contents of the file?
          > P.push_back(lin e 1);
          > P.push_back(lin e 2);
          > ect.
          >
          > I have tryed different stuff with getline() but it just wont work.[/color]

          What about:

          #include <iostream>
          #include <vector>
          #include <iterator>
          #include <algorithm>
          #include <fstream>

          int main ( void ) {
          std::fstream primes ( "primes.txt " );
          std::vector< int > p_vect;
          // read
          std::copy( std::istream_it erator<int>( primes ),
          std::istream_it erator<int>(),
          std::back_inser ter( p_vect ) );
          // write
          std::copy( p_vect.begin(), p_vect.end(),
          std::ostream_it erator<int>( std::cout, "\n" ) );
          }


          Best

          Kai-Uwe Bux

          Comment

          • felixnielsen@hotmail.com

            #6
            Re: reading lines from file

            Np, i dont have a hard time with that kind of remark, but other might.
            ;-)

            Comment

            • felixnielsen@hotmail.com

              #7
              Re: reading lines from file

              The first piece of code suplyed by Pete worked fine, heres the code so
              you can correct me if i have made a bummer ;-)

              #include <iostream>
              #include <vector>
              #include <math.h>
              #include <fstream>
              #include <iterator>

              using namespace std;

              vector<__int64> P(0); // an empty vector to cotain the primes.
              int prime_pointer = 0; // is used to make sure that the same prime
              // isnt writen to file more than once.

              void find_prime() { // searches for primes.
              int p_size = P.size();
              bool loop;
              int i;
              for (__int64 test = P.back() + 2; test > 0 && p_size == P.size();
              test += 2) {
              for (loop = true, i = 0; P[i] < sqrt(test) && loop == true;
              i++) {
              if (test % P[i] == 0) {
              loop = false;
              }
              else if (P[i+1] > sqrt(test)) {
              loop = false;
              P.push_back(tes t);
              }
              }
              }
              }

              void print_primes() { // print the complete P vector on
              screen.
              for (int i = 0; i < P.size(); i++) {
              cout << P[i] << endl;
              }
              }

              void read_primes() { // read primes from file.
              ifstream primes("primes. txt");
              if (!primes && P.size() == 0) {
              P.push_back(2);
              P.push_back(3);
              }
              else if (P.size() == 0) {
              copy(istream_it erator<__int64> (primes),
              istream_iterato r<__int64>(), back_inserter(P ));
              prime_pointer = P.size();
              }
              primes.close();
              }

              void write_primes() { // write primes to file.
              ofstream primes("primes. txt",ios::out | ios::app);
              for (int i = prime_pointer; i < P.size(); i++) {
              primes << P[i] << endl;
              }
              primes.close();
              prime_pointer = P.size();
              }

              int main() {
              // some code.
              }

              By the way i have only been coding c++ for about a week, so cut me some
              slack eh? ;-)

              Comment

              • Kai-Uwe Bux

                #8
                Re: reading lines from file

                felixnielsen@ho tmail.com wrote:
                [color=blue]
                > The first piece of code suplyed by Pete worked fine, heres the code so
                > you can correct me if i have made a bummer ;-)
                >
                > #include <iostream>
                > #include <vector>
                > #include <math.h>[/color]

                #include <cmath>
                [color=blue]
                > #include <fstream>
                > #include <iterator>
                >
                > using namespace std;[/color]

                Questionable practice: you will want to avoid this in header files.
                Also: what you show below might give rise to a header.

                [color=blue]
                > vector<__int64> P(0); // an empty vector to cotain the primes.[/color]

                For the sake of this group, I would suggest the use of standard types; in
                this case: unsigned long

                [color=blue]
                > int prime_pointer = 0; // is used to make sure that the same prime
                > // isnt writen to file more than once.[/color]

                Generally, I frown upon the use of global variables. Looks like this wants
                to be a class PrimeNumberTabl e.

                [color=blue]
                > void find_prime() { // searches for primes.
                > int p_size = P.size();
                > bool loop;
                > int i;
                > for (__int64 test = P.back() + 2; test > 0 && p_size == P.size();
                > test += 2) {[/color]

                Will this outer for loop ever loop? P.size() will increase but where is
                p_size updated. If this for loop executes only once, why is it a loop?
                [color=blue]
                > for (loop = true, i = 0; P[i] < sqrt(test) && loop == true;
                > i++) {
                > if (test % P[i] == 0) {
                > loop = false;
                > }
                > else if (P[i+1] > sqrt(test)) {
                > loop = false;
                > P.push_back(tes t);
                > }
                > }
                > }
                > }[/color]

                Are you sure find_primes() is a good name. Looks like you really want to add
                primes to the table.
                [color=blue]
                >
                > void print_primes() { // print the complete P vector on screen.
                > for (int i = 0; i < P.size(); i++) {
                > cout << P[i] << endl;
                > }
                > }
                >[/color]

                std::copy( P.begin(), P.end(),
                std::ostream_it erator<unsigned long>(std::cout , "\n") );
                [color=blue]
                > void read_primes() { // read primes from file.
                > ifstream primes("primes. txt");
                > if (!primes && P.size() == 0) {
                > P.push_back(2);
                > P.push_back(3);
                > }
                > else if (P.size() == 0) {
                > copy(istream_it erator<__int64> (primes),
                > istream_iterato r<__int64>(), back_inserter(P ));
                > prime_pointer = P.size();
                > }
                > primes.close();[/color]

                The stream closes automaticall upon destruction.
                [color=blue]
                > }
                >
                > void write_primes() { // write primes to file.
                > ofstream primes("primes. txt",ios::out | ios::app);
                > for (int i = prime_pointer; i < P.size(); i++) {
                > primes << P[i] << endl;
                > }
                > primes.close();[/color]

                Stream closes automatically.
                [color=blue]
                > prime_pointer = P.size();
                > }
                >
                > int main() {
                > // some code.
                > }[/color]

                As I said, it looks like this wants to be a data structure. Something like

                class PrimeNumberTabl e {

                std::string file_name;
                std::vector< unsigned long > data;
                unsigned long file_size;

                void read_file ( void );
                // fills data from file

                void update_file ( void );
                // flushes additionally computed primes to file.

                public:

                PrimeNumberTabl e ( std::string f_name = "primes.txt " );
                // init file_name
                // read_file()
                // set file_size

                ~PrimeNumberTab le ( void );
                // update the file on disk and store all additionally computed
                // primes.

                void extend_table ( unsigned long new_max_entry );
                // add all prime numbers <= new_max_entry

                }; // PrimeNumberTabl e
                [color=blue]
                > By the way i have only been coding c++ for about a week, so cut me some
                > slack eh? ;-)[/color]

                In this group, we give you just enough rope to hang you :-)

                Ah, nevermind. One week you say? Impressive!


                Best

                Kai-Uwe Bux

                Comment

                • felixnielsen@hotmail.com

                  #9
                  Re: reading lines from file

                  Thank you for the very nice walktrough, allthough i am only just
                  started with the program you have given me something to think about ;-)

                  heres an update, however none of the above has been taken in to account
                  yet...

                  #include <iostream>
                  #include <vector>
                  #include <math.h>
                  #include <fstream>
                  #include <iterator>

                  using namespace std;

                  vector<__int64> P(0); // an empty vector to cotain the primes.
                  int prime_pointer = 0; // is used to make sure that the same prime
                  // isnt writen to file more than once.
                  bool quit = false;
                  void find_prime() { // searches for primes.
                  int p_size = P.size();
                  bool loop;
                  int i;
                  for (__int64 test = P.back() + 2; test > 0 && p_size == P.size();
                  test += 2) {
                  for (loop = true, i = 0; P[i] < sqrt(test) && loop == true;
                  i++) {
                  if (test % P[i] == 0) {
                  loop = false;
                  }
                  else if (P[i+1] > sqrt(test)) {
                  loop = false;
                  P.push_back(tes t);
                  }
                  }
                  }
                  }

                  void print_primes() { // print the complete P vector on
                  screen.
                  for (int i = 0; i < P.size(); i++) {
                  cout << P[i] << endl;
                  }
                  }

                  void read_primes() { // read primes from file.
                  ifstream primes("primes. txt");
                  if (!primes && P.size() == 0) {
                  P.push_back(2);
                  P.push_back(3);
                  }
                  else if (P.size() == 0) {
                  copy(istream_it erator<__int64> (primes),
                  istream_iterato r<__int64>(), back_inserter(P ));
                  prime_pointer = P.size();
                  }
                  primes.close();
                  }

                  void write_primes() { // write primes to file.
                  ofstream primes("primes. txt",ios::out | ios::app);
                  for (int i = prime_pointer; i < P.size(); i++) {
                  primes << P[i] << endl;
                  }
                  primes.close();
                  prime_pointer = P.size();
                  }

                  void choice() {
                  int choice;
                  int amount;
                  system("cls");
                  cout << " 1. Find primes | 2. Print primes | 3. Write primes to
                  file | 4. Quit |" << endl;
                  cout << endl;
                  cout << " Make a choice: ";
                  cin >> choice;
                  if (choice == 1) {
                  cout << " How many primes do you want to find?: ";
                  cin >> amount;
                  system("cls");
                  cout << "Searching for primes..." << endl;
                  for (int i = 0; i < amount; i++) {
                  find_prime();
                  }
                  cin.get();
                  cout << "Done! Press enter to continue." << endl;
                  cin.get();
                  }
                  else if (choice == 2) {
                  system("cls");
                  print_primes();
                  cin.get();
                  cout << "Done! Press enter to continue." << endl;
                  cin.get();
                  }
                  else if (choice == 3) {
                  system("cls");
                  cout << " Writing primes to file: primes.txt" << endl;
                  write_primes();
                  cin.get();
                  cout << "Done! Press enter to continue." << endl;
                  cin.get();
                  }
                  else if (choice == 4) {
                  quit = true;
                  }
                  else {
                  system("cls");
                  cin.get();
                  cout << "Invalid choice! Press enter to continue." << endl;
                  cin.get();
                  }
                  }

                  int main() {
                  read_primes();
                  while (quit == false) {
                  choice();
                  }
                  }

                  try it, its a great waste of time...

                  Comment

                  • felixnielsen@hotmail.com

                    #10
                    Re: reading lines from file

                    k, now i have had some time to look at the walktrough...

                    1.[color=blue]
                    > #include <math.h>[/color]
                    #include <cmath>

                    i don really know the header cmath and math.h seems to work fine, so i
                    will need and explanation on that.

                    2.[color=blue]
                    > using namespace std;[/color]
                    Questionable practice: you will want to avoid this in header files.
                    Also: what you show below might give rise to a header.

                    i must admit that i don really understand what u mean.

                    3.[color=blue]
                    > vector<__int64> P(0); // an empty vector to cotain the primes.[/color]
                    For the sake of this group, I would suggest the use of standard types;
                    in
                    this case: unsigned long

                    as for the __int64, im using it because i have some problems with my
                    compiler, unsigned and long doesnt seem to make any difference and im
                    yet to experience if __int64 does.
                    by the way im using Bloodshed dev-c++

                    4.[color=blue]
                    > int prime_pointer = 0; // is used to make sure that the same prime
                    > // isnt writen to file more than once.[/color]
                    Generally, I frown upon the use of global variables. Looks like this
                    wants
                    to be a class PrimeNumberTabl e.

                    i couldnt agree more, however im still very inexperienced and havent
                    yet had a chance to learn about classes, actually i started writing it
                    all in the main function and then converted it all into single
                    funtions.maybe ill do some class thingy in a while ;-)

                    5.[color=blue]
                    > void print_primes() { // print the complete P vector on screen.
                    > for (int i = 0; i < P.size(); i++) {
                    > cout << P[i] << endl;
                    > }
                    > }[/color]
                    std::copy( P.begin(), P.end(),
                    std::ostream_it erator<unsigned long>(std::cout , "\n") );

                    this is yet another smart thing that i dont understand, but i guess it
                    will come in time. you cant learn it all at once ;)

                    6.
                    The stream closes automaticall upon destruction.

                    if you are sure about that, ofcourse they have to go.

                    7.
                    Are you sure find_primes() is a good name. Looks like you really want
                    to add
                    primes to the table.

                    well, it might not be a good name, but it work for me ;-)

                    8.[color=blue]
                    > for (__int64 test = P.back() + 2; test > 0 && p_size == P.size();
                    > test += 2) {[/color]
                    Will this outer for loop ever loop? P.size() will increase but where is

                    p_size updated. If this for loop executes only once, why is it a loop?

                    the for loop might as well have been an if statement, u r right about
                    that, but i not quite finish with it, in time it will execute a fixed
                    number of loops.
                    the reason i need an if statement is that i dont want the program to do
                    some thing weird when it reaches value higer than the __int64 can
                    contain. in my experience ull get some weird negative number if thats
                    the case, and thats the reason for the test > 0 statement.

                    9.
                    Ah, nevermind. One week you say? Impressive!
                    while thank you ;-)
                    although i have done a little php before, i must admit im a little
                    proud of myself.


                    thats all i guess, thanks once again...

                    Comment

                    • Kai-Uwe Bux

                      #11
                      Re: reading lines from file

                      felixnielsen@ho tmail.com wrote:
                      [color=blue]
                      > k, now i have had some time to look at the walktrough...
                      >
                      > 1.[color=green]
                      >> #include <math.h>[/color]
                      > #include <cmath>
                      >
                      > i don really know the header cmath and math.h seems to work fine, so i
                      > will need and explanation on that.[/color]

                      For some standard headers there are two versions. In this case, you used
                      <math.h>. The header <cmath> provides the same functions, except that it
                      defines everything within the namespace std. That is usually a Good Thing
                      (tm).
                      [color=blue]
                      > 2.[color=green]
                      >> using namespace std;[/color]
                      > Questionable practice: you will want to avoid this in header files.
                      > Also: what you show below might give rise to a header.
                      >
                      > i must admit that i don really understand what u mean.[/color]

                      If you say

                      using namespace std;

                      in a header file (e.g., prime_numbers.h ), then every file including that
                      header will also have all identifiers from std dumped into global
                      namespace. It is considered Bad Practice (tm) for a library designer to
                      make such a decision for the clients.
                      [color=blue]
                      > 3.[color=green]
                      >> vector<__int64> P(0); // an empty vector to cotain the primes.[/color]
                      > For the sake of this group, I would suggest the use of standard types;
                      > in
                      > this case: unsigned long
                      >
                      > as for the __int64, im using it because i have some problems with my
                      > compiler, unsigned and long doesnt seem to make any difference and im
                      > yet to experience if __int64 does.
                      > by the way im using Bloodshed dev-c++[/color]

                      __int64 does not exist on all platforms. On my machine, your program simply
                      does not compile. __int64 is *not* standard C++. Your compiler happens to
                      support it, mine does not. Portability is a plus. In this case, you ca
                      achieve it without loosing anything.
                      [color=blue]
                      > 4.[color=green]
                      >> int prime_pointer = 0; // is used to make sure that the same prime
                      >> // isnt writen to file more than once.[/color]
                      > Generally, I frown upon the use of global variables. Looks like this
                      > wants
                      > to be a class PrimeNumberTabl e.
                      >
                      > i couldnt agree more, however im still very inexperienced and havent
                      > yet had a chance to learn about classes, actually i started writing it
                      > all in the main function and then converted it all into single
                      > funtions.maybe ill do some class thingy in a while ;-)
                      >
                      > 5.[color=green]
                      >> void print_primes() { // print the complete P vector on screen.
                      >> for (int i = 0; i < P.size(); i++) {
                      >> cout << P[i] << endl;
                      >> }
                      >> }[/color]
                      > std::copy( P.begin(), P.end(),
                      > std::ostream_it erator<unsigned long>(std::cout , "\n") );
                      >
                      > this is yet another smart thing that i dont understand, but i guess it
                      > will come in time. you cant learn it all at once ;)[/color]

                      The standard library has some very nifty concepts that interact quite
                      nicely:

                      * containers
                      * iterators
                      * algorithms

                      Containers encapsulate and abstract data structures like doubly linked lists
                      or balanced trees into things like std::list< some_type > or std::map< key,
                      value >. Iterators provide a uniform abstraction for access and traversal
                      of those data structures. Finally algorithms usually work on ranges defined
                      by pairs of iterators. This way, iterators decouple algorithms from
                      containers.


                      Best

                      Kai-Uwe Bux

                      Comment

                      • Kai-Uwe Bux

                        #12
                        Re: reading lines from file

                        felixnielsen@ho tmail.com wrote:
                        [color=blue]
                        > Thank you for the very nice walktrough, allthough i am only just
                        > started with the program you have given me something to think about ;-)
                        >
                        > heres an update, however none of the above has been taken in to account
                        > yet...
                        >[/color]
                        [code snipped][color=blue]
                        >
                        > try it, its a great waste of time...[/color]

                        Ok, and here is some code for you to chew on. You may find it worthwhile
                        figuring out how it does what it does. In particular, notice how the class
                        PrimeNumberTabl e encapsulates the file and vector handling.

                        Here goes:


                        #include <iostream>
                        #include <vector>
                        #include <cmath>
                        #include <fstream>
                        #include <iterator>
                        #include <string>
                        #include <cassert>

                        class PrimeNumberTabl e {

                        typedef std::vector< unsigned long > Table;

                        public:

                        typedef Table::size_typ e SizeType;

                        private:

                        std::string file_name;
                        Table data;
                        SizeType file_size;

                        // prevent copying
                        PrimeNumberTabl e ( PrimeNumberTabl e const & );
                        PrimeNumberTabl e & operator= ( PrimeNumberTabl e const & other );

                        bool ungarded_is_pri me ( unsigned long candidate ) const {
                        assert( data.back() >= std::sqrt( candidate ) );
                        for ( SizeType index = 0;
                        data[index] < std::sqrt( candidate );
                        ++ index ) {
                        if ( candidate % data[index] == 0 ) {
                        return ( false );
                        }
                        }
                        return ( true );
                        }

                        void extend_by_bound ( unsigned long max_entry ) {
                        unsigned long next_candidate = data.back() + 2;
                        while ( next_candidate <= max_entry ) {
                        if ( ungarded_is_pri me( next_candidate ) ) {
                        data.push_back( next_candidate );
                        }
                        next_candidate += 2;
                        }
                        }

                        void extend_by_lengt h ( SizeType length ) {
                        unsigned long next_candidate = data.back() + 2;
                        while ( data.size() < length ) {
                        if ( ungarded_is_pri me( next_candidate ) ) {
                        data.push_back( next_candidate );
                        }
                        next_candidate += 2;
                        }
                        }

                        public:

                        PrimeNumberTabl e ( std::string name = "primes.txt " )
                        : file_name ( name )
                        {
                        std::ifstream primes ( file_name.c_str () );
                        std::copy( std::istream_it erator<unsigned long>( primes ),
                        std::istream_it erator<unsigned long>(),
                        std::back_inser ter( data ) );
                        file_size = data.size();
                        if ( data.size() < 2 ) {
                        data.clear();
                        data.push_back( 2 );
                        data.push_back( 3 );
                        }
                        }

                        ~PrimeNumberTab le ( void ) {
                        std::ofstream primes ( file_name.c_str (),
                        std::ios::out | std::ios::app );
                        std::copy ( data.begin() + file_size, data.end(),
                        std::ostream_it erator<unsigned long>( primes, "\n" ) );
                        }

                        std::ostream & print ( std::ostream & o_str ) const {
                        std::copy ( data.begin(), data.end(),
                        std::ostream_it erator< unsigned long >( o_str, "\n" ) );
                        return( o_str );
                        }

                        bool is_prime ( unsigned long candidate ) {
                        extend_by_bound ( std::sqrt( candidate ) );
                        return ( ungarded_is_pri me( candidate ) );
                        }

                        unsigned long nth_prime ( SizeType index ) {
                        // math guys beware: indexing starts with 0
                        extend_by_lengt h( index+1 );
                        return( data[index] );
                        }

                        SizeType num_primes_less _than ( unsigned long bound ) {
                        extend_by_bound ( bound );
                        return( std::lower_boun d( data.begin(), data.end(), bound )
                        - data.begin() );
                        }

                        }; // PrimeNumberTabl e

                        int main() {
                        PrimeNumberTabl e t;
                        PrimeNumberTabl e::SizeType from = t.num_primes_le ss_than( 100 );
                        PrimeNumberTabl e::SizeType to = t.num_primes_le ss_than( 1000 );
                        std::cout << "The primes in the range [100,1000) are:\n";
                        for ( PrimeNumberTabl e::SizeType index = from; index < to; ++ index ) {
                        std::cout << t.nth_prime( index ) << '\n';
                        }
                        }


                        If you look closely enough, you will find that most of this is actually
                        inspired by the code you provided. Most of it, I just reorganized.


                        Best

                        Kai-Uwe Bux

                        Comment

                        • Luke Meyers

                          #13
                          Re: reading lines from file

                          Kai-Uwe Bux wrote:[color=blue]
                          > ~PrimeNumberTab le ( void ) {
                          > std::ofstream primes ( file_name.c_str (),
                          > std::ios::out | std::ios::app );
                          > std::copy ( data.begin() + file_size, data.end(),
                          > std::ostream_it erator<unsigned long>( primes, "\n" ) );
                          > }[/color]

                          Destructors that can throw are evil. Is this destructor evil?

                          Luke

                          Comment

                          • Jim Langston

                            #14
                            Re: reading lines from file

                            <felixnielsen@h otmail.com> wrote in message
                            news:1138564912 .753246.170330@ g44g2000cwa.goo glegroups.com.. .[color=blue]
                            > k, now i have had some time to look at the walktrough...
                            >
                            > 1.[color=green]
                            >> #include <math.h>[/color]
                            > #include <cmath>
                            >
                            > i don really know the header cmath and math.h seems to work fine, so i
                            > will need and explanation on that.[/color]

                            math.h is a C header. Most of the C headers can be included by cxxxxx xxxx
                            being the old name without .h. The main reason to use cxxxx instead of the
                            old headers is to respect namespace (afaik). So the old header <string.h>
                            would be <cstring> <math.h> would be <cmath>, etc...
                            [color=blue]
                            > 2.[color=green]
                            >> using namespace std;[/color]
                            > Questionable practice: you will want to avoid this in header files.
                            > Also: what you show below might give rise to a header.
                            >
                            > i must admit that i don really understand what u mean.[/color]

                            Well, using namespace std; totally defeats the purpose of having namespaces
                            in the first place. Namespaces were created to get over the problem of name
                            collision. You declare a function MyFunction() in your program, but some
                            header your include also happens to have a MyFunction() declared, you get
                            name collision. But, if the header you include is using the std namespace,
                            then there is no collision. Your function is in the unnamed namespace, the
                            one you included is in the std namespace. Your function is gotten to by
                            MyFunction(), the one you included is gotten to by std::MyFunction ().

                            Now, you say using namespace std; now you just brought everything you
                            included into the unnamed namespace, and you can get name collision. I know
                            it's a pain to keep typing std:: but you get used to it real quick so that
                            if you actually do it you soon don't even notice.
                            [color=blue]
                            > 3.[color=green]
                            >> vector<__int64> P(0); // an empty vector to cotain the primes.[/color]
                            > For the sake of this group, I would suggest the use of standard types;
                            > in
                            > this case: unsigned long
                            >
                            > as for the __int64, im using it because i have some problems with my
                            > compiler, unsigned and long doesnt seem to make any difference and im
                            > yet to experience if __int64 does.
                            > by the way im using Bloodshed dev-c++[/color]

                            As for unsigned and long not seeming to make a difference, try a quick test
                            and see.
                            long num1 = 0 - 1;
                            unsigned long num2 = 0 - 1;
                            std::cout << num1 << " " << num2 << std::endl;

                            output for my compiler is:
                            -1 4294967295
                            isn't it similar for yours?
                            [color=blue]
                            > 4.[color=green]
                            >> int prime_pointer = 0; // is used to make sure that the same prime
                            >> // isnt writen to file more than once.[/color]
                            > Generally, I frown upon the use of global variables. Looks like this
                            > wants
                            > to be a class PrimeNumberTabl e.
                            >
                            > i couldnt agree more, however im still very inexperienced and havent
                            > yet had a chance to learn about classes, actually i started writing it
                            > all in the main function and then converted it all into single
                            > funtions.maybe ill do some class thingy in a while ;-)[/color]

                            Well, you can declare this variable in main and pass it to read_primes() and
                            choice()
                            [color=blue]
                            > 5.[color=green]
                            >> void print_primes() { // print the complete P vector on screen.
                            >> for (int i = 0; i < P.size(); i++) {
                            >> cout << P[i] << endl;
                            >> }
                            >> }[/color]
                            > std::copy( P.begin(), P.end(),
                            > std::ostream_it erator<unsigned long>(std::cout , "\n") );
                            >
                            > this is yet another smart thing that i dont understand, but i guess it
                            > will come in time. you cant learn it all at once ;)[/color]

                            This is probabably the correct c++ way to do it, but I'd probably do it like
                            you did anyway.
                            [color=blue]
                            > 6.
                            > The stream closes automaticall upon destruction.
                            >
                            > if you are sure about that, ofcourse they have to go.[/color]

                            Yes, the stream closes automatically upon destruction, but I also close my
                            streams when I'm done with them. I feel it's kinda like a documentation in
                            the code saying I ain't gonna be using this stream no more in the current
                            method/function.
                            [color=blue]
                            > 7.
                            > Are you sure find_primes() is a good name. Looks like you really want
                            > to add
                            > primes to the table.
                            >
                            > well, it might not be a good name, but it work for me ;-)[/color]

                            Actually, read_primes loads the table. Find primes seems to search the
                            table, so it seems like a good name to me. Although I didn't look at the
                            function real close.
                            [color=blue]
                            > 9.
                            > Ah, nevermind. One week you say? Impressive!
                            > while thank you ;-)
                            > although i have done a little php before, i must admit im a little
                            > proud of myself.[/color]

                            You should be proud.


                            Comment

                            • Roland Pibinger

                              #15
                              Re: reading lines from file

                              On Sun, 29 Jan 2006 11:03:13 -0500, Kai-Uwe Bux <jkherciueh@gmx .net>
                              wrote:[color=blue]
                              >What about:
                              >
                              >int main ( void ) {
                              > std::fstream primes ( "primes.txt " );[/color]

                              No error handling when open fails.
                              [color=blue]
                              > std::vector< int > p_vect;
                              > // read
                              > std::copy( std::istream_it erator<int>( primes ),
                              > std::istream_it erator<int>(),
                              > std::back_inser ter( p_vect ) );[/color]

                              No error handling. What happens when a read error occcurs or when an
                              invalid 'prime' (some text) is read from "primes.txt "?
                              What if the user wants to know the line of the error?
                              [color=blue]
                              > // write
                              > std::copy( p_vect.begin(), p_vect.end(),
                              > std::ostream_it erator<int>( std::cout, "\n" ) );[/color]

                              Always returns EXIT_SUCCESS[color=blue]
                              >}[/color]

                              Comment

                              Working...