ifstream no conversion from char to char exists error - c++

int main()
{
char buffer[1024];
ifstream dataFile ("./data.dat");
while(buffer)
{
localHouse->location = dataFile.getline(buffer, 1024);
}
}
This throws the error: No suitable converion function from "std::basic_istream<char, std::char_traits<char>>" to "char" exists.
It continues to throw this error if I use a pointer to buffer instead. as far as I can tell I'm using it exactly as seen in the example here.
There's another example on stackoverflow here
That shows similar usage but I can't get it to work, and it's really causing me to tear my hair out over an error that seems to say it can't convert from the char to char. >.<

getline's return value is the istream object which I guess it's not something that you want to assign to localHouse->location.
getline reads a line of your file into buffer variable that you have provided as the first parameter.

Related

How does read and write function work in C++ file handling?

I'm learning file handling in c++ from internet alone. I came across the read and write function. But the parameters they take confused me.
So, I found the syntax as
fstream fout;
fout.write( (char *) &obj, sizeof(obj) );
and
fstream fin;
fin.read( (char *) &obj, sizeof(obj) );
In both of these, what is the function of char*?
And how does it read and write the file?
The function fstream::read has the following function signature:
istream& read (char* s, streamsize n);
You need to cast your arguments to the correct type. (char*) tells the compiler to pretend &obj is the correct type. Usually, this is a really bad idea.
Instead, you should do it this way:
// C++ program to demonstrate getline() function
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
fstream fin;
getline(fin, str); // use cin instead to read from stdin
return 0;
}
Source: https://www.geeksforgeeks.org/getline-string-c/
The usage of the char * cast with read and write is to treat the obj variable as generic, continuous, characters (ignoring any structure).
The read function will read from the stream directly into the obj variable, without any byte translation or mapping to data members (fields). Note, pointers in classes or structures will be replaced with whatever value comes from the stream (which means the pointer will probably point to an invalid or improper location). Beware of padding issues.
The write function will the entire area of memory, occupied by obj, to the stream. Any padding between structure or class members will also be written. Values of pointers will be written to the stream, not the item that the pointer points to.
Note: these functions work "as-is". There are no conversions or translations of the data. For example, no conversion between Big Endain and Little Endian; no processing of the "end of line" or "end of file" characters. Basically mirror image data transfers.

Passing a string argument to ifstream

I'm trying to pass a string argument to a function from the main function and then passing this received string argument to ifstream constructor. I'm able to receive this string in the function, but when I pass this argument to ifstream, I get a error message:
no matching function for call to ‘std::basic_ifstream::basic_ifstream(const string&)’
std::ifstream file(fileName);
Here is my code:
int** read_CSV(std::string const& fileName)
{
//cout<<fileName<<"\n";//this works
std::ifstream file(fileName);//problem
//Rest of logic
}
The main function:
int main()
{
int** inputMatrix1 = read_CSV("inputData4_80-20_100x32.csv");
return 0;
}
The variable fileName is creating the problem. If I pass it as it is, it gives the error. But instead, if I explicitly mention the name of the file using string rather than the variable, the code works fine. Can someone explain what exactly is the problem here and how I can solve it?
Okay, so the problem was that I was trying to compile without C++11 standard using just g++ "NameOfFile.cpp". There are 2 possible solutions to the problem, from the comment section, and they are as follows:
Using -std=c++11 when giving the command for compilation. The resultant compilation command would look as follows:
g++ -std=c++11 "NameOfFile.cpp"
Another solution, as pointed out by #Peter, is to use c_str() at the end of the string object, since the ifstream doesn't accept strings as an argument before c++11, so c_str() is used for explicitly converting to a compatible format.
Here's the modified code line for the second solution:
std::ifstream file(fileName.c_str());
Thanks to #Peter and #Holt for their inputs.

File loader problems

