Need help with simple IO of a text file

closed account (9yq5oG1T)
Hello all,
I have written this code from a tutorial video and I can't get it to compile. Can someone have a look to see what is wrong? Thanks!

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ofstream fout;
ifstream fin;
string str;
char myFile = "c\\tempo\\gb.txt";

fin.open(myFile);
while(fin,str)
{
cout << str << "\n";
}

return 0;
}


Here is the error:

line 11 error: invalid conversion from 'const char*' to 'char' [fpermissive]
line 13 error: invalid conversion from 'char*' to 'const char' [fpermissive]
Last edited on
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;
}
Last edited on
closed account (9yq5oG1T)
Hi,
Thanks for the help. i'm new to c++ and am using a tutorial video for NetBeans IDE from 2011 and i'm using codeblocks. So I tried what you have written and it stops with an error on line 14.

C:\Users\Pickle\Documents\C++\IO Textfile\main.cpp|15|error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)'|
Last edited on
This would indicate that your compiler either does not support C++11 (from 2011), or if it does, you have not told it that you're coding in C++11.

Netbeans is your IDE; what's your compiler? You can get an up-to-date compiler free.
closed account (9yq5oG1T)
I went to the Code::Blocks website and downloaded the package that included the compiler.

codeblocks-13.12mingw-setup-TDM-GCC-481.exe 27 Dec 2013 BerliOS or Sourceforge.net

Is there a better IDE and compiler I should use? Again I am just googling c++ stuff and trying to learn so i'm doing the best I can with all this.
If that's GCC 4.8.1, you should be able to tell it you're coding in a more modern C++.

https://stackoverflow.com/questions/18174988/how-can-i-add-c11-support-to-codeblocks-compiler

C++11 was a big improvement for C++, with a lot of very good new features.
Last edited on
closed account (9yq5oG1T)
That made it work. Thanks!
Topic archived. No new replies allowed.