尝试从文本文件中读取一行并将其放入字符串C ++

时间:2018-01-24 17:43:07

标签: c++

我正在尝试从文本文件中读取一行并将其放入一个字符串中,以便我可以对它运行一些测试,但我无法让我的程序读取该文件。在过去的几个小时里,我一直在网上研究这个问题,而且我找不到任何实际可行的东西。它似乎无所事事或无法编译。根据我的研究,getline应该能够读取文件并将该行放入字符串中,但我无法弄清楚如何让它实际执行。我正在使用的文本文件有一个单词列表,每行一个,如:

apple
banana
pear
orange

这是我的代码:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
using namespace std;


int main()
{

string readWord = "";
string topWord = "";
int currentConst;
int maxConst;
string fline;
int line = 0;

ifstream textFile("words.txt");


//while (getline(textFile, readWord))
//{
    //cout << readWord << '/n';
//}

if (textFile.bad())
{

getline(textFile, readWord);
cout << readWord;
}
        cin >> line;

return 0;
}

感谢您的帮助!

0 个答案:

没有答案
相关问题