String library isnt importing getline function - c++

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>.

Related

passing ifstream object to function and use getline() in between

I want to open a file named 1.board by calling a function and use getline function to print it's characters to new line.But this is showing a lot of errors.
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using std::ifstream;
using std::cout;
using std::string;
using std::vector;
void ReadBoardFile(ifstream& search)
{
string line;
search.open("1.board");
while(getline("1.board",line))
{
cout<<line<<"\n";
}
}
int main() {
ifstream fin;
ReadBoardFile(fin);
}
I don't know what i'm doing wrong.I just can't find a perfect and exact answer.
Help,if you can.Thanku!!!!!
So here's your code rewritten so it works.
Two changes, first the first parameter to getline should be the stream you are reading from not the name of a file. I'm guessing that you just weren't concentrating when you wrote that.
Second change, I've moved the stream variable search so that it is local to your ReadBoardFile function. There's no reason in the code you've posted to pass that in as a parameter. You might want to pass the name of the file as a parameter, but I'll leave you to make that change.
void ReadBoardFile()
{
ifstream search("1.board");
string line;
while(getline(search,line))
{
cout<<line<<"\n";
}
}
int main() {
ReadBoardFile();
}

Strings as File names

If I set a string as a filename, it doesn't work and I have no idea why. (I'm using codeblocks and it seems to work on other IDEs)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string FileName="Test.txt";
ofstream File;
File.open(FileName);
}
This does not work,while this next one does:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream File;
File.open("Test.txt");
}
Error message:
no matching function for call to std::basic_ofstream::open(std::string&)
Can someone help a bit with this problem, I cannot understand why this error occurs.
Due to what should be considered a historical accident in the early era of C++ standardisation, C++ file streams originally didn't support std::string for filename parameters, only char pointers.
That's why something like File.open(FileName), with FileName being a std::string, didn't work and had to written as File.open(FileName.c_str()).
File.open("Test.txt") always worked because of the usual array conversion rules which allow the "Test.txt" array to be treated like a pointer to its first element.
C++11 fixed the File.open(FileName) problem by adding std::string overloads.
If your compiler doesn't support C++11, then perhaps you should get a newer one. Or perhaps it does support C++11 and you just have to turn on the support with a flag like -std=c++11.

C++ print palindromes from a text file

Check if a string is palindrome
I was using the link above to try to solve this problem (among many others, Ive been trying to solve it various ways all day with no dice, this is my first C++). All other examples are usually in an array format, and I can't make assumptions as to the length of a word.
I'm trying to make a program to detect if a word is a palindrome or not. I have a text file with one word per line, and want to test each word, line by line, if it is a palindrome, and if so to print it to the screen, and if not, to ignore it and not print it.
I figured the best way to locate the palindromes was to reverse the word line by line and match it to the original, and if they are the same (==), then to print it. Here is what I have so far:
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]) {
std::string line;
std::ifstream infile("wordlist.txt");
}
string reverse(string line){
if (line == string(line.rbegin(), line.rend())) {
cout << string;
}
}
All help is appreciated
I guess your question is a homework question and you would like to get some information on how to complete the C++ coding.
You look not to know how to read file contents in C++.
Here's a link of how to do it:
Read file-contents into a string in C++
I am not very sure about what you specifically would like to be answered. If your question is a homework question, here's some info of how to ask:
https://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions
#include<iostream>
#include<algorithm>
#include<string.h>
#include<fstream>
using namespace std;
int main() {
string line="", line_rev="";
ifstream infile;
infile.open("wordlist.txt");
do{
infile>>line;
line_rev=line;
reverse(line_rev.begin(), line_rev.end());
if(line==line_rev)
cout<<line<<endl;
}while(getline(infile, line));
//if(infile.is_open()){cout<<"open"<<endl;} //to check if file is open or not
//else{cout<<"unable to open"<<endl;}
return 0;
}
This is the solution. i dont know why you are writing "string reverse(string line)" out side the main() function.

How to read doubles from a string in c++ using istringstream

