导航:首页 > 文件处理 > 使用c语言实现打开文件夹

使用c语言实现打开文件夹

发布时间:2022-08-14 19:09:58

‘壹’ C语言怎么读取某一文件夹下的所有文件夹和文件

读取的代码方式如下:

int main()

{

long file;

struct _finddata_t find;

_chdir("d:\");

if((file=_findfirst("*.*", &find))==-1L)

{

printf("空白! ");

exit(0);

}

printf("%s ", find.name);

while(_findnext(file, &find)==0)

{

printf("%s ", find.name);

}

_findclose(file);

return 0;

}

‘贰’ 如何用c语言打开一个文件

近用c语言做文件操作比较频繁,记几个常用的操作

获得文件大小:

fseek(fp, 0, SEEK_END);
int fileSize = ftell(fp);
rewind(fp);

读取指定位置的数据块:

fseek( fp,offset,SEEK_SET );
int num_read = fread(buf, 1, length, fp);

删除文件

int res = access( filename,0 ); // 判断文件是否存在
if ( res == 0 )
{
res = remove( filename );// 删除文件
return ( res ==0 );
}

在指定位置写入块数据:

fseek( fp, offset, SEEK_SET );
num_write = fwrite( buf, 1, n, fp );

打开文件方式中有一个比较特别的,如果 某文件中已经有了一部分数据,你需要继续在上面添加数据,但是是在指定位置添加,也就是说,仍然需要通过 fseek 找到写入位置,然后再 fwrite,这时候需要以 "rb+" 方式打开。而不能以"a"或者"ab+"方式。以"a"方式打开,fseek函数不起作用。

获得文件属性

struct stat st;
FILE *fp = fopen( filename.c_str(),"rb" );
if ( !fp )
{ // error
}
fstat( fp->_file, &st );

遍历目录

std::string dirspec = dir + "\\*.*";
struct _finddata_t filefind;
int done = 0;
intptr_t handle = 0;

if( ( handle = _findfirst(dirspec.c_str(),&filefind) ) == -1 )
return IVS_FAIL;

IVS_RESULT res = IVS_OK, response =IVS_OK;
while( !(done=_findnext(handle,&filefind)) )
{
if( !strcmp(filefind.name,"..") || !strcmp(filefind.name,".") )
continue;

AdsFileInfo info;
if((_A_SUBDIR==filefind.attrib))
{
info._filename = filefind.name;
info._fileSize = filefind.size;
info._atime = filefind.time_access;
info._ctime = filefind.time_create;
info._mtime = filefind.time_write;
info._isdir = true;
}
else
{
std::string tmpFilename = dir + "\\";
tmpFilename += filefind.name;
res = getFileInfo( tmpFilename, info );
response = (!SUCCESS(res))?res: response;
}
list.push_back( info );
}
_findclose(handle);

‘叁’ 关于c语言中如何打开文件

//zifu.txt文件只能由ASCII字符组成,否则会出现乱码
#include<stdio.h>

int main()
{
char str[200],a,b;
FILE *p;
int i;

if(NULL==(p=fopen("zifu.txt","r+")))
{
printf("文件打开失败!\n");
}
else
{
fscanf(p,"%s",str);
printf("你要替换的字符:");
a=getchar();
getchar();
printf("你要替换成的字符:");
b=getchar();
getchar();
for(i=0;str[i];i++)
{
if(a==str[i])
str[i]=b;
}
printf("%s\n",str);
fprintf(p,"%s",str);
fclose(p);
}
return 0;
}

‘肆’ 如何使用c语言打开一个文件

#include<stdio.h>
intmain(intargc,char*argv[])
{
FILE*fp=fopen(argv[1],"w+");//以读写方式创建一个文本文件,其中文件名由参数argv[1]提供
if(fp==NULL)exit(0);//出错检查,如果打开失败,打开函数返回一个空指针,则退出程序
...
...
fclose(fp);//运行完毕后一定不要忘记关闭文件
return0;
}

运行:

  1. 打开cmd.exe,输入程序名(必须有路径) (空格) 打开的文件名 (回车)。注意,如果程序名或者文件名内部有空格,必须用双引号引起来

  2. 之后便可以运行下面的内容

‘伍’ c语言如何用fopen打开另一相对路径下的文件夹

这种情况既可以转换成绝对路径,也可以切换到那个目录下,也可以在这个目录到那个目录的相对路径。

‘陆’ 用C语言的函数创建、打开和读写文件

用_mkdir即可,不过需要调用direct.h头文件,下面举个例子
#include
#include
#include
int
main(
void
)
{
if(
_mkdir(
"\\testtmp"
)
==
0
)//0表示创建成功
{
printf(
"directory
'\\testtmp'
was
successfully
created\n"
);
system(
"dir
\\testtmp"
);//执行dos语句打开文件夹
if(
_rmdir(
"\\testtmp"
)
==
0
)//删除成功
printf(
"directory
'\\testtmp'
was
successfully
removed\n"
);
else
printf(
"problem
removing
directory
'\\testtmp'\n"
);
}
else
printf(
"problem
creating
directory
'\\testtmp'\n"
);
}

‘柒’ c语言怎么打开一个文件夹

在C语言中,对文件夹的操作,专业的说法称为"切换路径/目录",而不是"打开",因为文件夹,并不是一个"真正的文件",而只是一个访问文件的目录.

用C语言中的函数chdir,也就是change directory
int chdir(char *path)
-- 使指定的目录path变成当前的工作目录,之后所有的文件操作都是该目录下.

比如,想切换到f盘test目录下可以这样:
chdir("f:\\test ");
返回0表示切换成功,否则,表示失败.

‘捌’ c语言如何打开文件

如果对楼主有帮助,给个采纳好不,谢谢啦

[cpp]view plain

‘玖’ C语言打开一个文件夹

#include <stdio.h>
#include <windows.h>
int main()
{
char ch[]="12345",str[32]={NULL};
scanf("%s",str);//密码就是上边的ch保存的12345
if (strcmp(ch,str)==0)//密码正确就执行打开
{
WinExec("explorer.exe C:\\Windows\\System32",1);//打开指定文件夹
}
else
return -1;//错误就退出
return 0;
}
//如楼主所说,帮写了小段代码

阅读全文

与使用c语言实现打开文件夹相关的资料

热点内容
艾默生压缩机说明书 浏览:289
超解压手法 浏览:415
如何获取服务器上的文件地址 浏览:679
文件夹题用另存为吗 浏览:639
各种编译类型为自然选择提供了 浏览:914
cnc玻璃精雕机编程 浏览:313
电脑复制中途改文件夹名字 浏览:498
批处理转exe反编译工具 浏览:76
pdf怎么换成图片 浏览:323
换位加密能够按照一定 浏览:390
安卓开发入门pdf 浏览:192
日医pdf 浏览:861
指定文件夹换壁纸 浏览:898
天玥服务器是什么架构 浏览:236
苹果为什么回购安卓手机 浏览:87
27岁程序员发型 浏览:198
图库文件夹是什么意思 浏览:532
空调压缩机隔音 浏览:352
簿荷阅读app为什么登陆不了 浏览:517
zigbee与单片机通信 浏览:440