Home > Software design >  Does not work _cgets. Identifier not found
Does not work _cgets. Identifier not found

Time:10-01

I am trying to compile the following code in visual studio 2019:

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <string>
int main() 
{
     char buffer[5];
    char* p;
    buffer[0] = 3;
    p = _cgets(buffer); // problem with _cgets
    printf("\ncgets read %d symbols: \"%s\"\n", buffer[1], p);
    printf("A pointer is returned %p, buffer[2] on %p\n", p, &buffer);

return 0;
}

But I see the following error:

main.cpp(11,9): error C3861: '_cgets': identifier not found

CodePudding user response:

_cgets is no longer available, use _cgets_s instead:

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <string>
int main()
{
    char buffer[5];
    size_t sizeRead;
    auto error = _cgets_s(buffer, &sizeRead);
    printf("\ncgets read %zu symbols: \"%s\"\n", sizeRead, buffer);

    return 0;
}

Or the much simpler c code:

#include <iostream>
#include <string>

int main()
{
    std::string buffer;
    std::getline(std::cin, buffer);
    std::cout << "read " << buffer.size() << " characters \"" << buffer << "\"\n";
}
  • Related