Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用std::getline()将文本文件读入C++;?_C++_Arrays_File Io_Fstream_Dynamic Arrays - Fatal编程技术网

C++ 如何使用std::getline()将文本文件读入C++;?

C++ 如何使用std::getline()将文本文件读入C++;?,c++,arrays,file-io,fstream,dynamic-arrays,C++,Arrays,File Io,Fstream,Dynamic Arrays,我试图在项目中使用std::getline()将文本文件读入字符串数组 这是我的密码: ifstream ifs ( path ); string * in_file; int count = 0; while ( !ifs.eof() ) { ++count; if ( count == 1 ) { in_file = new string[1]; } else { // Dynamically alloc

我试图在项目中使用
std::getline()
将文本文件读入字符串数组

这是我的密码:

ifstream ifs ( path );
string * in_file;
int count = 0;
while ( !ifs.eof() )
{
    ++count;
    if ( count == 1 )
    {
        in_file = new string[1];
    }
    else
    {
            // Dynamically allocate another space in the stack
    string *old_in_file = in_file;
    in_file = new string[count];
            // Copy over values
    for ( int i = 0 ; i < ( count - 1 ) ; i++ )
    {
        in_file[i] = old_in_file[i];
    }
    delete[] old_in_file;
    }
            // After doing some debugging I know this is the problem what am I 
            // doing wrong with it?
    getline(ifs,in_file[count - 1]);
}
数组将按如下方式填充:

in_file [0] = Hello
in_file [1] = Bye
in_file [2] = See you later
为什么这么麻烦

只需使用
std::string的
std:vector

std::string str;

std::vector <std::string> vec;

while ( std::getline(ifs,str) )
{
  vec.push_back(str) ;
}

切勿使用以下循环包装从流中读取的内容:

while ( !ifs.eof() )
在一些网站上,你会发现一个例子告诉你要做:

while ( ifs.good() )
这比第一个循环好一点,但仍然很容易出错,不建议这样做。看看:

最常见的文件读取方法是在逐行读取时使用
std::getline

std::string line;
while ( std::getline(ifs, line) ) {
    if (line.empty())                  // be careful: an empty line might be read
        continue;                      
    ...
}
或者在按单词阅读或提取具体类型(如数字)时,只需使用
>
运算符:


对于动态分配的
std::string
对象的C样式数组:尽可能避免动态分配。相信我,你不想自己处理内存管理。更喜欢使用具有自动存储持续时间的对象。利用标准库提供的功能。
正如已经指出的:使用STL容器,例如
std::vector
,而不是C样式的数组:

std::ifstream ifs(path);
std::vector<std::string> lines;

std::string line;
while ( std::getline(ifs, line) )
{
    // skip empty lines:
    if (line.empty())
        continue;

    lines.push_back(line);
}
std::ifstream-ifs(路径);
std::矢量线;
std::字符串行;
while(std::getline(ifs,line))
{
//跳过空行:
if(line.empty())
继续;
线。推回(线);
}

如果您的作业允许您使用
std::vector
,您可能应该这样做,而不是每次迭代都使用
new
delete
。在StackOverflow中搜索“parse getline read file”。这已经被问了很多次了。我知道这是可行的,但是我不知道如何使用
std::getline
的结果作为
while
循环的条件。
std::getline
返回的流是如何导致循环继续或停止的?
while(std::getline(ifs,line))
在多线程环境中需要使用锁同步对流的访问时将不起作用
std::string line;
while ( std::getline(ifs, line) ) {
    if (line.empty())                  // be careful: an empty line might be read
        continue;                      
    ...
}
std::string word;
while ( ifs >> word ) {               
    ...
}
std::ifstream ifs(path);
std::vector<std::string> lines;

std::string line;
while ( std::getline(ifs, line) )
{
    // skip empty lines:
    if (line.empty())
        continue;

    lines.push_back(line);
}