trouble following psedocode

I'm doing a lesson for random access with files. They gave this pseudocode

Write a program that reads each line in a file, reverses its characters, and writes the resulting line to the same file. Use the following pseduocode:

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.


This is how i interpreted the pseudocode, which outputs the file missing the first character from the in.txt and does not reverse the letters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
	string line;
	ifstream input("in.txt");
	ofstream output("out.txt");
	int pos1 = 0;
	int pos2 = 0;
	char ch;
	while (input.get(ch))
	{
		pos1 = input.tellg();
	
		if (input)
		{
			pos2 = input.tellg();
			output.seekp(pos1);
			input.get(ch);
			output << ch;
			input.seekg(pos2);
		}

	}


	
}


This is the in.txt file's txt im using:


Mary had a little lamb
Its fleece was white as snow
And everywhere that Mary went
The lamb was sure to go.



and here's the out.file I am currently getting:

ary had a little lamb
Its fleece was white as snow
And everywhere that Mary went
The lamb was sure to go.






while (input.get(ch))
{
pos1 = input.tellg();
here, you have skipped the first letter in your tellg; you already consumed it and moved the position one notch. use a do while maybe:
do
pos1 = ..
...
while(input.get(ch)); //here, you got the position before you moved...

you can do getline on a file same as cin, put that in a c++ string, and use c++'s tools to reverse it if that is allowed. If it is not allowed, go ahead with your letter by letter approach.
Last edited on
still can't get it to reverse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
	ifstream line("in.txt");
	ofstream outline("out.txt");
	char ch;
	int pos1 = 0;
	int pos2 = 0;
	while (!line.fail())
	{
		pos1 = line.tellg();
		line.get(ch);
		if (line)
		{
			pos2 = line.tellg();
			outline.seekp(pos1);
			outline << ch;
			line.seekg(pos2);
		}
	}


}
It says read line by line, and without reversing it this gets your program going:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::string line;
    std::ifstream input("in.txt");
    
    std::streampos pos1 = 0;
    std::streampos pos2 = 0;
    
    while (pos2 != -1)
    {
        pos1 = input.tellg();
        std::getline(input, line);
        std::cout
        << "Start: " << pos1 << " \tSize: " << line.length() << ' ' << line << '\n';
        
        pos2 = input.tellg();
    }    
    std::cout << "pos1: " << pos1 << '\n';
}



Start: 0 	Size: 22 Mary had a little lamb
Start: 23 	Size: 28 Its fleece was white as snow
Start: 52 	Size: 29 And everywhere that Mary went
Start: 82 	Size: 24 The lamb was sure to go.
Start: 107 	Size: 0 
pos1: 107
Program ended with exit code: 0


There are several important points:

1. The file is read line by line.
2. The line then has to be reversed.
3. The reversed line is then put back to overwrite the original line. (2 & 3 can be done as one operation)
4. There is only one single text file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::string line;
    size_t size{0};
    
    std::fstream input("in.txt");
    
    long pos1 = 0;
    long pos2 = 0;
    
    while (pos2 != -1)
    {
        pos1 = input.tellg();
        std::getline(input, line);
        size = line.length();
        
        std::cout
        << "Start: " << pos1 << " \tSize: " << size << ' ' << line << '\n';
        
        for(int i = 0; i < size; ++i)
        {
            input.seekp(pos1 + i , std::ios::beg);
            input.write( &line[size - i - 1], 1);
        }
        pos2 = input.tellg();
    }
    std::cout << "pos1: " << pos1 << '\n';
}
Topic archived. No new replies allowed.