I'm doing a C++ intermediate course on udemy. At the lesson about reading text files the tutor has written the following code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string filename = "Text.txt";
ifstream inFile;
if (inFile.is_open()) {
string line;
while (!inFile) {
getline(inFile, line);
cout << line << '\n';
}
inFile.close();
}
else
cout << "Unable to open file";
return 0;
}
On the tutor's computer the program work fine but on my computer I get the error: error C3861: 'getline': identifier not found.
Even worse if I try (as my IDE -Visual Studio 2019- suggested) to replace getline by std::basic_istream::getline I get the error : 'std::basic_istream': use of class template requires template argument list. Does anyone understands what happens?
add header
#include<string>
as getline is part of this header file
and I will suggest you to always refer namespace
instead of
using namespace std;
use
std::cout
std::getline
etc
Related
Im new to c++ and i was trying to open a ".txt" file using ifstream. the file im using is called "ola.txt" which literally just contains two lines of text without punctuation just plain and simple text. The code that i wrote is this
#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int x;
string line;
vector<int> vect;
ifstream inFile("C:\\Users\\ruial\\Desktop\\ola.txt");
inFile.open("C:\\Users\\ruial\\Desktop\\ola.txt");
if (inFile.is_open()) {
while (getline(inFile, line))
{
cout << line << '\n';
}
inFile.close();
}
else {
cout << "Unable to open file";
exit(1); // terminate with error
}
return 0;
}
The path to the file that i wrote is correct such that the file opens, but when the program runs it doesn´t cout the lines that i wrote on the txt file to the cmd, i dont know if this is somewhat important but im coding in visual studio 2019.
I can't seem to find the answer to this problem anywhere in the internet and to be honest i think im doing it right, any help would be much appreciated,thanks in advance.
You are trying to open the inFile twice. First time during inFile construction, ifstream inFile("C:\\Users\\ruial\\Desktop\\ola.txt"), second time you try to open it again with inFile.open("C:\\Users\\ruial\\Desktop\\ola.txt"), when it's already open, which is erroneous, and flags the stream as no longer good.
3 possible fixes:
Remove inFile.open("C:\\Users\\ruial\\Desktop\\ola.txt")
Use default constructor, without specifying the file name
inFile.close() before you open it again (obviously, not the nicest fix).
I am writing a basic code but run into an error when trying to open a file. I've had a rough break and am having to start from the basics. Following is the part of the code where I run into the error:
int main()
{
string name;
fstream file;
cout << " Enter file name and type (E.g filname.txt) : ";
cin >> name;
file.open(name);
Following is the error:
[Error] no matching function for call to 'std::basic_fstream<char>::open(std::string&)'
I am returning after a long break so I apologize for any inconsistencies.
If the std::basic_fstream::open(std::string&) overload isn't available you are probably compiling using some C++ version prior to C++11.
Make sure you compile using at least C++11 and it should be fine.
you have to pass the open mode too.
Here is an example:
// print the content of a text file.
#include <iostream> // std::cout
#include <fstream> // std::ifstream
int main () {
std::ifstream ifs;
ifs.open ("test.txt", std::ifstream::in);
char c = ifs.get();
while (ifs.good()) {
std::cout << c;
c = ifs.get();
}
ifs.close();
return 0;
}
Code taken from Here
I suggest you to always check on cplusplus.com
It's very well documented!
I'm reading in a file name from the keyboard and opening the specified file. However it is not meeting my if statement that determines if it's open or not. Heres my code:
#include "prog.hh"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string fileName;
cout << "Enter the file name to be read: ";
cin >> fileName;
ifstream input_file("fileName");
std::string line_; // string which text file contents will be stored in
if(input_file.is_open()){ // validation to see if the file is open
while(getline(input_file, line_)){
std::cout<<line_<< '\n'; //prints the contents of the file into the console
}
input_file.close();
}
else {
std::cout<<"File is not open"<< '\n';
}
std::cin.get();
After being compiled, I type in the file name I want to open and I get back the else message "File is not open", although the ifstream should of opened it. I definitely have the right file I'm trying to open in the correct folder. Any help appreciated, thanks.
change
ifstream input_file("fileName");
to
ifstream input_file(fileName);
When I debug this I can see it opens datafile1 , it reads the firstline and
in the logfile I get roma-3-4.log
It change to c:/temp/roma-3-4.log but when I want to open it , it fails. I have check that the _Mystate = 2 .
What is the meaning of that
Thanks
in the transfersubs.cfg there is this
roma-3-4.log
** In the directory c:/temp/ I have the following file
roma-3-4.log
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string input;
string logfile;
string errorfile;
short logfilesize1;
fstream dataFile1("c:/temp/transfersubs.cfg", ios::in);
if (dataFile1)
{
getline(dataFile1, input, '$');
logfile=input;
logfilesize1=input.size();
errorfile=input;
errorfile[logfilesize1-4]='e';
errorfile[logfilesize1-3]='r';
errorfile[logfilesize1-2]='r';
logfile="C:/Temp/"+logfile;
fstream dataFile2( logfile, ios::in);
if (dataFile2)
{
dataFile2.close();
}
else
{
cout << "ERROR: Cannot open logfile.\n";
}
dataFile1.close();
}
else
{
cout << "ERROR: Cannot open file.\n";
}
system("Pause");
return 0;
}
I believe your getline doesn't bother looking the newline but only for a $. You didn't post the file you are reading from, but check to ensure it has a $ at the end of the file name otherwise it will fetch the entire file.
It appears that unless you put a \n or endl after writing to the file using ofstream, ifstream won't be able to read anything from the file. In fact, adding a space after whatever you've written into file won't help either.
So always add a newline right after whatever it is that you've written to file using ofstream.
I'm trying to parse a document using getline to take an entire line and place it in the string variable named 'line.' The problem is I'm getting an error that says: "No instance of overloaded function getline matches the argument list." Can anyone help me solve this problem?
#include <iostream>
#include <fstream>
#include <string>
#include "recordsOffice.h"
using namespace std;
RecordsOffice::RecordsOffice()
{
}
void RecordsOffice::parseCommands (string commandsFileName)
{
//String to hold a line from the file
string line;
//Open the file
ifstream myFile;
myFile.open(commandsFileName);
// Check to make sure the file opened properly
if (!myFile.is_open())
{
cout << "There was an error opening " << commandsFileName << "." << endl;
return;
}
//Parse the document
while (getline(myFile, line, '/n'))
{
if (line[0] == 'A')
{
addStudent(line);
}
Your escape sequence is backward - try replacing
/n
With
\n
Multicharacter character liberals in C++ have type int, rather than type char, which is causing the arguments to std::getline to have the wrong type. (Thanks to #chris for pointing out that the type will be int specifically!)
Hope this helps!