no matching function for call to 'fscanf' - c++

I'm trying to read a time from a file for example (12:00 A)
I need to read in all three parts. Here is what I have.
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
int main()
{
string name, filename;
ifstream inputFile;
cout << "What is the name of the file to be provessed ";
cin >> filename;
inputFile.open(filename.c_str());
getline(inputFile, name);
fscanf (inputFile, "%d:%d %c", &startH, &startM, &startAP);
fscanf (inputFile, "%d:%d %c", &endH, &endM, &endAP);
inputFile >> payRate;
I'm getting the error from the title and I'm not sure what I'm doing wrong.

Function fscanf is a standard C function that is declared in header <cstdio> the following way (I will show how the function is declared in header <stdio.h> in C. in fact the same declaration except the keyword restrict is used in C++)
int fscanf(FILE * restrict stream,
const char * restrict format, ...);
As you can see there is no parameter of type std::ifstream
So the compiler issues the error because it is unable to find a function with name fscanf that has the first parameter of type std::ifstream and at the same time it can not implicitly convert an object of type std::ifstream to a pointer of type FILE *
You should not mix C++ stream functions with C stream functions.

Related

String library isnt importing getline function

Im using string library but I can't seem to use the get line function
used both string and string.h but its still not working
I've added the code below and it basically is just to use input numbers in a text file and then using them in a sorting mehthod
the problem is that im only getting the error mentioned below and can't seem to wrap my head around the solution to it
,,assignmentDTS.cpp:34:17: error: no matching function for call to 'getline'
getline(inputFile,tempnumstring);
#include<iostream>
#include<fstream>
#include<string.h>
#include<time.h>
using namespace std;
class sorting {
public:
void bubblesort(int arraySize){
int num;
int *arr=new int[arraySize];
ofstream inputFile("data.txt");
if(inputFile.is_open()){
for (int i = 0; i <arraySize; i++){
string tempnumstring;
std::getline(inputFile,tempnumstring);
num=stoi(tempnumstring);
arr[i]=num;
}
And there we go: ofstream inputFile("data.txt"); needs to be ifstream inputFile("data.txt"); The fstream starts with i for input stream, rather than o for output stream.
Answered on chat
std::getline is declared in #include <string> per the C++ Library Standard.
The #include <string.h> statement includes the C Library Standard string library which has no getline function. This header contains functions like memcpy and strcpy.
The C++ recommended way to reference the C "string.h" header is #include <cstring>.

Creating a file with variable name in c++

so I want to create a file but the name of it will be dependent on the user input e.g. if the user types "shrek" the file must be named "shrek.txt". Thats what I came up with but it doesn't work.
int main(){
ofstream file;
string name = "abc";
file.open(name + ".txt");
file.close();
}
I guess you are using an old C++ standard. If that's the case, fstream::open won't accept a std::string, only a C string (char*). You can use c_str in your string to obtain a const char* that will be accepted:
int main(){
ofstream file;
string name = "abc";
string file_name = name + ".txt";
file.open(file_name.c_str()); // <- here
file.close();
}
However, it's recommendable to switch to a more modern standard, as your code actually works for C++11 and newer.
Do you have the libraries required? In this case #include fstream. Does the same issue happens to another complier? Check that out. I attempted this myself and your code surely works. Attempt to use my code which I confirm works and see if you have any issues.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string name;
cin>> name;
ofstream file(name +".txt");
file.close();
}

More than one instance of overloaded function matches the argument list and I can't find where the error happens

I get the above error when I use this code.
//Programming Assignment 1
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Function Prototypes
void getname(ofstream);
//void Evaluate_holesterol(ofstream);
//void Evaluate_BMI(ofstream);
//void Evaluate_bloodpressure(ofstream);
int main()
{
//Open output file
ofstream pfile;
pfile.open("Profile.txt");
getname(pfile);
//Evaluate_holesterol(pfile);
//Evaluate_BMI(pfile);
//Evaluate_bloodpressure(pfile);
//pfile.close();
system("pause");
return 0;
}
//Function to get patient's name
void getname(ofstream &pfile)
{
string name;
int age;
cout<<"What is the patient's full name (middle initial included)?";
getline(cin, name);
cout<<endl<<"What is the patient's age?";
cin>>age;
string line = "Patient's Name: ";
string ageline = "Patient's Age: ";
pfile<<line+name<<endl;
pfile<<age<<endl;
}
I've checked my functions and arguments and I don't see any function that its can be confusing its arguments with anywhere else. Apologies in advance if its something simple and I just didn't see it.
As the comments by cigien and Peter already pointed out: The declaration and the definition of getname() have mismatched parameters. To fix that, change the line
void getname(ofstream);
to
void getname(ofstream&);
Note the & after ofstream.
Furthermore, any function that gets an ofstream as parameter should get that by reference (i. e. as ofstream& and not just ofstream), because there is no copy constructor for ofstream and any attempt to pass ofstream by value will cause compile errors.

Getline function is undefined, even though <string> header is included

I have looked at a few other questions regarding getline() not functioning, however most problems regarding the topic were due to the programmer not including the string header. I have the string header included however getline is still giving me error E0304 (which I have already looked into).
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
char input[100];
getline(cin, input);
cout << input << endl;
}
There are two forms of getline:
std::cin.getline(array, size); // reads into raw character array
getline(std::cin, string); // reads into std::string
You should use a std::string instead of a raw character array:
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string input;
getline(std::cin, input);
std::cout << input << "\n";
}
The non-member getline only works with std::string. Use the std::istream member function getline for C-style strings:
std::cin.getline(input, sizeof(input));

C ++ ifstream I/O?

I'm experimenting with C++ file I/O, specifically fstream. I wrote the following bit of code and as of right now it is telling me that there is no getline member function. I have been told (and insisted still) that there is a member function getline. Anybody know how to use the getline member function for fstream? Or perhaps another way of getting one line at a time from a file? I'm taking in two file arguments on the command line with unique file extensions.
./fileIO foo.code foo.encode
#include <fstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <map>
#include <string>
#include <cassert>
using namespace std;
int main( int argc, char *argv[] )
{
// convert the C-style command line parameter to a C++-style string,
// so that we can do concatenation on it
assert( argc == 2 );
const string foo = argv[1];
string line;string codeFileName = foo + ".code";
ifstream codeFile( codeFileName.c_str(), ios::in );
if( codeFile.is_open())
{
getline(codeFileName, line);
cout << line << endl;
}
else cout << "Unable to open file" << endl;
return 0;
}
getline(codeFileName, line);
Should be
getline(codeFile, line);
You're passing in the file name, not the stream.
By the way, the getline you're using is a free function, not a member function. In fact, one should avoid the member function getline. It's much harder to use, and harkens back to a day when there was no string in the standard library.
Typo
getline(codeFileName, line);
should be
getline(codeFile, line);
I guess the lesson is you have to learn how to interpret compiler error messages. We all make certain kinds of mistakes and learn the compiler errors they tend to generate.