Search code examples
c++qtmingwgetline

Qt getline error with MinGW


I am a beginner in both C++ and Qt. Currently, I am studying a Qt program which I got it from open source website. It's a personal expense track Apps. When I am trying to build this project in Qt creator, it appears the error:

C:\Qt\Saved project\Expense-Tracker-master\dataRead.h:152: error: 'getline' was not declared in this scope while((read = getline(&line, &len, labelFile)) != -1){ ^

Here under are the related code:

std::vector<std::string> * readLabel(const char* labelToRead){
std::vector<std::string> * labels = new std::vector<std::string>;
FILE * labelFile = fopen(labelToRead, "r");
if(labelFile == NULL) std::cout<<labelToRead<<std::endl;
//TODO -- throw if failure
char * line = NULL;
size_t len = 0;
ssize_t read;
while((read = getline(&line, &len, labelFile)) != -1){
    std::string temp(line);
    std::string label(temp.begin(), temp.end()-1);
    labels->push_back(label);
}
free(line);
fclose(labelFile);
return labels;}

I search in Google, it seems that something goes wrong between getline() and MinGW compiler. Could someone help me out on this.

Plus, I include the string header already. When I put 'std::getline', it shows another error:

C:\Qt\Saved project\Expense-Tracker-master\dataRead.h:152: error: no matching function for call to 'getline(char**, size_t*, FILE*&)'
     while((read = std::getline(&line, &len, labelFile)) != -1){
                                                      ^

Solution

  • I can't find a standard function getline working with FILE*. I think the function you're looking for is fgets. If you're using Qt, I guess you're coding in C++. FILE* belongs to C. You should consider using C++ functions instead of C (see fstreams).