how to make file reference within program.

Neophyte to C++. Trying to reference a specific filename from within the program as opposed to asking user to input file name. Keep compiler messages referring to invalid conversion from 'char' to 'const char*. Been fighting this out for several hours referencing books and still have no success. Apparently I need someone to spell it out for me. It would be very much appreciated.

The file I want to open is called "history.txt"

code for this section that I've been trying to make work is something like this:

char jimfilename = "history.txt"
ifstream file_in(jimfilename);

compiler build message I get is invalid conversion from 'char' to const char*.

Please help. I'm baffled.

Thank you.

Have a Great Day,
Jim
Last edited on
Try making the name jimfilename a const char pointer

such as:
const char *jimfilename = "history.txt";
ifstream file_in(jimfilename);

regards mikeofthenight
That works!

Thank you very much!!!!!

Have a Great Day ... as a matter of fact have a Great rest of the month!!

Jim
Wow, Mike, you are incredible.
Last edited on
Spoke to soon. It does build without any error messages. Unfortunately something isn't working. The file that is supposed to be referenced isn't being referenced. By that I tried for some output from the file. It kept giving me "0" for readings. So I completely removed the file "history.txt" from the computer.

I have an "if" statement checking for the file.

If (! jimfilename)
{
then cout << "File " << jimfilename;
cout << " could not be opened" << endl;
return -1;
}

That should trigger a halt since there is no file on the computer labeled "history.txt"

but it goes right to the "else" part of the statement of giving me values I asked for based on reading lines. Unfortunately those values are not correct.

HELP

Thank you.

Have a Great Day,
Jim
Yep... you have a few problems. :)

First off, that little snippet shouldn't compile if that's what you have in your code. then? ;)

Second, did you mean !filein.is_open() for the condition of your if statement, by any chance? What you're basically doing there is checking if jimfilename is not equal to zero. >_>

Happy coding!

-Albatross
How about a lesson in, HOW to open a file. ???

if (! jimfilename.is_open())

Apparently the file isn't opening. How does one open a file in this case?

The code currently looks like this:

1
2
3
4
5
6
7
8
9
10
ifstream jimfilename("history.txt");

if (! jimfilename.is_open())
    {
        cout << "File " << jimfilename;
        cout << " could not be opened" << endl;
        return -1;
    }
    else
    {// the else side of the "if" statement 


I am getting a terminal readout: File 0 could not be opened

But the thing about it is there is "History.txt" present now in the directory.

as you can see the code keeps evolving as I keep reading and reading and reading. I appreciate the help.

Thank you. I know old dogs can learn new tricks it just takes awhile longer.

Have a Great Day,
Jim

Last edited on
Have you checked that you are trying to open "history.txt" and not "History.txt"
as the name you try to open must exist. "history.txt" as your program stands it
should work. Provided the file "history.txt" exists in your current directory.
Mikeofthenigjht .... thank you!
@jim3222
I like your use of bold. lol

on another note, I kind of dislike c-style strings. You can always use the std::strings!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include <string>
 #include <fstream> 
 #include <iostream>

 using namespace std;

 int main(){

  string file = "c:\file.txt"; //path to file

  fstream fin = (file.c_str(),ios::in|ios::binary);

  char buf = fin.get(); //get a single char from the file
  while(!fin.eof()){
    cout << buf; //output the recently received char
    buf = fin.get();
  }
 }

Topic archived. No new replies allowed.