C++中如何获取文件大小的总结
来源:互联网 发布:js获取div宽度 编辑:程序博客网 时间:2024/06/05 16:17
转载自 http://www.soft568.com/info/detail/4-5662.html
(一)
对文件操作时有时获得文件的大小时必要的.下面是获得其大小小的较简单方法.
#include<io.h> //C语言头文件
#include<iostream> //for system();
using namespace std;
int main()
{
int handle;
handle = open("test.txt", 0x0100); //open file for read
long length = filelength(handle); //get length of file
cout<<"file length in bytes:"<<length<<endl;
close(handle);
system("pause");
return 0;
}
#include<io.h> //C语言头文件
#include<iostream> //for system();
using namespace std;
int main()
{
int handle;
handle = open("test.txt", 0x0100); //open file for read
long length = filelength(handle); //get length of file
cout<<"file length in bytes:"<<length<<endl;
close(handle);
system("pause");
return 0;
}
(二)
//用Windows API 中的 GetFileSize()获得文件长度
//假设文件file.txt 在当前目录下
//file.txt的内容为:123abc
//关于windows API函数情参考部分windows API函数或MSDN
#include <iostream>
#include <windows.h> //for windows api
using namespace std;
int main()
{
//用API函数CreateFile()创建文件句柄
HANDLE fhadle = CreateFile("file.txt", //文件名或路径
0,
0,
0,
OPEN_EXISTING, //文件存在则打开并读取
0,
0);
DWORD size = GetFileSize(fhadle,0);
cout<<"size:"<<size<<endl;
return 0;
}
//输出:
size:6
//假设文件file.txt 在当前目录下
//file.txt的内容为:123abc
//关于windows API函数情参考部分windows API函数或MSDN
#include <iostream>
#include <windows.h> //for windows api
using namespace std;
int main()
{
//用API函数CreateFile()创建文件句柄
HANDLE fhadle = CreateFile("file.txt", //文件名或路径
0,
0,
0,
OPEN_EXISTING, //文件存在则打开并读取
0,
0);
DWORD size = GetFileSize(fhadle,0);
cout<<"size:"<<size<<endl;
return 0;
}
//输出:
size:6
(三)
//假设文件file.txt存在,且在当前目录下
#include <iostream>
#include <fstream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
ifstream in("file.txt");
in.seekg(0, ios::end); //设置文件指针到文件流的尾部
streampos ps = in.tellg(); //读取文件指针的位置
cout << "File size: " << ps << endl;
in.close(); //关闭文件流
return 0;
}
int main(int argc, char* argv[])
{
ifstream in("file.txt");
in.seekg(0, ios::end); //设置文件指针到文件流的尾部
streampos ps = in.tellg(); //读取文件指针的位置
cout << "File size: " << ps << endl;
in.close(); //关闭文件流
return 0;
}
以上部分转自http://blog.sina.com.cn/u/1066677252
(四)
在VC中,CFILE的GetLength()函数可直接得到大小:
CFile cf;
// Attempt to open thefile for reading.
if( !cf.Open( pszFilename, CFile::modeRead ) )
return( FALSE );
if( !cf.Open( pszFilename, CFile::modeRead ) )
return( FALSE );
// Get the size of the file and store
// in a local variable. .
DWORD dwSize;
dwDibSize = cf.GetLength();
// in a local variable. .
DWORD dwSize;
dwDibSize = cf.GetLength();
(五)
#include <sys/types.h>;
#include <sys/stat.h>;
long
get_filesize(char *filename)
{
struct stat f_stat;
if (stat(filename, &f_stat) == -1) {
return -1;
}
return (long)f_stat.st_size;
}
#include <sys/types.h>;
#include <sys/stat.h>;
long
get_filesize(char *filename)
{
struct stat f_stat;
if (stat(filename, &f_stat) == -1) {
return -1;
}
return (long)f_stat.st_size;
}
(六)
int main(void)
{
struct stat statbuf;
FILE *stream;
/* open a file for update */
if ((stream = fopen(FILENAME, "w+")) == NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return(1);
}
/* get information about the file */
stat(FILENAME, &statbuf);
fclose(stream);
/* display the information returned */
if (statbuf.st_mode & S_IFCHR)
printf("Handle refers to a device.\n");
if (statbuf.st_mode & S_IFREG)
printf("Handle refers to an ordinary file.\n");
if (statbuf.st_mode & S_IREAD)
printf("User has read permission on file.\n");
if (statbuf.st_mode & S_IWRITE)
printf("User has write permission on file.\n");
printf("Drive letter of file: %c\n", 'A'+statbuf.st_dev);
printf("Size of file in bytes: %ld\n", statbuf.st_size);
printf("Time file last opened: %s\n", ctime(&statbuf.st_ctime));
return 0;
}
{
struct stat statbuf;
FILE *stream;
/* open a file for update */
if ((stream = fopen(FILENAME, "w+")) == NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return(1);
}
/* get information about the file */
stat(FILENAME, &statbuf);
fclose(stream);
/* display the information returned */
if (statbuf.st_mode & S_IFCHR)
printf("Handle refers to a device.\n");
if (statbuf.st_mode & S_IFREG)
printf("Handle refers to an ordinary file.\n");
if (statbuf.st_mode & S_IREAD)
printf("User has read permission on file.\n");
if (statbuf.st_mode & S_IWRITE)
printf("User has write permission on file.\n");
printf("Drive letter of file: %c\n", 'A'+statbuf.st_dev);
printf("Size of file in bytes: %ld\n", statbuf.st_size);
printf("Time file last opened: %s\n", ctime(&statbuf.st_ctime));
return 0;
}
(七)
#include <sys\stat.h>;
#include <string.h>;
#include <stdio.h>;
#include <fcntl.h>;
#include <io.h>;
int main(void)
{
int handle;
char msg[] = "This is a test";
char ch;
/* create a file */
handle = open("TEST.$$$", O_CREAT | O_RDWR, S_IREAD | S_IWRITE);
/* write some data to the file */
write(handle, msg, strlen(msg));
/* seek to the beginning of the file */
lseek(handle, 0L, SEEK_SET);
/* reads chars from the file until we hit EOF */
do
{
read(handle, &ch, 1);
printf("%c", ch);
} while (!eof(handle));
close(handle);
return 0;
}
#include <string.h>;
#include <stdio.h>;
#include <fcntl.h>;
#include <io.h>;
int main(void)
{
int handle;
char msg[] = "This is a test";
char ch;
/* create a file */
handle = open("TEST.$$$", O_CREAT | O_RDWR, S_IREAD | S_IWRITE);
/* write some data to the file */
write(handle, msg, strlen(msg));
/* seek to the beginning of the file */
lseek(handle, 0L, SEEK_SET);
/* reads chars from the file until we hit EOF */
do
{
read(handle, &ch, 1);
printf("%c", ch);
} while (!eof(handle));
close(handle);
return 0;
}
八 这种方法刚在Dev-cpp上实验过
#include <iostream>
#include <fstream>
#include <fstream>
using namespace std;
int main(int argc, char * argv[])
{
if (argc>1)
{
cout<<argv[1]<<endl;
ifstream in(argv[1]);
if (!in) return (-1);
in.seekg(0, ios_base::end);
streampos pos=in.tellg();
cout<<"file size "<<pos<<endl;
}
else return -1;
return 0;
}
{
if (argc>1)
{
cout<<argv[1]<<endl;
ifstream in(argv[1]);
if (!in) return (-1);
in.seekg(0, ios_base::end);
streampos pos=in.tellg();
cout<<"file size "<<pos<<endl;
}
else return -1;
return 0;
}
- C++中如何获取文件大小的总结
- C语言中获取文件大小的方法
- C语言中获取文件大小的函数
- C语言如何获取文件大小
- C和C++获取文件大小的方法总结(转)
- C和C++获取文件大小的方法总结
- C和C++获取文件大小的方法总结
- C语言中如何获得文件大小
- C/C++获取文件大小的多种方法
- linux下C获取文件大小的方法
- C/C++ 获取文件大小
- linux获取文件大小[c]
- c获取文件大小
- c语言文件大小获取
- C语言获取文件大小
- LINUX C获取文件大小
- C语言获取文件大小
- C语言获取文件大小
- 安卓-文件读写
- JAVA对象与引用。
- B树、B-树、B+树、B*树
- 图像/视觉显著性检测技术发展情况梳理--计算机视觉专题
- Linux系统时间偏差的纠正 adjtimex
- C++中如何获取文件大小的总结
- Linux中Samba详细安装
- 千里之行,始于足下
- platform_device与platform_driver
- CodeIgniter 应用开发笔记 - 1
- 360黑匣子之谜
- Android开发效率—Eclipse快捷键
- Oracle database 15章 恢复管理器 连接到RMAN 理论试验
- CSS的三种扩展选择器