gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
#include <stdio.h> #include <iostream> #include <io.h> #include <ctime> #include…
- User
#include <stdio.h> #include #include <io.h> #include #include
using namespace std;
string GetLatestFile(const string& folderPath) { string latestFile = " "; time_t latestTime = 0;
_finddata_t fileInfo; intptr_t handle = _findfirst((folderPath + "\ * ").c_str(), &fileInfo); if (handle != -1) { do { if (!(fileInfo.attrib & _A_SUBDIR)) { time_t fileTime = fileInfo.time_create; if (fileTime > latestTime) { latestTime = fileTime; latestFile = fileInfo.name; } } } while (_findnext(handle, &fileInfo) == 0); _findclose(handle); } return latestFile;}
int main() { string folderPath; cout << "Enter the folder path: "; cin >> folderPath;
string latestFile = GetLatestFile(folderPath); if (!latestFile.empty()) { cout << "The latest file in the folder is: " << latestFile << endl; } else { cout << "No files found in the folder." << endl; } return 0;} замени все что с типом данных string на массивы char
- Assistant
#include <stdio.h> #include #include <io.h> #include #include
using namespace std;
char* GetLatestFile(const char* folderPath) { char* latestFile = new char [100]; time_t latestTime = 0;
_finddata_t fileInfo; intptr_t handle = _findfirst((string(folderPath) + "\\*").c_str(), &fileInfo); if (handle != -1) { do { if (!(fileInfo.attrib & _A_SUBDIR)) { time_t fileTime = fileInfo.time_create; if (fileTime > latestTime) { latestTime = fileTime; strncpy(latestFile, fileInfo.name, 100); } } } while (_findnext(handle, &fileInfo) == 0); _findclose(handle); } return latestFile;}
int main() { char folderPath[100]; cout << "Enter the folder path: "; cin >> folderPath;
char* latestFile = GetLatestFile(folderPath); if (*latestFile != '\0') { cout << "The latest file in the folder is: " << latestFile << endl; } else { cout << "No files found in the folder." << endl; } delete[] latestFile; return 0;}