C ++ ifstream I/O? - c++

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.

Related

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));

Dereference causes error: expected primary-expression before ')' token

When I use a dereference in a function as an argument, the preprocessor spits out an error.
I believe the * right before the parentheses causes ambiguity with the compiler.
Is there any way to get around this?
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main ()
{
char *in = NULL;
char *out = NULL;
getline(cin,in*);//error
out=system(in*);//error
printf(out);
return 0;
}
The errors are on marked lines.
Thank you!
Dereferencing in is written *in, not in*. (Also, even with that fixed, your program still won't work, as you're trying to dereference NULL, and the second argument to getline will have the wrong type. char* strings do not work the way you think they work.)
getline only works with C++ strings (not C-style strings). C++ strings can allocate memory as they go, in response to how much data is read.
There are other functions for reading into C strings, but you must pre-allocate the amount of memory you want, and also specify to the function how much memory you have allocated. In general there is no reason to do this, as the C++ string version is much simpler and less prone to error.
Also, avoid including C-style standard headers (i.e. ending in .h) and avoid using pointers. And system returns an int, not a string.
Example:
#include <iostream> // cin, cout
#include <string> // string
#include <cstdlib> // system
int main()
{
std::string s;
std::getline( std::cin, s );
int system_result = std::system( s.c_str() );
std::cout << system_result << "\n";
}

no matching function for call to 'fscanf'

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.

Differences in reading file using ifstream in g++ and msvc

When using ifstream class to read words from an input file, I have used the following expression:
#include <iostream>
#include <fstream>
int main(int argc, char *argv[])
{
std::ifstream inputStream(myFile.txt);
std::string myString;
myFile.open()
while(myFile.good())
{
myFile >> myString;
printf("%s \n", myString);
}
return 0;
}
The contents of myFile.txt are:
" This is a simple program. "
The compiles and executes as expected using g++ compiler.
However, the same code when compiled using msvc 2008, returns error at the extraction operator (>>) requiring me to replace the std::string with either an initialized character array or any of the supported native types.
This threw me off as I was expecting the usage of the standard library to be same across implementations.
I understand the compile error and know the way to fix it via using c_str().
But, it would help me a great deal, if someone could clarify why the usage for the standard library is different across platforms.
To me it is not starndard anymore !!
EDIT: Code updated to be complete. Content of myFile.txt updated.
Chances are that you forgot to #include <string>. Without it, Microsoft's version of <iostream> (and such) include enough of a declaration of std::string for some things to work, but other parts are missing, so you get strange, seemingly inexplicable failures.
One of the things that's missing is most of the operator overloads for std::string, which is exactly what you seem to be missing.
As an aside, while (myfile.good()) ... is pretty much a guaranteed bug -- you probably want:
while (myfile>>myString)
std::cout << myString << " \n";
Alternatively, you could do the job with a standard algorithm:
#include <string>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <iostream>
int main() {
std::ifstream myfile("input.txt");
std::copy(std::istream_iterator<std::string>(myfile),
std::istream_iterator<std::string>(),
std::ostream_iterator<std::string>(std::cout, " \n"));
return 0;
}
The following compiles fine for me on MSVC 2010:
std::ifstream inputStream;
std::string myString;
inputStream.open("myFile.txt", std::ifstream::in);
while(inputStream.good())
{
inputStream >> myString;
}
Note: without using std::ifstream::in as my open mode, I got the same error as you. I suggest you check what value you have for this parameter.

C++ STL remove error

I'm having trouble understanding where I went wrong with my code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string str = "";
cin >> str;
remove(str.begin(), str.end(), ' ');
cout << str;
cin.ignore();
}
The error says "'remove': function does not take 3 arguments (C2660)"
Try adding
#include <algorithm>
"algorithm" is an STL header containing a lot of functions, including std::remove, which the OP is trying to call. The error he got was because there is another function that takes a single argument, called "remove", which deletes a file.