How to obtain the file name system dir in C

Source: Internet
Author: User

Author: Qing Dujun


We know that DIR is the display directory. But how do I save the file name in the directory?

Several knowledge points are required:

I,DIR: Displays the file name (including summary) in the directory)

II,DIR/B: Only show the file name

III,DIR/B> 1.txt: Import the contents to the 1.txt file for storage.

IV,Del d: \ 1.txt: Delete D: The 1.txt file under the drive (if you want to delete the folder, use rm D: folder name)

V,DEL/q d: \ 1.txt: Force Delete, no need to confirm

VI,Fgets (): Reads a row from a file at a time.


Example: output the name of the bottom file in the XXX directory

Program ideology:

First, store the directory in the file, and then read the file until the last one.

# Include
 
  
# Include
  
   
Int main () {FILE * fp = NULL; char save [100]; system ("dir/B E: \ Ock \ DirSave> D: \ buf. ock "); // export the directory to buf. ockfp = fopen ("D: \ buf. ock "," rb "); while (! Feof (fp) {fgets (save, 100, fp);} fclose (fp); system ("del/q d: \ buf. ock "); // delete D: buf under the directory. ock file puts (save); // display the last file name return 0 ;}
  
 


Of course, you can obtain the path required by DIR from the parameters of the main () function:

#include 
 
  int main(int argc, char **argv){puts(argv[0]);return 0;}
 

Argv [0] content. This is the current directory. You can extract the desired path by yourself ~~~~


Use DIR/B D: to display the file directory diagram.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.