i have a text file which contains authors and books lists, i need to load it to my program, here is the code of the method which should load it:
void Loader::loadFile(const char* path)
{
FILE* file = fopen(path, "r");
char* bufferString;
while (feof(file) != 1) {
fgets(bufferString, 1000, file);
printf("%s", bufferString);
}
}
I use it in my main file:
int main(int argc, char** argv) {
Loader* loader = new Loader();
loader->loadFile("/home/terayon/prog/parser/data.txt");
return 0;
}
And I get data.txt file is not completely printed.
What I should do to get data completed?
fgets reads into the memory pointed to by the pointer passed as first parameter, bufferString on your case.
But your bufferString is an uninitialised pointer (leading to undefined behaviour):
char * bufferString;
// not initialised,
// and definitely not pointing to valid memory
So you need to provide some memory to read into, e.g by making it an array:
char bufferString[1000];
// that's a bit large to store on the stack
As a side note: Your code is not idiomatic C++. You're using the IO functions provided by the C standard library, which is possible, but using the facilities of the C++ STL would be more appropriate.
You have undefined behavior, you have a pointer bufferString but you never actually make int point anywhere. Since it's not initialized its value will be indeterminate and will seem to be random, meaning you will write to unallocated memory in the fgets call.
It's easy to solve though, declare it as an array, and use the array size when calling fgets:
char bufferString[500];
...
fgets(bufferString, sizeof(bufferString), file);
Besides the problem detailed above, you should not do while(!feof(file)), it will not work as you expect it to. The reason is that the EOF flag is not set until you try to read from beyond the file, leading the loop to iterate once to many.
You should instead do e.g. while (fgets(...) != NULL)
The code you have is not very C++-ish, instead it's using the old C functions for file handling. Instead I suggest you read more about the C++ standard I/O library and std::string which is a auto-expanding string class that won't have the limits of C arrays, and won't suffer from potential buffer overflows in the same way.
The code could then look something like this
std::ifstream input_file(path);
std::string input_buffer;
while (std::getline(input_file, input_buffer))
std::cout << input_buffer << '\n';

Variables not equivalent fstream vs. declaration

Basically I'm reading the contents of a file using fstream then converting it to const char* type. I'm supplying this to Lua, and Lua will do something with this. This however does not work. What does work is if I do:
const char* data = "print('Hello world')";
luaL_pushstring(L, data);
luaL_setglobal(L, "z");
They both are in the type const char* type and they are both the same string (e.g. I compared the two lengths). Except one works, and the other. I'm baffled. Any help here? Here is the code:
std::string line,text;
std::ifstream in("test.txt");
while(std::getline(in, line))
{
text += line;
}
const char* data = text.c_str();
luaL_pushstring(L, data);
luaL_setglobal(L, "z");
Here is the Lua code:
loadstring(z)()
To diagnose this, you probably want to know more about what Lua thought. I'd write the Lua side as assert(loadstring(s))() instead. If loadstring fails, your current code at best prints an error from the attempt to call nil. With the assert() in the sequence, the call to nil will be replaced by a more informative error about what went wrong.
Don't you have to set the global before you push the value? Anyways, what's up, Camoy :P

Pass path to file / filename as argument to a function that prints the file to screen in C++

As the title says, is there any way to pass the path to the file / filename to open as an argument in the function?
I've written a short code for printing a .txt-file to the screen in C++, but instead of having all the code in the main(), I'd rather have it as an own function that I can call with the filename of the file to open as the only input argument.
Basically the beginning of the function would look like
void printFileToScreen()
{
ifstream fin;
char c;
fin.open("FILE_TO_OPEN.txt", ios::in);
blablabla
}
Now is there any way to pass "FILE_TO_OPEN.txt" when I call the function?
I've tried
void printFileToScreen(string str)
{
ifstream fin;
char c;
fin.open(str, ios::in);
blablabla
}
where I call the function like printFileToScreen("FILENAME.txt"), but with no luck, so I'm not sure how to do this.
Hope anyone can help :)
Unfortunately, the iostream functions deal with const char* types rather than with std::string (the iostream functions were developed independently of the STL). You instead could use std::string::c_str() to obtain a const char*:
fin.open(str.c_str(), ios::in);
As a general design rule, I would not pass the file name to the called function. I would pass the already opened std::istream object to read from. This allows you to do the job of printing in a function, and to do the job of opening the file and dealing with non-existent files in another. This has the bonus of being able to pass std::cin to your function!
Try changing your function to look like this :
void printFileToScreen(const string &str);
//If you pass a const char*, a string will be constructed
or this :
void printFileToScreen(const char *);
The function you wrote expects an instance of std::string to be passed by value.
Never mind, after some more trying and failing I found out that I needed to pass a char pointer, and not a string. :)
Of course you can pass the filename as a function parameter. If in doubt, pass a "const char*" rather than a string. I should work.