I am new to programming c++. I am trying to read 75 doubles that are inside of a string that I read from a file. I am trying to use istringstream.
This is what I have so far:
Header File:
#ifndef READPOINTS_H_INCLUDE
#define READPOINTS_H_INCLUDE
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std::istringstream;
.....
istringstream linestr;
CPP FILE:
#include
void ReadPoints::grabPoin(const string& read_line, vector<doubles> PointVector){
linestr(read_line);
for(int i = 0; i < 75; i++){
linestr >> value
pointVector.push_back(value);
}
}
When I compile this code I get the following error:
ReadPoints.cpp: In member function ‘bool ReadPoints::grabPoint(const string&, std::vector&)’:
ReadPoints.cpp:48:19: error: no match for call to ‘(std::istringstream {aka std::basic_istringstream}) (const string&)’
linestr(read_line);
Can anyone explain what is wrong and why I am getting the no match for call?
Don't place a definition inside a header. Currently, you've got istringstream linestr;: place this in exactly one *.cpp file, and then in your header, extern std::istringstream linestr (the latter is called a declaration, which is different to a definition). However, this stringstream is best defined in the function itself anyway.
Replace linestr(read_line) in your *.cpp with std::istringstream line_str{ read_line } and remove these two lines from your header file: using namespace std::istringstream; and istringstream linestr;.
Your code should now look like this:
void ReadPoints::grabPoin(const string& read_line, vector<doubles> PointVector){
std::istringstream linestr{ read_line }; // if that doesn't work, use linestr(read_line) instead... note the () and {} have different meanings
for(int i = 0; i < 75; i++){
linestr >> value
pointVector.push_back(value);
}
}
Here's a couple of other tips:
Never place a using directive inside a header (that is, using namespace)
Avoid using directives at all costs. If you don't want to type std::istringstream, place it inside your *.cpp files (yes, each of them), as using std::istringstream.
If you must use a using directive, you need to do it like so: using namespace std;.
Both of these will help you to avoid namespace pollution, which makes calling the right functions difficult.

Read line from file: Unable to use getline()

I have the task to write a function that reads a line from a text file (renamed to .dat, however it contains only text), but I am out of options for a solution because of the following points:
I am using Borland C++ Version 5.02, and no, I CAN´T download another compiler because I dont have admin rights on my laptop and the guy who has the needed password isnt there until next week.
The compiler does not accept using namespace std, and also it doesnt accept getline(), no matter if string and iostream are included or not.
I am trying to find a solution or at least the tiniest approach, but I am unable to find one.
So my question is: How do I read a line from a simple textfile without using getline() (cin.getline works, the ones from string or fstream doesnt) ? The textfile contains several lines like these:
1234;12.05.03;08:44:23; XY12-AB;A1-12;Timeout
2345;12.05.03;09:04:34;XY1-CD;A22-9;Connection refused
And the numbers/letters between the ; need to be stored in variables so they can be worked with.
Im not asking for you to write my code, but I am reallyreaylly frustrated and my instructor is no help.
Live long and prosper,
Me.
http://www.cplusplus.com/reference/string/string/getline/
If you cant use (which you really shouldnt)
using namespace std
Then refer to your namespace with :: operator
For example:
std::string
Now try to write your own code using std:: and comment if you still cant do it.
Also, there is a lot of other options than std::getline() to read line of text.
Ref: Read file line by line
Option 1:
Try using the C's fgets() function.
Option 2:
You mention that cin.getline() works. You can freopen stdin with the input file and then cin will point to mentioned file. After that cin.getline() will read from the file:
Downside: After the freopen you will not be able to accept any input from the user.
Example: Note this has not been tried using g++ but I guess it should work with Borland too.
#include <iostream>
#include <stdio.h>
int main() {
char buf[1000];
freopen("somefile.dat", "r", stdin);
while (cin.getline(buf, sizeof(buf)).good()) {
// Now buf contains a line
// Do something with it
}
return 0;
}
Try using
getline(cin >> ws, variableName);
But first, you have to use
using namespace std;
I'm having the same problem while i using multi dimensional array on structs into a file, i have try different ways. But then i tried this one and it's work for me.
So in my case it gonna be like this
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream myFile;
myFile.open("test.txt");
int count = 0;
while (!myFile.eof())
{
getline(myFile >> ws, data[count].string1);
getline(myFile >> ws, data[count].string2);
myFile >> data[count].int1;
for (int i = 0; i < data[count].int1; i++) {
getline(myFile >> ws, data[count].data2[i].string3);
}
count++;
}
return 0;
}
For more : https://www.geeksforgeeks.org/problem-with-getline-after-cin/