![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2007
Posts: 16
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 234
Rep Power: 3
![]() |
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";
}This will read a file and will make the vector hold each line of the file. I hope it helps.
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Aug 2005
Posts: 68
Rep Power: 3
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem Associated with Vector Source code | buggytoast | Java | 3 | Apr 2nd, 2006 5:41 AM |
| Use Vectors To Calculate # Of Occurences | c_newbie | C++ | 17 | Dec 9th, 2005 8:38 AM |