Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 28th, 2007, 7:09 PM   #1
jasonfrost
Newbie
 
Join Date: Aug 2007
Posts: 16
Rep Power: 0 jasonfrost is on a distinguished road
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;
}
jasonfrost is offline   Reply With Quote
Old Sep 28th, 2007, 8:07 PM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Sep 29th, 2007, 4:51 AM   #3
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3 Soulstorm is on a distinguished road
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.
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Oct 3rd, 2007, 9:02 AM   #4
Lesliect6
Programmer
 
Join Date: Aug 2005
Posts: 68
Rep Power: 4 Lesliect6 is on a distinguished road
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.
Lesliect6 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:46 AM.

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