Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Need a little help

I need a code that will open a .txt file and display its content to the screen. Say the .txt filename was start.txt. What would the code be to make this happen please?
[167 byte] By [C++Monkey] at [2007-11-11 7:45:03]
# 1 Re: Need a little help
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream file ("start.txt");
if (file.is_open())
{
while (!file.eof() )
{
getline (file,line);
cout << line << endl;
}
file.close();
}

else cout << "Unable to open file";

return 0;
}
Code_Nerd at 2007-11-11 21:01:59 >
# 2 Re: Need a little help
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream file ("start.txt");
if (file.is_open())
{
while (!file.eof() )
{
getline (file,line);
cout << line << endl;
}
file.close();
}

else cout << "Unable to open file";

return 0;
}
You could have just posted the link :rolleyes:.
File I/O (http://www.cplusplus.com/doc/tutorial/files.html)
destin at 2007-11-11 21:02:59 >
# 3 Re: Need a little help
I have no problem referencing sources..

However this is how I would do it.. :)
Code_Nerd at 2007-11-11 21:04:04 >
# 4 Re: Need a little help
Thanks a million.. Do the txt files need to be in the same folder as the program? If it doesnt how do i make it so its in seperate folders?Also thanks for the link
C++Monkey at 2007-11-11 21:05:04 >
# 5 Re: Need a little help
ifstream file ("start.txt");

No the above line needs to be a direct link to the files location..
Code_Nerd at 2007-11-11 21:06:03 >
# 6 Re: Need a little help
Ok now I have another question. How do I change the color of the text?(if possible) And positioning according to pixels etc etc
C++Monkey at 2007-11-11 21:07:02 >
# 7 Re: Need a little help
You can change font colour with SetConsoleTextAttribute()..
#include <windows.h>
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_GREEN);
Will make output green.. FOREGROUND_BLUE will make text blue etc etc..

As far as positioning, you can use setw to format your output, it will be difficult if your reading in an entire file..
Code_Nerd at 2007-11-11 21:08:01 >
# 8 Re: Need a little help
Ok. I am trying to open two files of text in the same program..I have used namespaces for the string line;

Now its erroring saying im redefining ifstream myfile ("filenamehere.txt");

Thanks for the color help.

Any way to fix this?
C++Monkey at 2007-11-11 21:09:04 >
# 9 Re: Need a little help
A more detailed description would be...opening one file as an introductory. The next file is a log in system (complicated) but I cannot open the .txt file that contains the information. It says Im redefining it but i cannot put the ifstream myfile ("filenamehere.txt"); into a namespace. please help
C++Monkey at 2007-11-11 21:10:07 >
# 10 Re: Need a little help
If you could post some code it would help greatly!!
Code_Nerd at 2007-11-11 21:11:08 >
# 11 Re: Need a little help
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

namespace intro
{
string line;
}
namespace login
{
string line;
string playername;
}

int main()
{
intro::line;
ifstream myfile ("intro.txt");
if (myfile.is_open())
{
while (! myfile.eof())
{
getline (myfile,intro::line);
cout<<intro::line<<endl;
}
myfile.close();
}
system ("PAUSE");

login::line;
ifstream myfile ("playername.txt");
if (myfile.is_open())
{
while (! myfile.eof())
{
getline (myfile,login::line);
if (login::line = " ")
{
cout<<"A deep and hollow voice says: I see you are new here"<<endl;
cout<<"Please enter a player name: ";
cin>>login::playername;
myfile<<login::playername<<endl;
}
}
}
system ("PAUSE");
}

If you can't tell I am making a single-player text based game and came across a few errors :P

ifstream myfile ("playername.txt");

That is what is redefined. I just need to know how to solve that.. :p I know it looks wierd with the login system but it actually works for a single player game.

Also I didn't understand, Can I have a folder and then the program goes to the folder and opens the files there? If so can you post a bit of code for me?

Honors Call.cpp:32: error: redeclaration of `std::ifstream myfile'
Honors Call.cpp:19: error: `std::ifstream myfile' previously declared here
Honors Call.cpp:38: error: could not convert `(&login::line)->std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" "))' to `bool'
Honors Call.cpp:43: error: no match for 'operator<<' in 'myfile << login::playername'

That is what exactly the compiler said went wrong
C++Monkey at 2007-11-11 21:12:13 >
# 12 Re: Need a little help
The second time you use myfile you do not need to redefine is as ifstream myfile ("intro.txt");

You just use it myfile ("playername.txt");
Code_Nerd at 2007-11-11 21:13:11 >
# 13 Re: Need a little help
That didnt seem to work. :confused:
C++Monkey at 2007-11-11 21:14:12 >