当前位置:首页 » 编程语言 » c语言filepath含义
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言filepath含义

发布时间: 2023-03-06 03:07:22

c语言保存文件到指定的路径和文件名

fp=fopen(filename,"wb");
里的filename就表示了文件的路径及文件名,所以要把输入的文件名和文件路径拼接起来,计算出这个filename
最好过滤一下别让路径和文件名中有非法字符,比如:\/+<>什么的。
scanf也限制一下长度。
scanf("%19s",filename);
scanf("%19s",path);
参考如下:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char ch, filename[20], path[20],buffer[40];
printf("Enter the file name: ");
scanf("%s",filename);
printf("Enter the path: ");
scanf("%s",path);
sprintf(buffer, "%s\\%s", path,filename);
printf("\nto end input ,press Ctrl+Z in newline,then ENTER:\n");
if((fp=fopen(buffer,"wt+"))==NULL)
{
printf("no such path, \nstrike any key exit!");
getch();
exit(1);
}
while ((ch=getchar())!=-1) ch=fputc(ch,fp);
fclose(fp);
printf("==================================\n\n");
printf("file path \"%s\" \nfile name \"%s\":\nand its content:\n",path,filename);
fp=fopen(buffer,"rt");
while ((ch=fgetc(fp))!=-1) putchar(ch);
fclose(fp);
return 0;

⑵ 在C语言读文件时,如何说明是当前目录

这个如果是在c语言中是根据你读文件那个函数里定义的,如果那里你用绝对地址那你放那都可以,如果用相对地址就放到你c编译器的根目录下面,或者子目录,但是子目录需要在里面指出

⑶ 求c语言读取写入文本文件的函数实现

#include<stdio.h>
#include<string.h>//strlen()
#include<malloc.h>//malloc()
intFileCreate(char*filePath,char*fileContext)
{
FILE*fp;
fp=fopen(filePath,"w+");
if(fp)
{
fwrite(fileContext,strlen(fileContext),1,fp);
fclose(fp);
return0;
}
return1;
}
char*FileRead(char*filePath)
{
FILE*fp;
longintfsize;
char*fileContext;
fp=fopen(filePath,"r");
if(fp)
{
fseek(fp,0,SEEK_END);
fsize=ftell(fp);
rewind(fp);
fileContext=(char*)malloc(fsize+1);//注意释放内存
fread(fileContext,fsize,1,fp);
*(fileContext+fsize)='';
fclose(fp);
returnfileContext;
}
returnNULL;
}

intmain()//测试
{
char*fileContext;
char*filePath="d:\111.txt";
FileCreate(filePath,"abc123");
fileContext=FileRead(filePath);
printf("%s",fileContext);
free(fileContext);//释放
return0;
}

⑷ c语言自定义文件名

1.用C语言自定义文件名,涉及到的相关知识如下:

_finddata_t结构体:
struct _finddata_t {
unsigned attrib ;
time_t time_create ;
time_t time_access ;
time_t time_write ;
_fsize_t size ;
char name [260] ;
}

rename函数:
功能描述:
改变文件的名称或者位置,如果目标已存在,将被自动覆盖。
用法:
#include <stdio.h>
int rename(const char *oldpath, const char *newpath);
参数:
oldpath:旧文件名。
newpath:新文件名或者新位置。

返回说明:
成功执行时,返回0。失败返回-1,errno被设为以下的某个值
EACCES:权能不足
EBUSY:参数oldpath或者newpath代表的是目录,而且一些进程正在使用它们
EFAULT: 内存空间不可访问
EINVAL:参数无效
EISDIR:newpath是一个现存的目录,而oldpath不是目录
ELOOP :路径解析的过程中存在太多的符号连接
EMLINK:目录超出允许的最大连接数
ENAMETOOLONG:路径名超出可允许的长度
ENOENT:路径名部分内容表示的目录不存在
ENOMEM: 核心内存不足
ENOSPC: 磁盘配额限制或空间不足
ENOTDIR:路径名的部分内容不是目录
EPERM : 包含路径名的文件系统不支持建立目录
EROFS:文件系统只读
ENOTEMPTY:newpath是一个非空的目录,除了. 和 ..以外,还包含其它入口。
EEXIST:同上
EXDEV:oldpath和newpath不处于同一文件系统

2.用C语言自定义文件名的代码例程如下:

#include<stdio.h>
#include<io.h>
intmain(intargv,char*argc)
{
longhandle;
inti=0,j=0;
struct_finddata_tfileinfo;
charfilePT[256]={''};
charfileType[20]={'a',''};
charfilePath[256]={''};
charnewName[256]={''};
charoldName[256]={''};

printf("InputtherenamefilePath: ");
scanf("%s",filePath);
fflush(stdin);
printf("InputtherenamefileType: ");
scanf("%s",fileType);
fflush(stdin);
sprintf(filePT,"%s\*%s",filePath,fileType);

handle=_findfirst(filePT,&fileinfo);
if(-1==handle)
{
printf("_findfirst()error ");
getchar();
return(-1);
}
i=1000;
do{
sprintf(oldName,"%s\%s",filePath,fileinfo.name);//全部路径
sprintf(newName,"%s\%d%s",filePath,i++,fileType);
j=rename(oldName,newName);
if(j!=0)
{
printf("rename()error ");
break;
}
}while(!_findnext(handle,&fileinfo));
_findclose(handle);
printf("ProgramEnd ");
getchar();
return(0);
}