|
-
August 8th, 2012, 10:43 AM
#1
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.
-
August 8th, 2012, 10:51 AM
#2
Re: Problem with reversing a string using seek/tell
 Originally Posted by Symus
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
-
August 8th, 2012, 11:14 AM
#3
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.
-
August 8th, 2012, 11:45 AM
#4
Re: Problem with reversing a string using seek/tell
 Originally Posted by Symus
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
-
August 8th, 2012, 11:52 AM
#5
Re: Problem with reversing a string using seek/tell
 Originally Posted by Paul McKenzie
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.
-
August 8th, 2012, 11:52 AM
#6
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.
-
August 8th, 2012, 11:58 AM
#7
Re: Problem with reversing a string using seek/tell
 Originally Posted by Symus
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
-
August 8th, 2012, 12:24 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|