C++ 从文本文件c++;
我有以下代码,其中一个文本文件存储在一个文件夹(folder2)中,该文件夹存储在另一个文件夹(folder1)中。我的程序显示了folder2中存储的内容的名称,folder2是文本文件,但我希望能够逐行读取文本文件的内容。我该怎么做?? 这是我的密码:C++ 从文本文件c++;,c++,ifstream,C++,Ifstream,我有以下代码,其中一个文本文件存储在一个文件夹(folder2)中,该文件夹存储在另一个文件夹(folder1)中。我的程序显示了folder2中存储的内容的名称,folder2是文本文件,但我希望能够逐行读取文本文件的内容。我该怎么做?? 这是我的密码: #include <dirent.h> // directory header #include <iostream> #include <stdlib.h> // exit() #include <
#include <dirent.h> // directory header
#include <iostream>
#include <stdlib.h> // exit()
#include <fstream>
using namespace std;
int main()
{
DIR *pdir = NULL;
pdir = opendir ("C:\\folder1\\folder2");
struct dirent *pent = NULL;
if (pdir == NULL)
{
cout << "\nERROR! pdir could not be initialised correctly";
exit (3);
} // end if
while (pent = readdir (pdir))
{
if (pent == NULL)
{
cout << "\nERROR! pent could not be initialised correctly";
exit (3);
}
cout << pent->d_name << endl;
}
closedir (pdir);
cin.get ();
return EXIT_SUCCESS;
}
#包含//目录头
#包括
#包括//退出()
#包括
使用名称空间std;
int main()
{
DIR*pdir=NULL;
pdir=opendir(“C:\\folder1\\folder2”);
struct dirent*pent=NULL;
如果(pdir==NULL)
{
如果(pent==NULL)
怎么可能成功呢?@NeilButterworth if(pent==NULL)的意思是,如果pent尚未初始化,则会显示一条错误消息,但while(pent=readdir(pdir))
意味着如果pent
为NULL
,将永远不会进入循环。我真的不太明白你的论点,但你认为我应该写什么来代替它???@NeilButterworth所有这些代码只是为了找到文件名。一旦你有了文件名,作为一个选项,你可以使用ifstream在单独的函数中打开它并读取行使用std::ifstream::getline。如果您搜索这些代码,将会有大量代码。