Open a simple .txt file

Hey I am trying to find very basic code to simply open a file which I have created and display the information within it, all this is being done on a win32 console.

Any help would be most appreciated

Thanks
I have tried it but I don't want an if else statement in it I just want it to open and display thats all
I have tried it but I don't want an if else statement in it I just want it to open and display thats all
1
2
3
4
5
6
7
8
ifstream file("[path\\]filename.txt");//open the file for reading
char g;
while (true)
{
   file.get(g);//get the next character of the file
   if (file.eof()) break;//stop if the end of the file is reached
   cout << g;//display the character
}
Excellent do I simply copy and paste this in to the void main () area??
Excelent this code works, so to get an indervidual line from my .txt file woudl i use something like:
string name;
cin >> name;

if (name == "property")

{ int number;
int rent;
int cost;


}

cin >> number;
cin >> rent;


?? Most appreciated for your help guys
if you want to get a line from your file use getline:
1
2
string name;
getline ( file, name );
Excellent bazzy!! thank you, woudl you mind ever so much telling me where abouts I would place it in the code you wrote above?

thanks
When you want to get the line
(replace cin >> name with getline )
getline works as the istream >> operator but it reads until the next '\n' character (or any given character) instead of breaking input at the first whitespace
So I assume you mean cin >> name on the code you wrote above is actually cout << g??

or is that all wrong?
You posted some code with cin >> name ( http://www.cplusplus.com/forum/beginner/8386/#msg38791 ) so I presumed you wanted to get a line there.
cin >> name reads some text from the console as getline(cin,name) would do (they differ on when they stop reading)
But if you want to read from a file you should use the an ifstream ( file in the example I provided ) instead of cin

Maybe this code explains better how to use getline:
1
2
3
4
5
6
7
ifstream file("aFile.txt");
string line;
while (!file.eof())
{
    getline( file, line );//stores a line in 'line'
    cout << line << "\n\n"; //display the line and go newline twice
}

this example will read the lines from the file aFile.txt and display them separed by a blank line
Excellent it reads the file no problems and even puts a gap between each bit of info. So now how woudl I display a single line of that information?

I was tryingto use something like this:

string name;
cin >> name;

if (name == "property")

{
int number;
int rent;
int cost;
}

cin >> number;
cin >> rent;

system("pause");
}

but so far no joy. anyhelp would be great thanks
Right here is the code I have so far:

#include <stdafx.h>
#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
using namespace std;


void main ()
{
ifstream file("names.txt");
string line;
//while (!file.eof())
{
getline( file, line );//stores a line in 'line'
cout << line << "\n\n"; //display the line and go newline twice
}
system("pause");
}


and here is the file that i am reading from:

GO
Property 1 60 5 0
Property 2 80 10 0
Penalty
Property 3 100 15 1
Property 4 120 15 1
Jail
Property 5 140 20 2
Property 6 160 20 2
Property 7 180 25 3
Property 8 180 25 3
Property 9 200 25 3
Free Parking
Property 10 220 30 4
Property 11 240 30 4
Bonus
Property 12 260 35 5
Property 13 280 35 5
Go to Jail
Property 14 300 45 6
Property 15 320 45 6
Property 16 400 50 7
Property 17 420 50 7
Property 18 440 50 7

I now need to be able to direct to a single line or character, say show property 12 or asomething like that, so far I have been trying for just over a week and it's killing me!!!
You can try something like this:
1
2
3
4
5
6
7
8
9
ifstream file("File.txt");
string line;
string lookfor = "Property 12";
while (!file.eof())
{
    getline( file, line ); // get the line
    if ( line.substr(0,lookfor.length()) == lookfor ) // if the line starts with "Property 12"
		cout << line.substr(lookfor.length()) << '\n';//Display the rest of the line
}

This will display all the lines starting with "Property 12"
if you want to extract integers from the result ( " 260 30 4" ) you can use stringstreams:
1
2
3
4
5
6
7
if ( line.substr(0,lookfor.length()) == lookfor )
{
     stringstream ss(line.substr(lookfor.length()));
     int a, b, c;
     ss >> a >> b >> c;
     cout << "1st value = " << a << "\n2nd value = " << b << "\n3rd value = " << c;
}
Topic archived. No new replies allowed.