lib.cpp

URL: https://mirkwood.cs.edinboro.edu/~bennett/class/cmsc3100/spring2026/notes/files/code/list/lib.cpp
 
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#include <system_error>
#include <cerrno>

using namespace std;

extern "C" {
    int GetString(int fd, char * buffer, int maxBufSize);
}

int GetString(int fd, char * buffer, int maxBufSize) {
     static FILE * theFile {fdopen(fd, "r")}; 

     if (theFile == nullptr) {
        cerr << "Failed to open the file descriptor" << endl;
        throw system_error(errno, generic_category(),
                          "Failed to open the file descriptor");
     }

     string fmtString = "%" + to_string(maxBufSize) + "s";
     int len = fscanf(theFile, fmtString.c_str(), buffer);
     return len;
}