Domanda

I am having trouble loading a char into one of my functions. This is the source code which I started out with.

main.cpp

#include <IOF\IOF.h>

int main()
{

char load;
string intro = LoadFileToString( "Intro.txt" );

cout << intro << "> ";

cin >> load;

string loadedFile = LoadFileToString( load );

cout << loadedFile;

cin.get();
cin.ignore();

return 0;

}

IOF.h

#ifndef IOF_H
#define IOF_H

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string LoadFileToString( const char * filePath )
{

string fileData;

ifstream loadFile( filePath, ios::in );

if ( loadFile.is_open() )
{

    string line;

    while ( getline( loadFile, line ) )
    {

        fileData += line + "\n";

    }

    loadFile.close();

}

return fileData;

}

#endif

And this is what my compiler tells me.(I am using MinGW)

H:\Programming\Dropbox\C++\Basic Text Editor\main.cpp: In function 'int main()':
H:\Programming\Dropbox\C++\Basic Text Editor\main.cpp:13:45: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
string loadedFile = LoadFileToString( load );
                                         ^
In file included from H:\Programming\Dropbox\C++\Basic Text Editor\main.cpp:1:0:
H:\Programming\pocketcpp\MinGW\include/IOF\IOF.h:10:8: error:   initializing argument 1 of 'std::string LoadFileToString(const char*)' [-fpermissive]
string LoadFileToString( const char * filePath )

I have tried referencing load, it compiles fine, but when I run the program and try to load a text file, it crashes. I have also tried adding a cast to it

string loadedFile = LoadFileToString( ( const char * ) load );

but that gave me this error:

H:\Programming\Dropbox\C++\Basic Text Editor\main.cpp: In function 'int main()':
H:\Programming\Dropbox\C++\Basic Text Editor\main.cpp:13:57: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
  string loadedFile = LoadFileToString( ( const char * ) load );

I originally made the IOF header so I would have a fast and easy way of receiving text from a file, but am I going to have to do all of the ifstream stuff manually for this part? Or is there a workaround?

È stato utile?

Soluzione

char load;
string intro = LoadFileToString( "Intro.txt" );

cout << intro << "> ";

cin >> load;

string loadedFile = LoadFileToString( load );

You're trying to pass a char to LoadFileToString which takes in a char*. I think what you intend to do is this

string intro = LoadFileToString( "Intro.txt" );    
cout << intro << "> ";
string load;
cin >> load;    
string loadedFile = LoadFileToString( load.c_str() );

Notice that I'm reading the input into a string instead of a char since the LoadFileToString expects a (C-style) string to be passed in; instead you're reading and trying to pass a character to it. Once the input is read through a string, a C-style string can be obtained by calling string::c_str() function.

Altri suggerimenti

Never try to muzzle the compiler. Those warnings are invaluable. Obviously, you want a std::string instead of a char, where you define load.

you get the const char* from a std::string by calling c_str().

Though, there is no reason your LoadFileToString() function should not instead take a const std::string&.

The error message is clear enough.

The function declared as

string LoadFileToString( const char * filePath );

that is its parameter has type const char *

You are trying to call the function passing variable load as an argument

string loadedFile = LoadFileToString( load );

However the variable load is declared as having type char

char load;

So there is a type mismatch.

You could for example to write

string loadedFile = LoadFileToString( &load );

but in this case the code would be also invalid because the pointer referes to non zero-terminating string that is the behaviour of the program will be undefined.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top