Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   reading from file, and vectors question (http://www.programmingforums.org/showthread.php?t=14058)

jasonfrost Sep 28th, 2007 7:09 PM

reading from file, and vectors question
 
Hi

I am working on this program that needs to read in multiple lines from a file (in this case, 3) and prints the output. Then I need to print the text in reverse. I need to use vectors but I could find anything good to go off of. I am really not sure how I am to go about this. Can vectors do all this? I am assuming I will have to use strings...I think.

My file that it is reading from looks like this:

This is the first line of text.
This is the second line of text.
This is the third line of text.

Not sure if it will help, but here's my code I have so far. Any advice or pointers to other information would be great.

:

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

int main()
{
string text;
int foo;

ifstream myFile;
myFile.open("infile.txt", ios::in);
myFile >> text;
//ifstream myFile("infile.txt");
myFile.close();
cin >> foo;
        return 0;
}


grumpy Sep 28th, 2007 8:07 PM

using operator >> will get a token delimited by whitespace i.e. a single word. If you really want to read one line at a time, look up the getline() method of the std::istream class.

Vectors do not, in themselves, do what you want. To get the data from the file, you need some form of looping (eg repetitively read a line and push it into the vector) until end of file is reached.

You need to clarify what you mean by printing the text in reverse. Do you mean printing the last line first and first line last? Do you mean reversing the characters in each line? Do you mean printing all of the characters in the file in reverse order? All of those can be done using operations on vectors and/or strings but different technique is suited to each one.

Soulstorm Sep 29th, 2007 4:51 AM

This is code from a class implemented into one of my frameworks. I hope it helps


:

#define std::string stdString
#define std::vector stdVector

void Floader::loadDataFromFile(const char *fileName){
        int i;
        resetData();
        stdString line;
        fstream myFile(fileName);
        for (i=0; myFile.good(); i++) {
                getline(myFile,line);
                loadedData.push_back(line);
        }
       
        myFile.close();
        stdCout << "Process completed\n";
}

The loadedData is a vector holding strings.

This will read a file and will make the vector hold each line of the file. I hope it helps.

Lesliect6 Oct 3rd, 2007 9:02 AM

If you need to print the sentences backwards, it would be quite convinient to read byte per byte the three lines, and store each byte in a stack class.

This way, when you have finished reading every line, you can simply pop() byte per byte the words of the lines in a reverse order.


All times are GMT -5. The time now is 12:44 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC