Read line after line but from the eof.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ground21

    Read line after line but from the eof.

    Hello.
    How could I read the whole text file line after line from the end of
    file? (I want to ~copy~ file)

    I want to copy file in this way:

    file 1:

    a
    b
    c


    file 2:

    c
    b
    a


    --
    Ground21_______ _____\\
    ground21@poczta KROPKAfm
  • Rolf Magnus

    #2
    Re: Read line after line but from the eof.

    Ground21 wrote:
    Hello.
    How could I read the whole text file line after line from the end of
    file? (I want to ~copy~ file)
    >
    I want to copy file in this way:
    >
    file 1:
    >
    a
    b
    c
    >
    >
    file 2:
    >
    c
    b
    a
    The easiest way would be to read all the lines into something like an
    std::vector<std ::string, then write the new file iterating backwards
    through the vector. As long as you don't have huge files, this should work
    ok.

    Comment

    • Ground21

      #3
      Re: Read line after line but from the eof.

      The easiest way would be to read all the lines into something like an
      std::vector<std ::string, then write the new file iterating backwards
      through the vector. As long as you don't have huge files, this should work
      ok.
      I think I wrote bad example:

      Once more:

      file1:

      text1
      text2
      text3
      abctest

      file2:

      abctest
      text3
      text2
      text1


      --
      Ground21_______ _____\\
      ground21@poczta KROPKAfm

      Comment

      • Rolf Magnus

        #4
        Re: Read line after line but from the eof.

        Ground21 wrote:
        >
        >The easiest way would be to read all the lines into something like an
        >std::vector<st d::string, then write the new file iterating backwards
        >through the vector. As long as you don't have huge files, this should
        >work ok.
        >
        I think I wrote bad example:
        I did understand your posting anyway.
        Once more:
        >
        file1:
        >
        text1
        text2
        text3
        abctest
        >
        file2:
        >
        abctest
        text3
        text2
        text1
        Yup. What you should do is read the file line by line and put each line as a
        separate string into a vector. So later you have a vector of lines. Then
        you can iterate through that vector backwards and write the strings out
        again.

        Comment

        • Ondra Holub

          #5
          Re: Read line after line but from the eof.


          Rolf Magnus napsal:
          Ground21 wrote:
          >
          The easiest way would be to read all the lines into something like an
          std::vector<std ::string, then write the new file iterating backwards
          through the vector. As long as you don't have huge files, this should
          work ok.
          I think I wrote bad example:
          >
          I did understand your posting anyway.
          >
          Once more:

          file1:

          text1
          text2
          text3
          abctest

          file2:

          abctest
          text3
          text2
          text1
          >
          Yup. What you should do is read the file line by line and put each line as a
          separate string into a vector. So later you have a vector of lines. Then
          you can iterate through that vector backwards and write the strings out
          again.
          I would recommend std::list instead of std::vector, because list can be
          simplier (== more effectively) extended in length.

          Comment

          • red floyd

            #6
            Re: Read line after line but from the eof.

            Ondra Holub wrote:
            Rolf Magnus napsal:
            >Ground21 wrote:
            >>
            >>>The easiest way would be to read all the lines into something like an
            >>>std::vector< std::string, then write the new file iterating backwards
            >>>through the vector. As long as you don't have huge files, this should
            >>>work ok.
            >>I think I wrote bad example:
            >I did understand your posting anyway.
            >>
            >>Once more:
            >>>
            >>file1:
            >>>
            >>text1
            >>text2
            >>text3
            >>abctest
            >>>
            >>file2:
            >>>
            >>abctest
            >>text3
            >>text2
            >>text1
            >Yup. What you should do is read the file line by line and put each line as a
            >separate string into a vector. So later you have a vector of lines. Then
            >you can iterate through that vector backwards and write the strings out
            >again.
            >
            I would recommend std::list instead of std::vector, because list can be
            simplier (== more effectively) extended in length.
            >
            I used deque.

            I tried to be real clever, with reverse_copy on an istream_iterato r, and
            an ostream_iterato r, but reverse_copy requires Bidirectional Iterators. :(

            Comment

            • Ground21

              #7
              Re: Read line after line but from the eof.

              Rolf Magnus napisał(a):
              Yup. What you should do is read the file line by line and put each line as a
              separate string into a vector. So later you have a vector of lines. Then
              you can iterate through that vector backwards and write the strings out
              again.
              can You tell me what's wrong with this code?:
              (I want to save lines from file 'file' to vectors, and then, write them
              into "tmp.txt"). I got wrong output "tmp.log".. . I think that is problem
              with char*...

              #include <vector.h>
              vector <char*tab(10) ;

              tmp=fopen("tmp. txt","w");
              fseek(file, 0L, SEEK_SET);
              for(int i=0; i<lines; i++)
              {
              fgets(tab[i],256,file);
              }
              for(int i=lines; i>0; i--)
              {
              fputs(tab[i],tmp);
              }


              --
              Ground21_______ _____\\
              ground21@poczta KROPKAfm

              Comment

              • red floyd

                #8
                Re: Read line after line but from the eof.

                Ground21 wrote:
                Rolf Magnus napisał(a):
                >
                >Yup. What you should do is read the file line by line and put each
                >line as a
                >separate string into a vector. So later you have a vector of lines. Then
                >you can iterate through that vector backwards and write the strings out
                >again.
                >
                can You tell me what's wrong with this code?:
                (I want to save lines from file 'file' to vectors, and then, write them
                into "tmp.txt"). I got wrong output "tmp.log".. . I think that is problem
                with char*...
                >
                #include <vector.h>
                vector <char*tab(10) ;
                >
                tmp=fopen("tmp. txt","w");
                fseek(file, 0L, SEEK_SET);
                for(int i=0; i<lines; i++)
                {
                fgets(tab[i],256,file);
                }
                for(int i=lines; i>0; i--)
                {
                fputs(tab[i],tmp);
                }

                Lets see...

                1. What is "lines"?
                2. What happens if there are more than 10 lines in a file?
                3. What happens if a line is bigger than 256?
                4. Why are you using arrays and C-style I/O?
                5. What is <vector.h>? The C++ header is <vector>
                6. Every entry in your vector is identical, since you're storing the
                same address over and over.

                And that's just off the top of my head.

                Comment

                • Ground21

                  #9
                  Re: Read line after line but from the eof.

                  red floyd napisał(a):
                  1. What is "lines"?
                  int lines=0;
                  fseek(fileh,0L, SEEK_SET);
                  while(feof(file h)==0) if(fgetc(fileh) =='\n') linies++;

                  lines - number of lines in my text file.
                  2. What happens if there are more than 10 lines in a file?
                  I read in wikibooks, that value of size will automaticlly increase if
                  necessary...

                  3. What happens if a line is bigger than 256?
                  I know, that it can be done better using something like strlen to check
                  line lenght, but for me, 256 is OK.
                  4. Why are you using arrays and C-style I/O?
                  5. What is <vector.h>? The C++ header is <vector>
                  in c++ builder 6 it works the same for <vectorand <vector.h>...
                  6. Every entry in your vector is identical, since you're storing the
                  same address over and over.

                  I though, that I take first line to tab[0], second line to tab[1]...



                  --
                  Ground21_______ _____\\
                  ground21@poczta KROPKAfm

                  Comment

                  • r

                    #10
                    Re: Read line after line but from the eof.

                    Ground21 wrote:
                    >
                    can You tell me what's wrong with this code?:
                    (I want to save lines from file 'file' to vectors, and then, write them
                    into "tmp.txt"). I got wrong output "tmp.log".. . I think that is problem
                    with char*...
                    >
                    #include <vector.h>
                    vector <char*tab(10) ;
                    >
                    tmp=fopen("tmp. txt","w");
                    fseek(file, 0L, SEEK_SET);
                    for(int i=0; i<lines; i++)
                    {
                    fgets(tab[i],256,file);
                    }
                    for(int i=lines; i>0; i--)
                    {
                    fputs(tab[i],tmp);
                    }
                    It's a bad mix of C and C++, besides the "Access violation".

                    // reading a file line by line
                    std::ifstream ifs ("tmp.txt");
                    std::string line;
                    std::vector<std ::stringlines;
                    while (getline (ifs, line)) {
                    lines.push_back (line);
                    }

                    Is this already in the C++ FAQ?

                    Comment

                    • Rolf Magnus

                      #11
                      Re: Read line after line but from the eof.

                      Ground21 wrote:
                      red floyd napisał(a):
                      >
                      >1. What is "lines"?
                      int lines=0;
                      fseek(fileh,0L, SEEK_SET);
                      while(feof(file h)==0) if(fgetc(fileh) =='\n') linies++;
                      >
                      lines - number of lines in my text file.
                      >
                      >2. What happens if there are more than 10 lines in a file?
                      >
                      I read in wikibooks, that value of size will automaticlly increase if
                      necessary...
                      Yes, if you use the push_back member function.
                      >3. What happens if a line is bigger than 256?
                      >
                      I know, that it can be done better using something like strlen to check
                      line lenght, but for me, 256 is OK.
                      It would be better as well as easier to use std::string instead of raw char
                      arrays and pointers.
                      >4. Why are you using arrays and C-style I/O?
                      >5. What is <vector.h>? The C++ header is <vector>
                      >
                      in c++ builder 6 it works the same for <vectorand <vector.h>...
                      Well, then you better stick to the standard header <vector>, otherwise your
                      program will be non-portable.
                      >6. Every entry in your vector is identical, since you're storing the
                      >same address over and over.
                      >
                      >
                      I though, that I take first line to tab[0], second line to tab[1]...
                      You could try something based on this (untested):

                      #include <istream>
                      #include <ostream>
                      #include <string>
                      #include <vector>

                      void reverse_copy_fi le(std::istream & infile, std::ostream& outfile)
                      {
                      typedef std::vector<std ::string vec_type;
                      vec_type text;

                      std::string line;
                      while (std::getline(i nfile, line))
                      text.push_back( line);

                      vec_type::const _reverse_iterat or it = text.rbegin(), end = text.rend();
                      for (; it != end; ++it)
                      outfile << line << '\n';
                      }


                      Comment

                      • Ground21

                        #12
                        Re: Read line after line but from the eof.

                        r wrote:
                        // reading a file line by line
                        std::ifstream ifs ("tmp.txt");
                        std::string line;
                        std::vector<std ::stringlines;
                        while (getline (ifs, line)) {
                        lines.push_back (line);
                        }
                        >
                        Is this already in the C++ FAQ?
                        [C++ Error] zapis.cpp(136): E2285 Could not find a match for
                        'getline<_CharT ,_Traits,_Alloc >(FILE *,string)'

                        --
                        Ground21_______ _____\\
                        ground21@poczta KROPKAfm

                        Comment

                        • red floyd

                          #13
                          Re: Read line after line but from the eof.

                          Ground21 wrote:
                          r wrote:
                          >
                          >// reading a file line by line
                          >std::ifstrea m ifs ("tmp.txt");
                          >std::string line;
                          >std::vector<st d::stringlines;
                          >while (getline (ifs, line)) {
                          > lines.push_back (line);
                          >}
                          >>
                          >Is this already in the C++ FAQ?
                          >
                          [C++ Error] zapis.cpp(136): E2285 Could not find a match for
                          'getline<_CharT ,_Traits,_Alloc >(FILE *,string)'
                          >
                          That's because you didn't copy the whole example. He used an ifstream
                          for input, not a FILE*.

                          Use C++ style I/O if you're writing C++.

                          Comment

                          • BobR

                            #14
                            Re: Read line after line but from the eof.


                            Ground21 wrote in message ...
                            >Rolf Magnus napisał(a):
                            >Yup. What you should do is read the file line by line and put each line as
                            a
                            >separate string into a vector. So later you have a vector of lines. Then
                            >you can iterate through that vector backwards and write the strings out
                            >again.
                            >
                            >can You tell me what's wrong with this code?:
                            >(I want to save lines from file 'file' to vectors, and then, write them
                            >into "tmp.txt"). I got wrong output "tmp.log".. . I think that is problem
                            >with char*...
                            >
                            // >#include <vector.h>
                            #include <vector// C++
                            >vector <char*tab(10) ;
                            tmp=fopen("tmp. txt","w");
                            fseek(file, 0L, SEEK_SET);
                            for(int i=0; i<lines; i++){
                            fgets(tab[i],256,file);
                            }
                            for(int i=lines; i>0; i--){
                            fputs(tab[i],tmp);
                            }

                            #include <vector// C++
                            #include <string>
                            #include <fstream>
                            #include <algorithm>

                            int main(){
                            std::ifstream copin( "ZZtest.txt " );
                            if( not copin.is_open() ){ throw "a fit";}

                            std::vector<std ::stringTmpFile ;
                            for( std::string line; std::getline( copin, line ); ){
                            TmpFile.push_ba ck(line);
                            }
                            // copin.close();
                            std::reverse( TmpFile.begin() , TmpFile.end() ); // reverse it
                            std::ofstream out( "ZZtestR.tx t" );
                            for( size_t i(0); i < TmpFile.size(); ++i ){
                            std::reverse( TmpFile.at(i).b egin(), TmpFile.at(i).e nd());
                            out<< TmpFile.at( i ) <<std::endl;
                            }
                            // out.close();
                            } // main()
                            // ----------
                            // - contents "ZZtest.txt " -
                            Record 0
                            Record 1
                            Record 2
                            Record 3
                            Record 4
                            Record 5
                            Record 6

                            // - contents "ZZtestR.tx t" -
                            6 droceR
                            5 droceR
                            4 droceR
                            3 droceR
                            2 droceR
                            1 droceR
                            0 droceR

                            --
                            Bob R
                            POVrookie


                            Comment

                            • Tobias Gneist

                              #15
                              Re: Read line after line but from the eof.

                              Ground21 wrote:
                              Hello.
                              How could I read the whole text file line after line from the end of
                              file? (I want to ~copy~ file)
                              >
                              I want to copy file in this way:
                              >
                              file 1:
                              >
                              a
                              b
                              c
                              >
                              >
                              file 2:
                              >
                              c
                              b
                              a
                              >
                              >
                              I think the most simple solution would use a stack an C++ I/O.

                              #include <iostream>
                              #include <fstream>
                              #include <stack>
                              #include <string>

                              using namespace std;

                              int main()
                              {
                              ifstream in("in.txt");
                              ofstream out("out.txt");
                              stack<stringss;

                              string s;
                              in >s;
                              while (!in.eof()) {
                              ss.push(s);
                              in >s;
                              }
                              while (!ss.empty()) {
                              out << ss.top() << endl;
                              ss.pop();
                              }
                              return 0;
                              }

                              Comment

                              Working...