Загрузка...

Friends - help make a lab - tomorrow is the deadline ... Save (

Thread in C/C++ created by IZI_ACC Dec 6, 2022. 257 views

  1. IZI_ACC
    IZI_ACC Topic starter Dec 6, 2022 0 Dec 18, 2020
    Задача написать программу, которая будет находить введенный нами файл(вводим название И разрешение) на всем компьютере. Если есть такой файл, выводит на экран «такой файл имеется». В противном случае выводим на экран «данный файл отсутствует».

    Ребят, помогите пожалуйста, ничего не получается, завтра надо сдавать...
     
  2. IZI_ACC
    IZI_ACC Topic starter Dec 6, 2022 0 Dec 18, 2020
    #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(
     
  3. teresen554
    teresen554 Dec 6, 2022 59 Sep 22, 2022
    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
    Попробуй это.
     
  4. Joker9019
    Joker9019 Dec 7, 2022 Banned 3 Nov 27, 2022
    IZI_ACC,
    C
    #include &lt;iostream&gt;

    #include &lt;string&gt;

    #include &lt;fstream&gt;

    #include &lt;windows.h&gt;

    using namespace std;

    int main()

    {

    SetConsoleCP(1251);

    SetConsoleOutputCP(1251);

    string name, name1;

    cout &lt;&lt; "Введите название файла: ";

    cin &gt;&gt; name;

    cout &lt;&lt; "Введите расширение файла: ";

    cin &gt;&gt; name1;

    ifstream file(name + "." + name1);

    if (file.is_open())

    {

    cout &lt;&lt; "Такой файл имеется" &lt;&lt; endl;

    }

    else

    {

    cout &lt;&lt; "Данный файл отсутствует" &lt;&lt; endl;

    }

    system("pause");

    return 0;

    }
     
  5. IZI_ACC
    IZI_ACC Topic starter Dec 7, 2022 0 Dec 18, 2020
    Успел сдать)
     
    1. teresen554
      Всё правильно? Ошибок не было?@IZI_ACC,
    2. IZI_ACC Topic starter
      teresen554, были) Всегда писал, что нет такого файла, но препод сжалился) Спасибо еще раз))
Top
Loading...