c文件路径字符串|java代码实现从路径字符串中获取文件名称

c文件路径字符串|java代码实现从路径字符串中获取文件名称的第1张示图

⑴ 在文件路径中,“\”与“/”的区别是什么

"\"在DOS中表示右边的字符串是以左边字符串为名称的目录下的子目录或者文件名;比如C:\bb\aa.bat表示C盘根目录下bb子目录中的aa.bat文件。“/”我没有见过用于表示文件路径的,如果是下种情况C:\bb\aa.bat /cc则是aa.bat运行需要的参数。其它最多的是在网络中的运用,看你的地址栏。问题很不清晰,先回答这些。

⑵ C语言 如何向一个路径的文件中写入一段字符串

步骤逻辑:

1:打开要写入字符串的文件并检查文件是否已打开。

2:向该文件写入指定的字符串。

3:关闭文件。

需要的头文件和函数(这里对函数的使用方法不再介绍):

1:stdio.h头文件下的fopen函数,fputs函数,fclose函数。

2:stdlib.h头文件下的exit函数。

以下是完整代码并附有详细注释:

#include<stdio.h>//所用到的关键函数fopen,fputs,fclose都在stdio.h头文件中;#include<stdlib.h>//exit函数在stdlib.h头文件中;intmain(void){FILE*file=NULL;//定义一个文件类型(FILE)的指针并初始化;constchar*FileName="D:\测试\网络知道.txt";//这里假设是要在D盘的“测试”文件夹中的“网络知道.txt”文件写入字符串。注意:字符串中写入''符号必须双写。file=fopen(FileName,"r+");//调用fopen函数,将返回值赋于指针file;if(!file)//检查文件是否打开,若打开失败,返回一条信息后,结束程序。{printf("文件打开失败,请检查文件是否存在!");exit(1);}constchar*string="你知道,所以我知道。";//需要写入的字符串内容。if(!fputs(string,file))//调用fputs函数写入文件,不管成功或失败都会返回一条信息。printf("写入成功!");elseprintf("写入失败!");fclose(file);//关闭文件。file=NULL;//放空file指针。return0;}

⑶ 用vb6实现:在一个包涵文件路径的字符串中,自动提取所在盘符 例如,"c:\demo\1.exe",自动提取"c"

按搜索“:\”的方法,可以制做如下函数。Option ExplicitPrivate Function GetDrive(Path As String) As StringGetDrive = Split(Path, ":\")(0)End FunctionPrivate Sub Form_Load()MsgBox GetDrive("c:\demo\1.exe")End Sub不过我感觉你可以直接获取字符串的第一个字符就可以了。Left(path,1)

⑷ java代码实现从路径字符串中获取文件名称

这道题主要就是利用了String类的split()方法,进行多次调用,已经帮你实现了,代码如版下:public class Test{ public static void main(String[] args){ String str = "c:/win/good/aaa.txt;d:/win/good/bbb.txt;c:/win/cccc.txt;"; //得到路权径数组 String[] fileRoot = str.split(";"); String[] fileName = null; for(int i = 0;i < fileRoot.length;i++){ if(fileRoot[i] != null){ fileName = fileRoot[i].split("/"); //得到最终需要的文件名 System.out.println (fileName[fileName.length-1]); } } }}

⑸ c语言里,如何字符串数组中存放的文件路径

路径 中反斜杠 用 双斜杠书写。用 sprintf 构成 文件全路径。程序例子如下。#include<stdio.h>int main( ){ FILE *fp;char path1[80]="E:\\Users\\Wang"; //主路径char sub_path[40]="P1\\text\\win_ver.txt"; //子路径和文件名char f_name[120]; //文件全路径sprintf(f_name,"%s\\%s",path1,sub_path); //构成文件全路径printf("I will open %s\n",f_name); fp=fopen(f_name,"r"); //打开文件if (!fp) {printf("Can not opne %s\n",f_name);exit(0);};printf("Good !\n");fclose(fp); //关闭文件return 0;}

⑹ C语言 如何向一个路径的文件中写入一段字符串

用fopen函数,假设有一文件指针p,p=fopen(“这里写文件名”,“这里写打开方式”);打开方式写a就行

⑺ C语言判断一个字符串是文件还是文件夹

一般来说在C语言中读取txt文件的信息有两种方法,一种是使用C语言标准文件I/O中的fopen()、fread()等等函数,一种是调用操作系统中的API函数,比如Windows上的ReadFile()、OpenFile()等等,现在操作系统一般都具备内存文件映射功能,对于大的txt文件,一般都使用这种方式操作。下面是一个使用C语言标准文件I/O操作文件的例子。#include<stdio.h>FILE*stream;void main(void){ long l; float fp; char s[81]; char c; stream=fopen("fscanf.out","w+"); if(stream==NULL) printf("Thefilefscanf.outwasnotopened\n"); else { fprintf(stream,"%s%ld%f%c","hello world", 65000,3.14159,'x'); /*Setpointertobeginningoffile:*/ fseek(stream,0L,SEEK_SET); /*Readdatabackfromfile:*/ fscanf(stream,"%s",s); fscanf(stream,"%ld",&l); fscanf(stream,"%f",&fp); fscanf(stream,"%c",&c); /*Outputdataread:*/ printf("%s\n",s); printf("%ld\n",l); printf("%f\n",fp); printf("%c\n",c); fclose(stream); }}

⑻ objective-c如何获得文件路径

方法:通过NSHomeDirectory获得文件路径代码如下:NSString *homeDirectory = NSHomeDirectory();NSString *fileDirectory = [homeDirectory :@"temp/app_data.plist"];1.//使用检索指定路径NSArray *path = (NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];zNSString *fileDirectory = [documentsDirectory :@"file.txt"];2.//使用Foundation中的NSTemporaryDirectory函数直接返回代表temp文件夹的全路径的字符串对象NSString *tempDirectory = NSTemporaryDirectory();NSString *file = [tempDirectory :@"file.txt"];实现例子如下:NSArray *path = (NSCachesDirectory, NSUserDomainMask, YES);NSString *docDir = [path objectAtIndex:0];NSLog(@"filepath:%@",docDir);NSString *str = @"hello.jpg";NSString *filepath = [docDir :str];//NSString *filepath = [docDir :[NSString stringWithUTF8String:"///mest.txt"]];NSLog(@"filepath:%@",filepath); BOOL success = [[NSFileManager defaultManager]createFileAtPath: filepath contents:nil attributes:nil];NSLog(@"result",success);printf("Create File:%s %s.",[filepath UTF8String], success ? "Success" : "Error");NSString* reValue= [NSString stringWithString:@"\\"success\\""];

⑼ C语言 字符串 文件路径

char s[100];FILE *fp;strcat(strcpy(s,q),"\\yourfilename.txt");fp=fopen(s,"w");……这样就在原路径字符串q(已知路径字符串指针)上下创建了一个名为yourfilename.txt的文件。

⑽ C++中如何从路径字符串中获取文件名!

C风格:

char*p=strrchr(path.c_str(),'/')

p是path里最后一个'/'的地址。然后

strings(p+1);

,内s就是"world.shp"了。

C++风格:

intpos=path.find_last_of('/');

pos就是最后一个'/'的下标容。

然后

strings(path.substr(pos+1));

s就是"world.shp"了。

未经允许不得转载:山九号 » c文件路径字符串|java代码实现从路径字符串中获取文件名称

赞 (0)