Задача написать программу, которая будет находить введенный нами файл(вводим название И разрешение) на всем компьютере. Если есть такой файл, выводит на экран «такой файл имеется». В противном случае выводим на экран «данный файл отсутствует». Ребят, помогите пожалуйста, ничего не получается, завтра надо сдавать...
#include<iostream> #include<dirent.h> using namespace std; int main() { setlocale(LC_ALL, "Ru"); DIR* directory; // creating pointer of type dirent struct dirent* x; // pointer represent directory stream cout << "Please enter file name with its extension" << endl; string s; //declaring string variable cin >> s; // taking string as input or file name with input bool result = false; //declaring string variable and assign it to false. if ((directory = opendir("C:\\Users\\MyPC\\Desktop\\codespeedy")) != NULL) { // check if directory open while ((x = readdir(directory)) != NULL) { { if (s == x->d_name) { result = true; //if file found then assign result to false. break; // break the loop if file found. } } } closedir(directory); //close directory.... } if (result) // if file is present then.... { cout << "Такой файл имеется" << endl; } else //if file is not present.... { cout << "Файл отсутствует" << endl; } The post was merged to previous Dec 6, 2022 написали так с соседом в общаге, ругается на библиотеку dirent(
#include <iostream> #include <cstring> #include <dirent.h> #include <cstdlib> using namespace std; int main() { string fileName; string fileExtension; bool fileExists = false; cout << "Введите название файла: "; cin >> fileName; cout << "Введите расширение файла: "; cin >> fileExtension; DIR *folder; struct dirent *entry; folder = opendir("."); if(folder == NULL) { perror("Error"); exit(1); } while((entry = readdir(folder)) != NULL) { if((strcmp(entry->d_name, fileName.c_str()) == 0) && (strcmp(strrchr(entry->d_name, '.'), fileExtension.c_str()) == 0)) { fileExists = true; break; } } if(fileExists) { cout << "Такой файл имеется" << endl; } else { cout << "Данный файл отсутствует" << endl; } closedir(folder); return 0; } C #include <iostream> #include <cstring> #include <dirent.h> #include <cstdlib> using namespace std; int main() { string fileName; string fileExtension; bool fileExists = false; cout << "Введите название файла: "; cin >> fileName; cout << "Введите расширение файла: "; cin >> fileExtension; DIR *folder; struct dirent *entry; folder = opendir("."); if(folder == NULL) { perror("Error"); exit(1); } while((entry = readdir(folder)) != NULL) { if((strcmp(entry->d_name, fileName.c_str()) == 0) && (strcmp(strrchr(entry->d_name, '.'), fileExtension.c_str()) == 0)) { fileExists = true; break; } } if(fileExists) { cout << "Такой файл имеется" << endl; } else { cout << "Данный файл отсутствует" << endl; } closedir(folder); return 0; } The post was merged to previous Dec 6, 2022 Попробуй это.
IZI_ACC, #include <iostream> #include <string> #include <fstream> #include <windows.h> using namespace std; int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); string name, name1; cout << "Введите название файла: "; cin >> name; cout << "Введите расширение файла: "; cin >> name1; ifstream file(name + "." + name1); if (file.is_open()) { cout << "Такой файл имеется" << endl; } else { cout << "Данный файл отсутствует" << endl; } system("pause"); return 0; } C #include <iostream> #include <string> #include <fstream> #include <windows.h> using namespace std; int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); string name, name1; cout << "Введите название файла: "; cin >> name; cout << "Введите расширение файла: "; cin >> name1; ifstream file(name + "." + name1); if (file.is_open()) { cout << "Такой файл имеется" << endl; } else { cout << "Данный файл отсутствует" << endl; } system("pause"); return 0; }