我们将讨论以下列出的四个文件黑客:
- 重命名–使用C / C ++重命名文件
- 删除–使用C / C ++删除文件
- 文件大小–使用C / C ++获取文件大小
- 检查是否存在–检查C / C ++中是否存在文件
// A C++ Program to demonstrate the
// four file hacks every C/C++ must know
// Note that we are assuming that the files
// are present in the same file as the program
// before doing the below four hacks
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
// A Function to get the file size
unsigned long long int fileSize( const char *filename)
{
// Open the file
FILE *fh = fopen (filename, "rb" );
fseek (fh, 0, SEEK_END);
unsigned long long int size = ftell (fh);
fclose (fh);
return (size);
}
// A Function to check if the file exists or not
bool fileExists( const char * fname)
{
FILE *file;
if (file = fopen (fname, "r" ))
{
fclose (file);
return ( true );
}
return ( false );
}
// Driver Program to test above functions
int main()
{
printf ( "%llu Bytes\n" , fileSize( "Passwords.txt" ));
printf ( "%llu Bytes\n" , fileSize( "Notes.docx" ));
if (fileExists( "OldData.txt" ) == true )
printf ( "The File exists\n" );
else
printf ( "The File doen't exist\n" );
rename ( "Videos" , "English_Videos" );
rename ( "Songs" , "English_Songs" );
remove ( "OldData.txt" );
remove ( "Notes.docx" );
if (fileExists( "OldData.txt" ) == true )
printf ( "The File exists\n" );
else
printf ( "The File doesn't exist\n" );
return 0;
}
输出如下:
执行程序前的屏幕截图:
执行程序后的屏幕截图:
本文作者:拉奇特·贝尔瓦里亚(Rachit Belwariar)。如果你喜欢lsbin并希望做出贡献, 那么你也可以写一篇文章并将你的文章邮寄到lsbin。查看你的文章出现在lsbin主页上, 并帮助其他Geeks。
如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请发表评论。