c++ 怎样打开 文件夹

来源:百度知道 编辑:UC知道 时间:2024/05/11 19:29:47
我只知道打开文件,比如说打开.exe等文件,
我是用system();函数打开的。
但是比如说我打开一个.html文件时,
程序会先打开一个cmd.exe,再用cmd.exe打开html文件。
所以,桌面上会有一个cmd的窗口,
有什么办法可以直接打开,或者可以看不到cmd窗口???

还有我打开一个文件夹的话,又要怎么做??
比如说:我只想打开c:\windows;
如果我用system("c:\\windows");
这样是打不开的 ,有什么办法??

打开文件夹??
system是控制台函数,可以用explorer c:\\windows实现“打开”文件夹。

在C++中打开是指将文件以流或者块的形式载入到内存中,来实现IO操作的,你说的打开和C++的打开是不同的

system是阻塞式函数调用,会陷进去的。不想看到dos模拟器,就不能用system函数,这个函数在不同系统中的行为很不一样的。打开目录应该用DIR结构体,用opendir系统调用,用readdir读取,用closedir关闭。例子如下:

#include<stdio.h>
#include<unistd.h>
#include<dirent.h>
int main(int argc, char *argv[])
{
DIR *pDir;
struct dirent *pDirEntry;
const char *directory = argc == 2 ? argv[1] : ".";
if ((pDir = opendir(directory)) == NULL)
err_sys("Can not open directory %s", directory);
while ((pDirEntry = readdir(pDir)) != NULL) {
printf("%8ld\t%d\t%s\n", pDirEntry->d_ino, // inode number
pDirEntry->d_type, // type of file
pDirEntry->d_name); // filename
/* call stat() function to get all the attributes of the f