mercredi 15 septembre 2021

Issue using FILE* with std::getline()

I would like to get some advice on an issue I've encountered once working on small adjustments to an existing program.

The program itself has to:

  1. Open a file and read it line-by-line preferably
  2. Pack the lines into istringstream and then split to 2 strings on a ':' separator
  3. Insert those 2 strings line1 and line2 into an existing std::map container
  4. Than I can do more stuff with the map and the data from it.

My code looks like that:

int main()
{
    FILE *fpFile;
    map<string, string>mapOfPci;
    std::string tempBuff="";
    std::string line1="", line2="";
        
    fpFile = fopen(PCI_MAPPING_PATH, "r");
    
    if(!fpFile)
        return false;
    
    while(getline(fpFile, tempBuff)){
        istringstream iSs(tempBuff);
        iSs >> line1;
        iSs.ignore(numeric_limits<std::streamsize>::max(), ':');
        iSs >> line2;
        mapOfPci.insert(make_pair(line1, line2)); 
    }
    for(const auto &m : mapOfPci){
        cout << m.first << " : " << m.second << "\n";
    }
    
    fclose(fpFile);
    return (0);
}

Now what I'm getting in my compiler feedback is:

mismatched types 'std::basic_istream<_CharT, _Traits>' and 'FILE* {aka _iobuf*}'
  while(getline(fpFile, tempBuff))

At this point I presume that this is due to the usage of FILE* file handling method.

I might not be able to use the C++ std::ifstream, std::fstream, so is there any method to move this further with the current FILE* usage?

Aucun commentaire:

Enregistrer un commentaire