C file operations

Source: Internet
Author: User

1. delete an object

1. c call the doscommand: System (char * cmd); for example, system ("del D: \ Chang \ xxx.txt"); delete the file

2. Call API: delete a file: int remove (char * filename); Return 0 success-1 Failure

3. deletefile ("D: \ new.txt"); // delete an object

 
Bool deletefile (lpctstr filename );

4. _ unlink (filename) // return-1 if the connection fails.

 

Ii. Rename (you can also use the move and copy mentioned later)

Rename (oldname, newname) // if the call succeeds, 0 is returned.

 

3. Create a directory

1. mkdir ("C: \ info \ world"); // returns-1 if the error occurs.

2. Single-level (SA is a security attribute, and null is the default value). You need to customize multi-level directories.

 
Bool createdirectory (lpctstr dirname, lpsecurity SA );

3. Multi-Level

System ("Mkdir c: \ A \ B \ c");

 

4. list file information in a directory

 
VoidListall (Char* Path ){
Win32_find_data finddata;
Handle listfile;
Strcat (path, "\ *"); // indicates listing all files
// Findfirstfile ("D: \ htdocs \ *. txt", & finddata)
Listfile = findfirstfile (path, & finddata );
Do {
If (Lstrcmp (finddata. cfilename, text (" . " ) = 0 |
Lstrcmp (finddata. cfilename, text ( " .. " ) = 0 )
Continue ;
Printf ( " % S \ t " , Finddata. cfilename );
If (Finddata. dwfileattributes & file_attribute_hidden)
Printf ( " Hidden \ t " );
If (Finddata. dwfileattributes & file_attribute_directory)
Printf ( " Dir \ t " );
Printf ( " \ N " );
} While (Findnextfile (listfile, & finddata ));
}

5. File Read modification time

# Include <windows. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <Io. h>
DWORD showfiletime (pfiletime lptime );
Int Main ( Int Argc, Char * Argv [])
{
// File Attribute Structure
Win32_file_attribute_data wfad;
// Get file attributes
Getfileattributesex ( " D: \ htdocs \ index.html " , Getfileexinfostandard, & wfad );

Showfiletime (& (wfad. ftlastwritetime ));
// Showfilesize (wfad. nfilesizehigh, wfad. nfilesizelow );

Return 0 ;
}

DWORD showfiletime (pfiletime lptime ){
Filetime ftlocal;
Systemtime st;
Filetimetolocalfiletime (lptime, & ftlocal );
Filetimetosystemtime (& ftlocal, & St );

Printf ( " % 4D/% 02d/% 02d " , St. wyear, st. wmonth, st. wday );
Return 0 ;
}

6. Read File Size

1.

The third parameter of findfirst can have the following values:

# Define fa_rdonly 0x01/* read only attribute */

# Define fa_hidden 0x02/* hidden file */

# Define fa_system 0x04/* System File */

# Define fa_label 0x08/* volume label */

# Define fa_direc 0x10/* directory */

# Define fa_arch 0x20/* archive */

# Include <windows. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <Io. h>
DWORD showfilesize (DWORD dwhigh, DWORD dwlow );
Int Main ( Int Argc,Char * Argv [])
{
// File Attribute Structure
Win32_file_attribute_data wfad;
// Get file attributes
Getfileattributesex ( " D: \ test.txt " , Getfileexinfostandard, & wfad );

Showfilesize (wfad. nfilesizehigh, wfad. nfilesizelow );

Return 0 ;
}

DWORD showfilesize (DWORD dwhigh, DWORD dwlow ){
Ulonglong lifilesize;
Lifilesize = dwhigh;
Lifilesize <= Sizeof (DWORD )* 8 ;
Lifilesize + = dwlow;

Printf ( " % I64u \ n " , Lifilesize );
Return 0 ;
}

2. directly call system functions for greater convenience. This parameter can be set to null.

 
DWORD getfilesize (handle HF, lpdword hCG );

 

7. traverse all files

# Include <windows. h>
# Include <stdio. h>
# Include <stdlib. h>

DWORD listallfile (lpstr path );
DWORD listallfile (lpstr path ){
Win32_find_data finddata;
Handle listfile;
Char Fullpath [ 100 ];
Char Temppath [ 100 ];

Lstrcpy (temppath, PATH );
Lstrcat (temppath, " \\* " );

Listfile = findfirstfile (temppath, & finddata );
Do {
If (Lstrcmp (finddata. cfilename, text ( " . " ) = 0 |
Lstrcmp (finddata. cfilename, text ( " .. " ) = 0 )
Continue ;
// Printf ("% s \ t", finddata. cfilename );
// Create full path
Wsprintf (fullpath, " % S \ % s " , Path, finddata. cfilename );
Printf ( " % S \ n " , Fullpath );

/* If (finddata. dwfileattributes & file_attribute_hidden)
Printf ("Hidden \ t "); */
If (Finddata. dwfileattributes & file_attribute_directory ){
Listallfile (fullpath );
}
} While (Findnextfile (listfile, & finddata ));

Return 0 ;
}
Int Main ( Int Argc, Char * Argv [])
{
Listallfile ( " E: \ linuxbooks " );
Return 0 ;
}

8. File Read operations

# Include <stdio. h>
# Include <stdlib. h>

IntMain ()
{
IntCh;
File * FP;
Fp = fopen ("Aaa.txt","W");
While(CH = GETC (FP ))! = EOF ){
Putchar (CH );
}
Fclose (FP );
Return 0;
}

 

9. delete a directory

1. rmdir ("UGG"); // delete a directory, empty

2. delete an empty directory.

 
Bool removedirectory (lpctstr path );

3. System ("rmdir/S/q ugg"); // Delete non-empty folders

 

10. Check the file existence and permissions

1.

 
Int_ Access (Const Char* Path,IntMode );
Mode Value Checks file
00Existence only
02Write Permission
04Read Permission
06Read and Write Permission

2. User-Defined Functions

 Bool fileexists (lpctstr filename, bool dircheck) {DWORD DWA = Getfileattributes (filename );  If (DWA =0 xffffffff  ){  /*  Error  */          Return  False ;}  /*  Is a dir  */      If (DWA & file_attribute_directory) = File_attribute_directory ){  If  (Dircheck ){  Return True ;}  Else  {  Return  False ;}}  Else  {  /*  Is a file  */          If (! Dircheck ){  Return  True ;}  Else  { Return  False ;}}} 

 

11. Copying and Moving Files

 //  Not Overwrite  If (Copyfile ( "  D: \ test.txt  " , "  C: \ test.txt  "  , True) puts (  "  File copied  " );  If (Copyfile ( "  D: \ test.txt  " , "  C: \ test.txt  "  , False) puts (  "  File overwritten  "  );  If (Movefile ( "  D: \ test.txt " , "  C: \ test.txt  "  ) Puts (  "  Move  " );

Another mobile function prototype (overwritable) dwflags: movefile_replace_existing (replace the original file)

 
Bool movefileex (maid SRC, maid, DWORD dwflags );

 

 

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.