no instance of overloaded function "getline" matches argument list - c++

the fstream and string inlcusions are in the .h file. I've read multiple people's problems but can't seem to apply it to my own. I've tried putting in infile.getfile(dataFile, line)) to no avail. The dataFile is just a list of employees. I still keep getting the same no instance of overloaded function with getfile(dataFile, line)
{
ifstream infile;
infile.open(dataFile);
if(!infile.good())
{
cout << "File is not open";
}
else
{
int i = 0;
while(!infile.good())
{
string line;
while (getline(dataFile, line))
{
if (line[0] == 'h' )
{
HospitalEmployee newEmp;
}
}
}
}
}

while (getline(dataFile, line))
The first argument is the stream, and the second argument is the string where the line will be assigned. You got the second argument right, but the first argument is a string object, not the file stream from which the line will be extracted. You're supposed to pass the stream, inFile:
while (getline(inFile, line))
// ^^^^^^

Your infile.open(dataFile) suggests that dataFile is the string filename and infile is the stream.
So, instead of getline(dataFile, line), you mean getline(infile, line).
Typo, I'm sure.

Related

fstream reading error (only reading first line)

I want to read a file with std::getline. but reads first line only
string FileReader::readLine() {
string line;
string read;
ifstream ReadFile;
ReadFile.open("input.txt");
if (ReadFile.is_open()) {
getline(ReadFile, line);
//ReadFile.close();
}
return line;
}
this is my method. I call this method several time but always reads first line how can i do to read next lines?
You need to change your program flow.
Don't return a string. Use the line within the loop to do whatever it is you want. Ensuring that you either don't leave the method or return to it.
You can't keep coming back to a function like this, as it will keep reading from the beginning.
void FileReader::readLine() {
string line;
string read;
ifstream ReadFile;
ReadFile.open("input.txt");
if (ReadFile.is_open()) {
while(getline(ReadFile, line))
{
//do what you want with that line, but return program flow here.
}
ReadFile.close();
}
}

How to use getline for consecutive lines in a file

I am trying to use getline on a file (unkown size) to grap the first line, input it into a string, manipulate this string (replace words with others, move some around) and output the manipulated line back to the file.
After this, I need to do the same thing to line 2, 3, etc. until the end of the file. How would I go about doing this? I figured a while loop for getline would work, but not sure how to get the conditions for the while loop or how to manipulate each line individually. Such as lines 1 and 3 must be manipulated differently than lines 2 and 4. etc.
A rough idea of what I'm trying to do:
void readFile(string filename, string text)
{
ifstream infile;
infile.open(filename);
getline(cin, text) // pretty sure this is wrong..
infile.close(); // close here, or after manipulation???
}
void swapText(string filename, string text)
{
string decrypText;
//Manupulate several things..
return decrypText;
}
void writeToFile(string filename, string decrypText)
{
ofstream outfile;
outfile.open(filename);
outfile << decrypText << endl;
outfile.close();
}
The standard idiom for reading text lines from a file and storing them is:
std::vector<std::string> file_data;
std::string text_line;
while (std::getline(my_data_file, text_line))
{
// Optional: store the text line
file_data.push_back(text_line);
// Call a function to process (or ignore) the text line:
Process_Text_Line(text_line);
}
If you want to have a function that reads the file, you may need to pass the vector:
void Read_File(std::vector<std::string>& file_data)
{
//...
// Read the data, see "while" loop above.
}
Don't open and close the file for every read. Keep it open and read a line at a time:
std::istream in("filein.txt");
std::ostream out("fileout.txt");
std::string line;
while (std::getline(in, line)) {
// modify line as appropriate
out << line << '\n';
}

How to get a specific line from a .txt (C++)

I would like some help with getting the first line from a txt file called "test.txt", I have discovered the getline function however I am not sure why my code doesn't work or what I need to do. I would like to get the first line from the .txt file, but it prints "t" for some reason. Feel free to correct me as you please if I am not handling it correctly. This is the code I am using:
string FirstLine;
ifstream File("test.txt");
string line;
if (File)
{
while (getline(File, line))
{
FirstLine = line[0];
}
File.close();
}
cout << FirstLine;
And this is the .txt file:
this is line 1
this is line 2
this is line 3
If you just want the first line:
string line;
getline(File, line);
Your first line of the file is then stored in line as a, you guessed it, string
To get all lines (line by line):
while(getline(File, line).good())
//do something with line
string FirstLine;
ifstream File("test.txt");
string line;
if (File)
{
getline(File, line);
FirstLine = line;
File.close();
}
cout << FirstLine;
Is the absolute minimum changes you need to your code to make it do what you want to do. However, there is A LOT of room for improvement on the above code sample. For example, why create two strings, line, and FirstLine, just pass FirstLine to the getline() function. I just modified what you provided to highlight where the mistakes where. Hope this helps...

Error using getline with ifstream - C++

I need help, I tried googling if I could find a similar problem but the solutions for others didn't work for me.
I'm trying to use getline() to read the file I've opened but it's not accepting the parameters I've given it.
What I'm trying to accomplish at this time (not the entire program) is to open a .csv file and determine how many elements it has inside by using getline() and using the , character as the delimiter. My loop has an index which I could just add 1 to it so that I can get the total number of elements inside the file.
The reason I'm doing this is because I intend to use it for a project at school but so far I've gotten stuck at the getline() error:
no matching function for call to 'std::basic_ifstream::getline(std::string&, int, const char [2])'
My code is here:
void readfile(string a)
{
int i = 0;
ifstream infile;
infile.open(a.c_str());
string temp;
//count how many elements are inside
if(infile.is_open())
{
while(infile.good())
{
infile.getline(temp, 256, ",");
i++;
}
infile.close();
i+=1;
}
else
{
cout<<"Error opening file.";
}
cout<<i;
}
Use the free getline() function:
std::string line;
getline(infile, line);
In addition to the answer by #UlrichEckhardt, I'd handle delimiters like this:
if(infile.is_open())
{
string temp;
// std::getline(std;:istream&, std::string) used below
while(getline(infile, temp)) {
std::stringstream stream(str);
std::string token;
while (std::getline(stream, token, ','))
if (!token.empty()) // it's up to you to decide how to handle empty tokens
i++;
}
}
Note the ','. If it were ".", this would be considered a string by the compiler, which is exactly what you're seeing in the error message: a '\0' is appended automatically, thus producing a char[2].

Creating String Array from CSV

I have a csv file of atomic elements with atomic number, symbol, and name. The file is formatted as:
1,H,Hydrogen
2,He,Helium
3,Li,Lithium
...
I'd like to create an array of the symbols referenced by the atomic number. ie. ArrayName[32]="Ge";
I've been trying to use sscanf but it hasn't been working. rough code below:
char temp[200];
float temp_z;
std::string temp_ele;
std::string temp_name;
while(!fin.eof())
{
fin.getline(temp,200);
sscanf(temp, "\"%f\",\"%s\", \"%s\"",&temp_z, &temp_ele, &temp_name);
cout<<temp_z<<endl;
cout<<temp_ele<<endl;
cout<<temp_name<<endl;
}
Read every line of your file with this loop :
string line;
ifstream myfile;
myfile.open("myfile.txt");
if(!myfile.is_open()) {
perror("Error open");
exit(EXIT_FAILURE);
}
while(getline(myfile, line)) {
// Split line by comma to get what's your want
}
Then split every line by comma to get every element of the line.
You can read each element like so:
string theStrings[200]; //initialize to correct size
int i = 0;
string name;
while(!fin.eof())
{
getline(thefilestream, name, ',' );
theStrings[i++] = name;
cout<<name<<endl;
}