C++获取文件具体方法详解

开发 后端
C++编程语言对于文件的操作是一个比较基础而且重要的操作。我们今天将会为大家详细介绍其中C++获取文件的像相关操作,方便大家理解。

在这篇文章中,我们将会为大家详细介绍一下有关C++获取文件的相关方法。对于刚刚接触C++编程语言不久的朋友们来说,这篇文章介绍的内容可以帮助他们解决一些在文件操作中经常遇到的难题。

  1. /*read File*/  
  2. char *txt = NULL;  
  3. long txtlen;  
  4. //seek to file end to calculate file length  
  5. fseek(fp,0,SEEK_END);  
  6. txtlen=ftell(fp);  
  7. //rewind to file start  
  8. rewind(fp);  
  9. //read from file  
  10. txt = new char[txtlen + 1];  
  11. if (txt != NULL)   
  12. {  
  13. fread(txt,sizeof(char),txtlen,fp);  
  14. txt[txtlen]='\0';  
  15. fv.setData(txt);  
  16. }  
  17. //close file and destroy temp array  
  18. fclose(fp);  
  19. if(txt!=NULL)  
  20. {  
  21. delete []txt;  
  22. txt = NULL;  

C++获取文件的写法:

  1. /*read File*/  
  2. ifstream in(filesrc);  
  3. if(in.fail())  
  4. {  
  5. printf("open file failed!\n");  
  6. }  
  7. else  
  8. {  
  9. string strtmp;  
  10. while (getline(in,strtmp))  
  11. {  
  12. fv.getData()+=strtmp;  
  13. fv.getData()+='\n';  
  14. }  
  15. in.close();  

以上就是我们为大家介绍的C++获取文件相关方法。

【编辑推荐】

  1. C++ makefile写法标准格式简介
  2. C++统计对象个数方法详解
  3. C++ #define预处理指令特点评比
  4. C++二维数组初始化相关应用技巧分享
  5. C++模拟event关键字具体实现方案
责任编辑:曹凯 来源: 博客园
相关推荐

2009-11-23 19:16:32

路由器测试

2009-12-03 18:23:23

2010-02-04 11:23:25

C++反射机制

2009-11-23 15:10:28

PHP获取当前url

2010-07-21 14:33:01

删除telnet

2010-02-03 13:26:53

C++计时

2010-02-01 17:02:53

C++产生随机数

2010-02-03 09:59:42

C++文件流操作

2011-04-01 13:34:12

zabbix

2009-12-16 15:04:26

Ruby实现strea

2009-12-31 15:05:00

Silverlight

2009-12-31 14:12:40

Silverlight

2009-12-03 14:43:46

phpMyAdmin配

2010-02-02 18:01:47

C++字符串替换函数

2010-02-01 14:53:42

C++属性

2010-04-23 13:53:29

Oracle密码

2009-12-01 10:11:51

PHP自动获取关键词

2009-12-25 16:52:57

网络接入控制

2010-02-02 13:57:31

C++解析#pragm

2009-12-18 16:56:05

ADO.NET应用程序
点赞
收藏

51CTO技术栈公众号