C++ 从文件名字符串中找到路径和文件名, 并打开以其内容为文件名的文件

本文介绍了一个使用C++实现的文件路径解析方法,包括如何从完整的文件路径中分离出目录路径和文件名,并展示了如何读取指定路径下的文件内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

// StringOperations.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

char filenamestring[]="C:\\DiskKang\\tmp\\100k-1no.rot\\filelist.txt";
string path;
string filename;
int pathPosition=0;
string str_pathfilename(filenamestring);
pathPosition=str_pathfilename.find_last_of('\\');
pathPosition++;   //add 1,

path.assign(str_pathfilename.c_str(),pathPosition);
filename.assign(&str_pathfilename[pathPosition],str_pathfilename.length()-pathPosition);
for (int i=0;i<str_pathfilename.length();i++)
{
   cout<<i<<" : "<<str_pathfilename[i]<<endl;
}
cout<<"path:"<<path<<endl;
cout<<"filename : "<<filename<<endl;
cout<<"pathPosition: "<<pathPosition<<endl;

if (pathPosition==1)
{//no path
   return false;
}

cout<<"---------------------"<<endl; cout<<"---------------------"<<endl;

//string str_pathfilename;
ifstream f_listfile;
f_listfile.open(str_pathfilename.c_str());
char filenamechar[1024],filenameCharBuffer[1025];
// f_listfile.getline(filenamechar,1024,'\n');
do
{
   f_listfile.getline(filenameCharBuffer,1024,'\n');
   filename.assign(filenameCharBuffer);
   str_pathfilename=path+filename;
   //   LoadFile(str_pathfilename.c_str(), true);
   cout<<str_pathfilename<<endl;

} while (!f_listfile.eof());
f_listfile.close();

cout<<endl<<endl;
ifstream f_datafile;
string str_datafilepathname=path+filename;
cout<<"data file : "<<str_datafilepathname<<endl;

f_datafile.open(str_datafilepathname.c_str());
string data;
int a=0;
do
{
   a++;
   f_datafile.getline(filenamechar,1024,'\n');
   data.assign(filenamechar);
   cout<<data<<endl;
   //data[0]
} while (a<3);


f_datafile.close();

return 0;
}

output:

0 : C
1 : :
2 : \
3 : D
4 : i
5 : s
6 : k
7 : K
8 : a
9 : n
10 : g
11 : \
12 : t
13 : m
14 : p
15 : \
16 : 1
17 : 0
18 : 0
19 : k
20 : -
21 : 1
22 : n
23 : o
24 : .
25 : r
26 : o
27 : t
28 : \
29 : f
30 : i
31 : l
32 : e
33 : l
34 : i
35 : s
36 : t
37 : .
38 : t
39 : x
40 : t
path:C:\DiskKang\tmp\100k-1no.rot\
filename : filelist.txt
pathPosition: 29
---------------------
---------------------
C:\DiskKang\tmp\100k-1no.rot\0_at_real_-1_sec_Pos_Vel.dat
C:\DiskKang\tmp\100k-1no.rot\0_at_real_0_sec_Pos_Vel.dat
C:\DiskKang\tmp\100k-1no.rot\10000_at_real_0.00153729_sec_Pos_Vel.dat
C:\DiskKang\tmp\100k-1no.rot\10000_at_real_0.00890833_sec_Pos_Vel.dat
C:\DiskKang\tmp\100k-1no.rot\1000_at_real_0.000229788_sec_Pos_Vel.dat
C:\DiskKang\tmp\100k-1no.rot\1000_at_real_0.000836763_sec_Pos_Vel.dat
C:\DiskKang\tmp\100k-1no.rot\9500_at_real_0.0084497_sec_Pos_Vel.dat


data file : C:\DiskKang\tmp\100k-1no.rot\9500_at_real_0.0084497_sec_Pos_Vel.dat
//Data Format: Central Position (3xD);   Rotated angle (Euler Angles) (3xD);     Translational Veloc
ity (3xD);        Rotational Velocity(3xD)
* 0.0084497
   -1.4873541989359945e-02   -7.4005658407876804e-02    0.0000000000000000e+00   0 0 -16.39081056539
764        -4.6787394058492255e+00    7.3567724731043427e-01    0.0000000000000000e+00      0.000000
0000000000e+00    0.0000000000000000e+00   -1.6499987238445734e+04
Press any key to continue . . .

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值