将txt文件读入类并输入到矢量中 [英] reading txt file into class and inputting into a vector

查看:114
本文介绍了将txt文件读入类并输入到矢量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我刚刚开始上课。我们很久没有了解它们,所以我在如何做以下事情上苦苦挣扎。


以下是该类定义的一小部分..我没有提供所有的因为我对其余部分没有问题....我想知道我应该如何在类构造函数目标中打开一个txt文件....

展开 | 选择 | Wrap | 行号

解决方案

首先,您的类描述说默认构造函数打开一个默认字典dict.txt。所以我不明白为什么你的构造函数不是这样做的。不应该只是打开dict.txt ?


您正在做的是使用 local 字符串对象中包含的名称打开文件,但是您无法在该对象中放置文件名。 / blockquote>

当我把它作为文件名时...例如。

target :: target()

//默认构造函数

//打开小型默认字典(dict.txt)并读取单词进入

//向量wordList

//默认字典仅包含长度为4-9的小写单词

//设置centreLetter和otherLetters到空格。

{

ifstream infile;

string fname;

int data;

infile.open(fname。" dict.txt");

if(infile.fail())

{

cout<< 未找到文件 << ENDL;

返回EXIT_FAILURE;

}

infile>>数据;

centreLetter ='''';

otherLetters ='''';

}

it出现错误,说聚合''std :: ifstream infile''具有不完整的类型且无法定义..

预期不合格-id在字符串常量之前

返回构造函数的值

[构建错误] [target.o]错误1


我只是坚持我做错了...


1) - 在ifstream上阅读文档,注意你应该包含哪些标题以便使用它(fstream)

2)正如你已经注意到的那样,编译器可能你指的是这行 - fname。dict.txt 。这不是一个有效的表达式。

3)返回EXIT_FAILURE; 。是的,施工人员不会回报价值。如果出现问题,您可以抛出异常。


Hi everyone. I am just starting out on my assignment on classes. We havent long learnt about them so I am struggling a little on how to do the following.

Below is a small part of the definition of the class.. I have not provided all of it as I dont have questions on the rest of it.... I was wondering how I am supposed to open a txt file in the class constructor target....

Expand|Select|Wrap|Line Numbers

解决方案

First, your class description says that the default constructor opens a default dictionary "dict.txt" so I don''t uinderstand why your constructor isn''t doing that. Shouldn''t it just open "dict.txt" ?

What you are doing is opening a file using a name contained in a local string object but you have failed to put a file name in that object.


when I make it the file name.. eg.
target :: target()
// default constructor
// Opens small default dictionary ("dict.txt") and reads words into
// vector wordList
// Default dictionary contains only lower case words of length 4-9
// Sets centreLetter and otherLetters to spaces.
{
ifstream infile;
string fname;
int data;
infile.open(fname."dict.txt");
if (infile.fail())
{
cout << "File not found " << endl;
return EXIT_FAILURE;
}
infile >> data;
centreLetter = '' '';
otherLetters = '' '';
}
it comes up with errors saying "aggregate ''std::ifstream infile'' has incomplete type and cannot be defined..
expected unqualified -id before string constant
returning a value from a constructor
[Build Error] [target.o] Error 1

I am just stuck on what I am doing wrong...


1) - read doc on ifstream, note what headers you should include in order to use it ( fstream )
2) As you already noticed, the compiler probably pointed you to this line - fname."dict.txt" . This is not a valid expression.
3) return EXIT_FAILURE; . Yes, constructors don''t return value. You can thrown an exception if something went wrong.


这篇关于将txt文件读入类并输入到矢量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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