setting read and write point

Discussion in 'C++' started by John Flynn, Aug 26, 2004.

  1. John Flynn

    John Flynn Guest

    hi,

    having problems reading from and writing back to the same file.

    basically, i want to read lines of text from a file and reverse them and
    write them back to the same file.. it has to replace the text its reversing


    eg.

    the quick brown fox
    jumped over the
    lazy dog

    .....becomes..

    xof nworb kciuq eht
    eht revo depmuj
    god yzal


    following is what i've got so far.. its all over the place.. for me its a
    lot of trial and error.. hence all the code that i've made into comments
    (//)
    any help would be greatly appreciated

    ********************************************************************

    #include <iostream>
    #include <fstream>

    using namespace std;



    /***********************
    * Main execution
    ***********************/
    int main()//int argc, char* argv[])
    {
    // if (argc != 2)
    {
    // cout << "\nUsage: rever file\n\n";
    // return 1;
    }

    // string fileName(argv[1]);
    string readLine;
    string tmp;
    fstream stream;
    fstream outStream;
    long readPos, writePos;
    stream.open("fileName.txt");
    //outStream.open("fileName.txt");
    //outStream.open("fileName.txt");

    if (!stream.is_open())
    {
    cout << "Unable to open: " << "fileName" << endl;
    //return 1;
    }
    readPos = 0;
    writePos = 0;
    for (;;)
    {

    // writePos = stream.tellp();

    //stream.seekg(readPos, ios::beg);
    getline(stream, readLine);

    readPos = stream.tellg();

    for (int i = readLine.length() - 1; i >= 0; i-- )
    {
    tmp = tmp + readLine;
    }

    // stream.seekp(writePos, ios::beg);

    // outStream << readPos << endl;



    cout << readPos << "and "<< writePos;
    cin >> tmp;


    if (stream.eof()) break;
    tmp = "";
    }




    }
     
    John Flynn, Aug 26, 2004
    #1
    1. Advertising

  2. John Flynn wrote:

    > hi,
    >
    > having problems reading from and writing back to the same file.
    >
    > basically, i want to read lines of text from a file and reverse them and
    > write them back to the same file.. it has to replace the text its reversing
    >
    >
    > eg.
    >
    > the quick brown fox
    > jumped over the
    > lazy dog
    >
    > ....becomes..
    >
    > xof nworb kciuq eht
    > eht revo depmuj
    > god yzal
    >
    >
    > following is what i've got so far.. its all over the place.. for me its a
    > lot of trial and error.. hence all the code that i've made into comments
    > (//)
    > any help would be greatly appreciated



    Far too complex. Why not read it all in and then write it all out in
    reverse order?

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

    int main(){
    using namespace std;
    istreambuf_iterator<char> first(cin), last;
    vector<char> v(first, last);
    copy(v.rbegin(), v.rend(), ostreambuf_iterator<char>(cout));
    }


    Jacques.
     
    Jacques Labuschagne, Aug 26, 2004
    #2
    1. Advertising

  3. John Flynn

    Sam Holden Guest

    On Thu, 26 Aug 2004 21:28:41 +1200,
    Jacques Labuschagne <> wrote:
    > John Flynn wrote:
    >
    >> hi,
    >>
    >> having problems reading from and writing back to the same file.
    >>
    >> basically, i want to read lines of text from a file and reverse them and
    >> write them back to the same file.. it has to replace the text its reversing
    >>
    >>
    >> eg.
    >>
    >> the quick brown fox
    >> jumped over the
    >> lazy dog
    >>
    >> ....becomes..
    >>
    >> xof nworb kciuq eht
    >> eht revo depmuj
    >> god yzal
    >>
    >>
    >> following is what i've got so far.. its all over the place.. for me its a
    >> lot of trial and error.. hence all the code that i've made into comments
    >> (//)
    >> any help would be greatly appreciated

    >
    >
    > Far too complex. Why not read it all in and then write it all out in
    > reverse order?


    Because that results in output completely unrelated to the specified
    required output?

    And ignores the "same file" requirement that seems rather important?


    > #include <vector>
    > #include <iostream>
    > #include <iterator>
    > #include <algorithm>
    >
    > int main(){
    > using namespace std;
    > istreambuf_iterator<char> first(cin), last;
    > vector<char> v(first, last);
    > copy(v.rbegin(), v.rend(), ostreambuf_iterator<char>(cout));
    > }


    #include <cstdio>
    #include <stack>

    using namespace std;

    void in_place_line_reverse(FILE *file) {
    stack<int> line;
    int c;
    fpos_t start_of_line;
    fpos_t end_of_line;

    fgetpos(file, &start_of_line);
    while ((c=getc(file)) != EOF) {
    if (c=='\n') {
    fgetpos(file, &end_of_line);
    fsetpos(file, &start_of_line);
    while (line.size() > 0) {
    putc(line.top(), file);
    line.pop();
    }
    fsetpos(file, &end_of_line);
    fgetpos(file, &start_of_line);
    } else {
    line.push(c);
    }
    }
    }

    int main() {
    FILE *file;
    file = fopen("text.txt", "r+");
    in_place_line_reverse(file);
    }


    But it's more C than C++ :)

    And needs to double in length in order to actually perform
    error checking on all those functions that can fail :(


    --
    Sam Holden
     
    Sam Holden, Aug 26, 2004
    #3
  4. John Flynn

    Kai-Uwe Bux Guest

    Sam Holden wrote:

    > On Thu, 26 Aug 2004 21:28:41 +1200,
    > Jacques Labuschagne <> wrote:
    >> John Flynn wrote:
    >>
    >>> hi,
    >>>
    >>> having problems reading from and writing back to the same file.
    >>>
    >>> basically, i want to read lines of text from a file and reverse them and
    >>> write them back to the same file.. it has to replace the text its
    >>> reversing
    >>>
    >>>
    >>> eg.
    >>>
    >>> the quick brown fox
    >>> jumped over the
    >>> lazy dog
    >>>
    >>> ....becomes..
    >>>
    >>> xof nworb kciuq eht
    >>> eht revo depmuj
    >>> god yzal
    >>>
    >>>
    >>> following is what i've got so far.. its all over the place.. for me its
    >>> a lot of trial and error.. hence all the code that i've made into
    >>> comments (//)
    >>> any help would be greatly appreciated

    >>
    >>
    >> Far too complex. Why not read it all in and then write it all out in
    >> reverse order?

    >
    > Because that results in output completely unrelated to the specified
    > required output?
    >
    > And ignores the "same file" requirement that seems rather important?
    >
    >
    >> #include <vector>
    >> #include <iostream>
    >> #include <iterator>
    >> #include <algorithm>
    >>
    >> int main(){
    >> using namespace std;
    >> istreambuf_iterator<char> first(cin), last;
    >> vector<char> v(first, last);
    >> copy(v.rbegin(), v.rend(), ostreambuf_iterator<char>(cout));
    >> }

    >
    > #include <cstdio>
    > #include <stack>
    >
    > using namespace std;
    >
    > void in_place_line_reverse(FILE *file) {
    > stack<int> line;
    > int c;
    > fpos_t start_of_line;
    > fpos_t end_of_line;
    >
    > fgetpos(file, &start_of_line);
    > while ((c=getc(file)) != EOF) {
    > if (c=='\n') {
    > fgetpos(file, &end_of_line);
    > fsetpos(file, &start_of_line);
    > while (line.size() > 0) {
    > putc(line.top(), file);
    > line.pop();
    > }
    > fsetpos(file, &end_of_line);
    > fgetpos(file, &start_of_line);
    > } else {
    > line.push(c);
    > }
    > }
    > }
    >
    > int main() {
    > FILE *file;
    > file = fopen("text.txt", "r+");
    > in_place_line_reverse(file);
    > }
    >
    >
    > But it's more C than C++ :)
    >
    > And needs to double in length in order to actually perform
    > error checking on all those functions that can fail :(
    >
    >


    What about:

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

    typedef std::string Line;
    typedef std::vector<Line> Text;

    int main( int argn, char ** args ){
    Text text;
    { // read:
    std::fstream in_file ( args[1] );
    Line line;
    while ( std::getline( in_file, line ) ) {
    text.push_back( line );
    }
    in_file.close();
    }
    { // write:
    std::fstream out_file ( args[1] );
    std::eek:streambuf_iterator<char> out_iter ( out_file );
    for ( Text::const_iterator line_iter = text.begin();
    line_iter != text.end(); ++ line_iter ) {
    std::copy(line_iter->rbegin(), line_iter->rend(), out_iter );
    *out_iter = '\n';
    ++ out_iter;
    }
    }
    }


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Aug 26, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sanja Tanasijevic
    Replies:
    0
    Views:
    484
    Sanja Tanasijevic
    Apr 11, 2006
  2. Malcolm McLean

    portable floating-point read/write

    Malcolm McLean, Feb 12, 2010, in forum: C Programming
    Replies:
    11
    Views:
    947
    Barry Schwarz
    Feb 13, 2010
  3. Saraswati lakki
    Replies:
    0
    Views:
    1,389
    Saraswati lakki
    Jan 6, 2012
  4. sahm
    Replies:
    4
    Views:
    706
  5. Tim Chase
    Replies:
    0
    Views:
    103
    Tim Chase
    Dec 16, 2013
Loading...

Share This Page