CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2012
    Posts
    15

    Problem with reversing a string using seek/tell

    Hello guys,

    I have an assignment that asks me to write a program which reads all lines from a text file (one by another), reverses them and write to the same file. I also have a pseudocode with which I have to work and it is as follows:
    While the end of the file has not been reached
    pos1 = current get position
    Read a line.
    If the line was successfully read
    pos2 = current get position
    Set put position to pos1.
    Write the reversed line.
    Set get position to pos2.

    I have tried many things and the code misses letters. Below you can see my code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        fstream file;
        file.open("output.txt");
    
        while (!file.eof())
        {
            int pos1 = file.tellg();
            string capture;
            getline (file, capture);
            if (file.fail()) { return 0; }
            int pos2 = file.tellg();
            file.seekp(pos1);
            for (int i = capture.length() - 1; i >= 0; i--)
            {
                string ch = capture.substr(i, 1);
                file << ch;
            }
            file << '\n';
            file.seekg(pos2);
        }
    
        file.close();
        system("PAUSE");
        return 0;
    }
    This is the text with which I have to work:
    Mary had a little lamb
    Its fleece was white as snow
    And everywhere that Mary went
    The lamb was sure to go.

    And this is what I get:
    bmal elttil a dah yraM
    Itswons sa etihw saw eceelf
    Antnew yraM taht erehwyreve d
    T.og ot erus saw bmal eh


    Please help me on this.

    Your help is much appreciated.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with reversing a string using seek/tell

    Quote Originally Posted by Symus View Post
    Hello guys,

    I have an assignment that asks me to write a program which reads all lines from a text file (one by another), reverses them and write to the same file. I also have a pseudocode with which I have to work and it is as follows:
    While the end of the file has not been reached
    pos1 = current get position
    Read a line.
    If the line was successfully read
    pos2 = current get position
    Set put position to pos1.
    Write the reversed line.
    Set get position to pos2.
    The assignment is much simpler than that psuedocode:
    Code:
    For each line in the input file:
    1) Read a line fron input file into a string
    2) Reverse the string 
    3) Write the string to output file
    All of these calls to seekg(), seekp() and whatever else are not necessary. This is basically a 2 or 3 line while loop.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jul 2012
    Posts
    15

    Re: Problem with reversing a string using seek/tell

    Thank you Paul,

    But the assignment requires to use these pointers and I think that my problem is right there. I think my positions are not correct.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with reversing a string using seek/tell

    Quote Originally Posted by Symus View Post
    Thank you Paul,

    But the assignment requires to use these pointers and I think that my problem is right there. I think my positions are not correct.
    First, you are reading a text file using the automatic translation of carriage return/line feeds into a single character. Using functions such as seekg and tellg() are not reliable in this mode, unless you're seeking to the end or the beginning of the file. That's why you're not getting the correct output -- all of those offsets are skewed because of the CR/LF translation done in text mode.

    So your assignment is wrong-headed to begin with. No one opens a file in text mode, and tries to do this type of processing within it.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jul 2012
    Posts
    15

    Re: Problem with reversing a string using seek/tell

    Quote Originally Posted by Paul McKenzie View Post
    First, you are reading a text file using the automatic translation of carriage return/line feeds into a single character. Using functions such as seekg and tellg() are not reliable in this mode, unless you're seeking to the end or the beginning of the file. That's why you're not getting the correct output -- all of those offsets are skewed because of the CR/LF translation done in text mode.

    So your assignment is wrong-headed to begin with. No one opens a file in text mode, and tries to do this type of processing within it.

    Regards,

    Paul McKenzie
    I see. So is there a way to do this with seek/tell. Could also point me the easiest way of reversing a string?

    Thank you in advance.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Problem with reversing a string using seek/tell

    Can you post the assignment exactly as it was worded? Generally you don't try to read and write to a text file at the same time. Typically, you'd write to a second file, then delete the input file when you're done and rename the output file. Alternatively, you'd store the output contents in memory, delete the input file then write the output from the strings stored in memory.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with reversing a string using seek/tell

    Quote Originally Posted by Symus View Post
    I see. So is there a way to do this with seek/tell.
    All lines in the file must be fixed length and not terminated by CR/LF. Your file is not set up this way -- it is variable line length, delimited with CR/LF.

    So are you going to walk on your hands (keep using seekg() and tellg()), or walk using your legs?
    Could also point me the easiest way of reversing a string?
    Code:
    #include <algorithm>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       string s = "abc123";
       reverse(s.begin(), s.end());
       cout << s;
    }
    http://www.cplusplus.com/reference/algorithm/reverse/

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Jul 2012
    Posts
    15

    Re: Problem with reversing a string using seek/tell

    Thanks for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured