0

I am trying to make a program that opens a file and hunts for some values in the file. I'm doing this by making a function that is given a reference keyword and the file as inputs, and calling it within the main function and I'm finding errors saying that some functions have been deleted.

My Code

#include<iostream>
#include<fstream>
using namespace std;

string findLine( ifstream openFile, string reference){
    string line;
    getline(openFile,line);
    while(line.find(reference) == std::string::npos){

            getline(openFile,line);

        }
    return line;    

}

int main()
{
ifstream file;
    string name;
    cin >> name;
    file.open(name); //opens file
    if(!file.is_open()){"error while opening the file";
    
    }else{
        string ref;
        ref = "TOGW";
        string TOGW;
        TOGW = findLine(file, ref);
    }
}

The errors:

[{
    "resource": "/c:/Users/zhtho/OneDrive/Documents/Research/C++ Programming/Programs/Research/newProcessor.cpp",
    "owner": "cpptools",
    "severity": 8,
    "message": "use of deleted function 'std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const std::basic_ifstream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]'",
    "source": "gcc",
    "startLineNumber": 29,
    "startColumn": 24,
    "endLineNumber": 29,
    "endColumn": 24
}]

and

[{
    "resource": "/c:/Users/zhtho/OneDrive/Documents/Research/C++ Programming/Programs/Research/newProcessor.cpp",
    "owner": "C/C++",
    "code": "1776",
    "severity": 8,
    "message": "function \"std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const std::basic_ifstream<_CharT, _Traits> &) [with _CharT=char, _Traits=std::char_traits<char>]\" (declared at line 590 of \"C:\\msys64\\mingw64\\include\\c++\\12.2.0\\fstream\") cannot be referenced -- it is a deleted function",
    "source": "C/C++",
    "startLineNumber": 29,
    "startColumn": 25,
    "endLineNumber": 29,
    "endColumn": 29
}]

I was able to get the same code working in a main function but I am trying to call a helper function for readability purposes.

  • 6
    Pass the file by reference to the function. string findLine( ifstream &openFile, string reference){ – Vlad from Moscow Nov 01 '22 at 21:27
  • 1
    Like Vlad says, use a reference. The deleted function is the copy constructor of the stream. Streams cannot be copied, because it is not clear what that would mean (like should it also create a new file on the disk? So just not allowed). – BoP Nov 01 '22 at 21:32
  • 1
    This doesn’t address the question, but get in the habit of initializing objects with meaningful values rather than default constructing them and immediately overwriting the default values. In this case, that means changing `ifstream file; … file.open(name);` to `ifstream file(name);`, and changing `string TOGW; TOGW = findLine(file, ref);` to `string TOGW = findLine(file, ref);`. – Pete Becker Nov 01 '22 at 21:34
  • 1
    The `std::string` should be passed by reference also, or by constant reference if the string is not modified. – Thomas Matthews Nov 01 '22 at 21:53

0 Answers0