‘壹’ 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;
}
运行:
打开cmd.exe,输入程序名(必须有路径) (空格) 打开的文件名 (回车)。注意,如果程序名或者文件名内部有空格,必须用双引号引起来
之后便可以运行下面的内容
‘伍’ 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
#include<commdlg.h>
OPENFILENAMEofn;
charszFile[MAX_PATH];
ZeroMemory(&ofn,sizeof(ofn));
ofn.lStructSize=sizeof(ofn);
ofn.lpstrFile=szFile;
ofn.lpstrFile[0]=TEXT('