21xrx.com
2025-03-28 14:58:14 Friday
文章检索 我的文章 写文章
C++获取文件路径和文件名
2023-07-09 12:57:33 深夜i     --     --
C++ 获取 文件路径 文件名

C++是一种功能强大的编程语言,它可以用于实现各种应用程序。当我们在编写C++程序时,常常需要获取文件的路径和文件名,以便对这些文件进行操作。在本文中,我们将探讨如何在C++中获取文件路径和文件名。

获取文件路径

在C++中获取文件路径的方法主要有两种:利用C++标准库中的文件流和利用Windows API函数。

使用文件流获取文件路径

C++标准库中的文件流提供了一种简单的方法来获取文件路径。可以使用fstream类的open()函数打开一个文件,并使用gcount()函数获取文件的长度。然后,我们就可以使用seekg()函数将读取位置设置为文件的末尾,然后使用tellg()函数获取文件的位置,这就是文件的长度。最后,我们可以使用路径分解函数获取文件路径:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdlib.h>
using namespace std;
int main(int argc, char** argv)
{
  if (argc < 2)
  {
    cout << "Usage: getpath filename\n";
    return -1;
  }
  ifstream file(argv[1], ios::binary);
  if (!file.good())
  {
    cout << "Failed to open file " << argv[1] << "\n";
    return -1;
  }
  file.seekg(0, ios::end);
  int length = file.tellg();
  file.seekg(0, ios::beg);
  char* buffer = new char[length];
  file.read(buffer, length);
  string path = argv[1];
  string::size_type pos = path.find_last_of("\\/");
  if (pos != string::npos)
    path.erase(pos);
  cout << "File path: " << path << "\n";
  delete[] buffer;
  return 0;
}

使用Windows API函数获取文件路径

在Windows系统上,我们可以使用Windows API函数获取文件路径。可以使用GetFullPathName()函数获取文件的全路径名,并利用PathRemoveFileSpec()函数将文件名从路径中删除,这就是文件的路径。

#include <iostream>
#include <windows.h>
#include <Shlwapi.h>
using namespace std;
int main(int argc, char** argv)
{
  if (argc < 2)
  {
    cout << "Usage: getpath filename\n";
    return -1;
  }
  TCHAR buffer[MAX_PATH];
  GetFullPathName(argv[1], MAX_PATH, buffer, nullptr);
  PathRemoveFileSpec(buffer);
  wstring path(buffer);
  wcout << "File path: " << path << "\n";
  return 0;
}

获取文件名

可以使用路径分解函数获取文件名。

我们可以使用C++标准库中的路径分解函数获取文件名。可以使用文件流、字符串流或字符串函数获取文件名。

使用文件流获取文件名

我们可以先使用路径分解函数获取文件路径,然后使用文件流打开文件,并使用函数rfind()获取文件名:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdlib.h>
using namespace std;
int main(int argc, char** argv)
{
  if (argc < 2)
  {
    cout << "Usage: getname filename\n";
    return -1;
  }
  string path = argv[1];
  string::size_type pos = path.find_last_of("\\/");
  if (pos != string::npos)
    path.erase(0, pos + 1);
  ifstream file(argv[1], ios::binary);
  if (!file.good())
  {
    cout << "Failed to open file " << argv[1] << "\n";
    return -1;
  }
  string name;
  getline(file, name);
  file.close();
  cout << "File name: " << name << "\n";
  return 0;
}

使用字符串流获取文件名

我们可以将文件内容读入字符串流,并使用getline()函数从字符串流中获取第一行,该行就是文件名:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdlib.h>
using namespace std;
int main(int argc, char** argv)
{
  if (argc < 2)
  {
    cout << "Usage: getname filename\n";
    return -1;
  }
  string path = argv[1];
  string::size_type pos = path.find_last_of("\\/");
  if (pos != string::npos)
    path.erase(0, pos + 1);
  ifstream file(argv[1], ios::binary);
  if (!file.good())
  {
    cout << "Failed to open file " << argv[1] << "\n";
    return -1;
  }
  stringstream stream;
  stream << file.rdbuf();
  string name;
  getline(stream, name);
  cout << "File name: " << name << "\n";
  return 0;
}

使用字符串函数获取文件名

我们可以使用函数find_last_of()获取文件名:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdlib.h>
using namespace std;
int main(int argc, char** argv)
{
  if (argc < 2)
  {
    cout << "Usage: getname filename\n";
    return -1;
  }
  string path = argv[1];
  string::size_type pos = path.find_last_of("\\/");
  if (pos != string::npos)
    path.erase(0, pos + 1);
  ifstream file(argv[1], ios::binary);
  if (!file.good())
  {
    cout << "Failed to open file " << argv[1] << "\n";
    return -1;
  }
  string name;
  getline(file, name);
  pos = name.find_last_of("\\/");
  if (pos != string::npos)
    name.erase(0, pos + 1);
  cout << "File name: " << name << "\n";
  return 0;
}

结论

本文介绍了如何在C++中获取文件路径和文件名。我们可以使用标准库中的文件流来获取文件路径和文件名,也可以使用Windows API函数来获取文件路径。我们还介绍了如何使用字符串流和字符串函数来获取文件名。无论我们使用哪种方法,只要掌握了这些技术,我们就可以更方便地操作文件,编写出更加高效、实用的C++程序。

  
  

评论区