Help to write a console application in Visual Studio:

  1. ask for a directory
  2. run through all files (file folders) in the directory specified in item 1
  3. add each file to the structure ( name, saiz, address ) // name. file extension, size, path to it
  4. sort the structure elements by имя
  5. we check each element in turn with the following ones up to the first mismatch by name (we check elements with identical names by size) // find duplicates
  6. display duplicates // address 1, address 2, size of duplicates

I really need help ... I haven’t learned how to program myself yet, I can only think so far)

  • one
    And if you say the truth, the teacher gave the task to be solved. since for a person far from programming, your 3 point will be a mystery. Start writing yourself, but if something does not work out then ask for help, and no one will do it for you. - SoftR
  • Yes, by the way, and how to open a folder in C ++ and find out its contents? - Surendil

2 answers 2

The oldest way to browse the directory is to use _findfirst, _findnext from "io.h"

In the loop, we check the specified directory, check each found element for the presence of the _A_SUBDIR bit in the attributes, and if so, pass recursively into this subdirectory

 #include <io.h> void listdir(char *root) { _finddata_t fd; int rc; long hFile; char mask[260]; char subdir[260]; sprintf(mask, "%s\\*.*", root); for( rc = (hFile = _findfirst(mask, &fd)); rc != -1; rc = _findnext(hFile, &fd) ) { if ( fd.name[0] != '.' ) { printf("\n%s\\%s", root, fd.name); if ( fd.attrib & _A_SUBDIR ) { sprintf(subdir, "%s\\%s", root, fd.name); listdir(subdir); } } } 

}

 int main() { char root[260] = "d:\\temp"; listdir(root); return 0; 

}

  • I did not know, but now I will use =) Thank you! - Surendil
  • Thank! and now how to put each file into the structure (name, size, address?) - denangel
  • Try it yourself. And then if you answer this, then the next question will be "how to sort." And you will receive a mark for a lab as a result, but not @renegator. - yozh
  • Well, it's really quite simple. The name is in fd.name, the size in fd.size, the path is the value of root - renegator

Why sort?

Make a hash table by file names, each element is a list header of your structures (size, attr, path ...). Placed in the table all the files.

Run through it, print from lists of 2 or more. Only with synonyms (by hash-code), carefully understand if all synonyms are put in one list.

It will be faster than sorting.

  • I need speed))) I need any speed to work ...) - denangel
  • Then the simplest (and slowest). Read all the files in the array (as needed realloc () it). Alternately take the elements of the array and compare with all from the next to the end of the array. Found a duplicate - printed and crossed out (for example, the first byte of the name was reset). Everything. (Write the code yourself?) - avp
  • Stupidly with the following will not work, several identical ones may go in a row. Anyway nested loop to do. Sorting (if you still think of doing it) do not write yourself yet . Use qsort (). (See man qsort). - avp
  • Split the task into clearly defined parts (they will become functions in your program). For each part (if you do not decide for a day), formulate a specific question. It’s better to try to do something, and then ask, give pieces of code and what is the problem (what they wanted to see and what they see). Well, like that. This is my last comment here (Limit-s). - avp