首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ifstream getline问题

ifstream getline问题
EN

Stack Overflow用户
提问于 2012-11-30 20:17:47
回答 3查看 12.1K关注 0票数 1

我的代码打开一个文本文件,计算行数,分配一个数组来存储所有行,然后调用一个函数用每一行填充这个数组。此函数file.getline调用返回空字符串:

代码如下:

代码语言:javascript
复制
typedef char* line;

..。

代码语言:javascript
复制
char* filename=new char[256];
cout << "Type a file name: " << endl;
cin.ignore();
cin.getline(filename,255);

ifstream iFile(filename);

int nLines=CountLines(iFile);

line* LineArray = new line[nLines];
ReadLines(LineArray,iFile);

CountLines函数:

代码语言:javascript
复制
int CountLines(ifstream &file)
{
line templine=new char[64];
int nLines=0;

while (!file.eof())
{
    file.getline(templine,64);

    if (*templine != '\n')
        nLines++;

}
delete [] templine;

return nLines;
}

这可以正常工作。然而,ReadLines并不:

代码语言:javascript
复制
void ReadLines(line* LineArray, ifstream &file)
{
    line templine=new char[64];

file.seekg(0,ios::beg);

int i = 0;
while (!file.eof())
{

    if (*templine != '\n')
    {
        LineArray[i]=templine;
        i++;
    }

}
delete [] templine;
}

我感觉这与getline的'\n‘问题有关,但当我将get指针设置为0,并且文件以普通文本而不是一行开始时,我不明白为什么它用空字符串填充模板行。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-30 21:23:49

你的代码中有太多的错误。

在释放内存后,您需要清除

  • 参数istream::getline()的参数错误
  • 您需要清除eof标志...

指针不是玩具,你最好使用Tino Didriksen的解决方案。

如果你真的喜欢字符和指针,它应该是这样的:

代码语言:javascript
复制
#include <iostream>
#include <fstream>
#include <cassert>

using namespace std;

int CountLines(ifstream &fin) {
  char templine[1024];      // no need for dynamic allocation.
  int count = 0;
  while (fin.getline(templine, 1024))
    count++;
  return count;
}

void ReadLines(char** lines, int count, ifstream &fin) {
  fin.seekg(0, ios::beg);
  for (int i = 0; i < count; i++) {
    lines[i] = new char[1024];      // you need dynamic allocation here.
    fin.getline(lines[i], 1024);
    assert(fin.gcount() < 1024);    // assure the line is shorter than 1023 chars
  }
}

int main() {

  char filename[256];         // no need for dynamic allocation.
  cin.getline(filename, 256); // second parameter should be the same size of your buffer.

  ifstream fin(filename);

  int count = CountLines(fin);
  char** lines = new char*[count];

  // After CountLines() called, fin.eof is set, you need to clear it.
  // Otherwise fin.getline() won't do a thing.
  fin.clear();
  ReadLines(lines, count, fin);

  // When every thing is done, you need to free all the memory.
  for (int i = 0; i < count; i++)
    delete[] lines[i];
  delete[] lines;

}
票数 1
EN

Stack Overflow用户

发布于 2012-11-30 20:44:10

您不需要先计算行数,然后再读行。你可以做到

代码语言:javascript
复制
#include <istream>
#include <vector>
#include <string>

std::vector<std::string> ReadLines(std::istream& is) {
    std::vector<std::string> lines;
    std::string line;

    while (std::getline(is, line)) {
        lines.push_back(line);
    }

    return lines;
}

它将返回一个包含所有行的std::vector,无需任何麻烦或手动内存管理。

票数 6
EN

Stack Overflow用户

发布于 2012-11-30 20:36:33

您的错误存在于以下代码中:

代码语言:javascript
复制
if (*templine != '\n')

因为您正在检查行中第一个符号。

您应该像这样更改代码:

代码语言:javascript
复制
int CountLines(ifstream &file)
{
    string line;
    int nLines=0;
    while(getline(file,line))
        nLines++;

    return nLines;
}


void ReadLines(string LineArray, ifstream &file)
{
    file.seekg(0,ios::beg);

    string line;
    while(getline(file,line))
    {
        LineArray += line;
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13645095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档