读取文件getline的内容不能正常工作c ++ [英] Read the contents of the file getline not working c++

查看:101
本文介绍了读取文件getline的内容不能正常工作c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码应该这样做:
$ b $b读取文件的名称并尝试打开它,如果不存在具有给定名称的文件,则提供错误消息,如a)。
$ b $b读取文件的第一行以确定N的值。
$ b $b随机生成[1 .. N]范围内的整数K.
$ b $b读取文件的内容,直到它到达第K行(不计算第一行)。
$ b $b在屏幕上显示该行的内容。



例如:



文本文件的内容(例如:beatles.txt):

17

在我出生的城镇

住在一个航行到海上的人

他告诉我们他的生活

在潜艇的土地上

所以我们航行到太阳

直到我们找到绿海

我们生活在海浪之下

在我们的黄色潜水艇中

我们都生活在一艘黄色潜水艇中

黄色潜水艇,黄色潜水艇arine

我们的朋友都在船上

他们中的更多人住在隔壁

乐队开始演奏

我们过着轻松的生活

我们每个人都拥有我们所需要的一切

蓝色的天空和绿色的海洋

在我们的黄色潜水艇

执行结果:

FILENAME? beatles.txt

SENTENCE:我们都生活在一艘黄色潜水艇中//随机生成的K等于9



My code is supposed to do this:
 Read the name of the file and try to open it, providing an error message if no file with the given name exists, as in a).
 Read the first line of the file to determine the value of N.
 Generate randomly an integer K in the range [1 .. N].
 Read the contents of the file until it reaches the K-th row (not counting the first row).
 Display the contents of that line on the screen.

For example:

Contents of a text file (ex: beatles.txt):
17
In the town where I was born
Lived a man who sailed to sea
And he told us of his life
In the land of submarines
So we sailed up to the sun
Till we found the sea of green
And we lived beneath the waves
In our yellow submarine
We all live in a yellow submarine
Yellow submarine, yellow submarine
And our friends are all on board
Many more of them live next door
And the band begins to play
As we live a life of ease
Everyone of us has all we need
Sky of blue and sea of green
In our yellow submarine
Execution result:
FILENAME? beatles.txt
SENTENCE: We all live in a yellow submarine // when K, randomly generated, was equal to 9

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<cctype>
#include<vector>
#include <cmath>
#include <math.h>

using namespace std;


int main () {

  srand ((unsigned int) time(0));

  string filename;

  cout << "FILENAME? ";
  cin >> filename;

  ifstream fin(filename.c_str());

  if (fin.is_open())
  {cout << "SENTENCE: ";
  string line;
  char sline[256];
  std::string newline;

  vector<string> list;

	while(getline(fin, line))
	{

	  strcpy(sline, line.c_str());
	  int N;
      N = atoi(sline);
      int k;
      k = rand() % N + 1;



      for (int i=0; i<(k+1);i++)
          {
              getline(fin,newline);
          }}

cout << newline;

  fin.close();}

  else cout << "ERROR: File not found!";

  return 0;
}





我每次运行程序都会返回:

FILENAME? beatles.txt

句号:他们中的更多人住在隔壁

乐队开始演奏

因为我们过着轻松的生活

我们每个人都有我们需要的全部

蓝色和绿色海洋的天空

在我们的黄色潜水艇中



我认为这是第二个getline的问题,但我不知道如何解决它。

你能帮忙吗?



Everytime I run the program it is returning:
FILENAME? beatles.txt
SENTENCE: Many more of them live next door
And the band begins to play
As we live a life of ease
Everyone of us has all we need
Sky of blue and sea of green
In our yellow submarine

I think it is a problem with the second getline but i don't know how to solve it.
Can you help?

推荐答案

请记住,getline会添加到现有字符串中。如果你想丢弃任何东西,你需要在每次调用getline之前调用newline.clear()。
Remember that getline adds to the existing string. If you want to discard anything, you need to call newline.clear() before each call to getline.


这篇关于读取文件getline的内容不能正常工作c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