while(fin,str)
This is just plain wrong and makes no sense. I bet you meant
while(fin >>str)
.
Anyway,
char myFile = "c\\tempo\\gb.txt";
Let's examine this line of code. Start on the left.
char myFile
Ok, we're creating a char object. A single char object, named myFile. A quick refresher. What can you store in a single char object? One char. How many? One. Would it make any sense to try to store more than one char in a char object? No.
= "c\\tempo\\gb.txt";
And then we're trying to assign to that
single char whatever this is. Does this look like a single char? It does
not. What does this look like? Looks like a whole bunch of chars. Looks like some kind of array of chars. When you try to assign this, you get a pointer to the first char.
So let's look at that error message.
invalid conversion from 'const char*' to 'char' |
You're trying to assign a char-pointer to a char object, and what does this error message say? It says that the compiler won't convert a char-pointer to a char. So that all makes sense.
Did you mean this?
char* myFile = "c\\tempo\\gb.txt";
While I'm here, don't do this. Your tutorial is out of date. If yopu have something that is to act as a string ( "c\\tempo\\gb.txt" ) don't use char-pointers. Just use a
string
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream fout;
ifstream fin;
string str;
string myFile("c\\tempo\\gb.txt");
fin.open(myFile);
while(fin >> str)
{
cout << str << "\n";
}
return 0;
}
|