在C ++中对文件进行操作需要帮助 [英] operating on files in C++ need help

查看:56
本文介绍了在C ++中对文件进行操作需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名C程序员,也是C ++的新手。在C中,我熟悉文件中的一些

操作。现在当使用iostream从c转换到c ++时,我不知道如何执行以下操作


1.检查文件或目录是否存在或者不是

2.删除给定目录下的所有文件

3.重命名

解决方案

< blockquote> Rex_chaos写道:

我是AC程序员,也是C ++的新手。在C中,我熟悉文件中的一些操作。现在当使用iostream从c转换到c ++时,我不知道如何执行以下操作

1.检查文件或目录是否存在


目录:没有标准的方法来做到这一点。既不是C语言,也不是C ++语言。 POSIX

提供了一些东西,Boost有一个相当便携的库。


文件:再次,没有便携式方式AFAIK。你可以尝试打开它。如果

你可以,它就在那里。如果你不能,它可能不存在或你没有

权利或......

2.删除给定目录下的所有文件


没有便携方式。 POSIX或Boost再次提供帮助:
http:/ /www.boost.org/libs/filesystem/doc/index.htm

3.重命名




std ::重命名,如在C.


-

WW aka Attila


>我是一名c程序员,也是C ++的新手。在C中,我熟悉文件中的一些

操作。现在当使用iostream从c转换到c ++时,我不知道如何执行以下操作

1.检查文件或目录是否存在


std :: ifstream check(" file.ext");


if(!check)

std :: cout< ;< 文件不存在;

2.删除给定目录下的所有文件


否,除非您有文件列表。从< cstdio>删除单个文件


std :: remove(" filename");


IIRC。您将不得不使用一些特定于实现的

函数来获取有关文件系统的更多信息。

3.重命名




否,除非通过新名称复制然后删除旧的



Jonathan

Jonathan Mcdougall写道:

我是AC程序员,也是C ++的新手。在C中,我熟悉文件中的一些操作。现在当从c转换到c ++时,使用iostream,
我不知道如何执行以下操作

1.检查文件或目录是否存在



std :: ifstream check(" file.ext");

if(!check)
std :: cout<< 文件不存在;




哪些因其他原因而失败。没有权利,临时网络

问题,文件由其他人等专门打开等。

3。重命名



不,除非通过新名称复制然后删除旧的名称。




如何关于cstdio.h中的std :: rename函数?


-

WW aka Attila


I am a c programmer and new to C++. In C, I am familiar with some
operations on file. Now when shifting from c to c++, using iostream, I
have no idea how to perform the following operations

1. check if a file or directory exists or not
2. delete all the files under a given directory
3. rename

解决方案

Rex_chaos wrote:

I am a c programmer and new to C++. In C, I am familiar with some
operations on file. Now when shifting from c to c++, using iostream, I
have no idea how to perform the following operations

1. check if a file or directory exists or not
Directory: no standard way to do it. Neither in C nor in C++. POSIX
provides something and Boost has a fairly portable library.

Files: again, there is no portable way AFAIK. You can try to open it. If
you can, it is there. If you cannot, it might not be there or you have no
rights or...
2. delete all the files under a given directory
No portable way. Again POSIX or Boost can help:
http://www.boost.org/libs/filesystem/doc/index.htm
3. rename



std::rename, as in C.

--
WW aka Attila


> I am a c programmer and new to C++. In C, I am familiar with some

operations on file. Now when shifting from c to c++, using iostream, I
have no idea how to perform the following operations

1. check if a file or directory exists or not
std::ifstream check("file.ext");

if ( ! check )
std::cout << "file does not exist";
2. delete all the files under a given directory
No, unless you have the file list. Remove a single file with

std::remove("filename");

from <cstdio> iirc. You''ll have to use some implementation-specific
functions to get more informations about the file system.
3. rename



No, except by copying under a new name and then remove the old
one.
Jonathan


Jonathan Mcdougall wrote:

I am a c programmer and new to C++. In C, I am familiar with some
operations on file. Now when shifting from c to c++, using iostream,
I have no idea how to perform the following operations

1. check if a file or directory exists or not



std::ifstream check("file.ext");

if ( ! check )
std::cout << "file does not exist";



Which can fail for a thousand other reasons. No rights, temporary network
problem, file is opened exclusively by someone else etc. etc. etc.

3. rename



No, except by copying under a new name and then remove the old
one.



How about the std::rename function in cstdio.h?

--
WW aka Attila


这篇关于在C ++中对文件进行操作需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