I can not get to a reasonable solution to this problem, I tried to use ifstream, fopen. Reading a file in c ++

int main() { FILE *fp = NULL; char str[3500]; if ((fp = fopen_s("C:\\Users\\I9609\\source\\repos\\lab5\\eng.txt", "r")) == NULL) { cout << "Error of opening the file! "; exit; } fgets(str, 3500, fp); fclose(fp); _getch(); return 0; } 

    2 answers 2

    Signature function:

     errno_t fopen_s( FILE** pFile, const char *filename, const char *mode ); 

    The first parameter is to pass a pointer to the file descriptor:

     FILE *fp; errno_t err; err = fopen_s( &fp, "filename.txt", "r" ); if( err == 0 ) { printf( "Файл открыт\n" ); } else { printf( "Ошибка\n" ); } 

    MSDN

      "If nothing helps, read the documentation" (c)

      And it says:

       errno_t fopen_s(FILE**stream, const char* name. const chhar* mode); 

      ( restrict s lowered.)

      Those. not

       if ((fp = fopen_s("C:\\Users\\I9609\\source\\repos\\lab5\\eng.txt", "r")) == NULL) 

      but

       errno_t err; FILE * fp; err = fopen_s(&fp,"C:\\Users\\I9609\\source\\repos\\lab5\\eng.txt","r"); if (err != 0) .... 

      But even better in C ++, use ifstream ...

      • Just tried to remake it under "errno_t err;", the file does not open :( Maybe you just need to specify the file name? Now I’ll try via ifstream. - John