Using Vectors

E

edward_nig

Hi all,

Please I tried to copy from a text file line by line and to paste the
text in reverse order in another text file also line by line-Making the
first line the last and the last line the first.

I tried using vectors in the following codes but it just won't run.
please help

------------------------------------------

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main() {
vector<string> v;
ifstream in("mtext1.txt");
ofstream out("mtext2.txt");

string line;

while (getline (in, line) ) {
v.push_back(line);
}
int j = v.size();

for(int i=0; i < j; i++) {
out << v[j] ;
j=j-1;
}
}
 
G

Gianni Mariani

edward_nig said:
Hi all,

Please I tried to copy from a text file line by line and to paste the
text in reverse order in another text file also line by line-Making the
first line the last and the last line the first.

I tried using vectors in the following codes but it just won't run.
please help

------------------------------------------

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main() {
vector<string> v;
ifstream in("mtext1.txt");
ofstream out("mtext2.txt");

string line;

while (getline (in, line) ) {
v.push_back(line);
}
int j = v.size();

int j = v.size() - 1;
for(int i=0; i < j; i++) {

the for statement makes no sense.
out << v[j] ;
j=j-1;
}
}

try ...
for ( int j = v.size(); j-- > 0; ) {

and remove the j=j-1;



for(int i=0; i < j; i++) {
 
E

edward_nig

Hi,

It still won't work. It transfers the texts line by line to the second
file, but it still wasn't done in reverse other.
 
C

Cy Edmunds

edward_nig said:
Hi all,

Please I tried to copy from a text file line by line and to paste the
text in reverse order in another text file also line by line-Making the
first line the last and the last line the first.

I tried using vectors in the following codes but it just won't run.
please help
[snip]

Traversing a std::vector in reverse order using indexes can be confusing.
However there is no need to do it that way:

void
reverse()
{
typedef std::vector<std::string> t_vec;
t_vec v;
std::ifstream in("mtext1.txt");
std::eek:fstream out("mtext2.txt");

std::string line;

while (getline(in, line))
v.push_back(line);

for (t_vec::reverse_iterator i = v.rbegin(); i != v.rend(); ++i)
out << *i << '\n';
}
 
E

edward_nig

Thanks Cy,

But I am compiling the following. I use Codeblocks IDE which uses gcc
and my input text still is the same same as my output text.
-------------------------------------------

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main() {

typedef std::vector<std::string> t_vec;
t_vec v;
std::ifstream in("mtext1.txt");
std::eek:fstream out("mtext2.txt");

std::string line;

while (getline(in, line))
v.push_back(line);

for (t_vec::reverse_iterator i = v.rbegin(); i != v.rend(); ++i)
out << *i << '\n';

}
 
R

red floyd

edward_nig said:
Thanks Cy,

But I am compiling the following. I use Codeblocks IDE which uses gcc
and my input text still is the same same as my output text.
-------------------------------------------

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main() {

typedef std::vector<std::string> t_vec;
t_vec v;
std::ifstream in("mtext1.txt");
std::eek:fstream out("mtext2.txt");

std::string line;

while (getline(in, line))
v.push_back(line);

for (t_vec::reverse_iterator i = v.rbegin(); i != v.rend(); ++i)
out << *i << '\n';

}

---------------------------
Note: This is the content of my mtext1.txt
---
There is a new concept introduced here.which is the while loop.Although
this will be explained in detail in the next chapter

I expect it to give output like this in mtext2.txt

std::getline reads an entire line (amazingly enought), thus your output
should be:

this will be explained in detail in the next chapter
There is a new concept introduced here.which is the while loop.Although
 
D

Daniel T.

"edward_nig said:
Same thing.

Are you trying to say that Cy Edmunds code didn't work? If not, then you
have problems with your computer rather than your code...

Try this one:

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

using namespace std;

int main() {
stack<string> lines;

ifstream in( "mtext1.txt" );
string line;
while ( getline( in, line ) )
lines.push( line );

ofstream out( "mtext2.txt" );
while ( ! lines.empty() ) {
out << lines.top() << '\n';
lines.pop();
}
}
 
E

edward_nig

std::getline reads an entire line (amazingly enough),

Yeah i agree, that thought output was my mistake.
 
E

edward_nig

I am grateful.

Cy Edmunds codes worked! Daniel T's too did, but not until I modified
the content of the notepad file mtext1. I added a page break! at the
end of the first sentence and then added a break to the middle of the
second sentence.

I don't know why it was like that, but will be glad to know after
taking you guys through this, all the way.

Thanks.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Filter sober in c++ don't pass test 0
Chatbot 0
TF-IDF 2
Crossword 2
Crossword 14
How to multiply two matrices of size in using inline assembly in C++ 3
vectors 19
vector of vectors 3

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top