C++ 打开文件并将数据输入到类对象

发布于 2024-07-08 10:00:19 字数 508 浏览 9 评论 0原文

简单的问题,希望是一种简单的方法,只是想验证我是否以正确/有效的方式进行操作。

我有一个 T 类对象,通常将其放入在 main() 函数中创建的向量中。 它可以是任何类型的数据,字符串,整数,浮点......等。我正在从文件中读取......它是从用户输入并传递给函数的。 这是我的基本读入函数:

template <class T, class U>
void get_list(vector<T>& v, const char *inputFile, U)
{
ifstream myFile;
T object;

myFile.open("inputFile")
while(!myFile.eof())
   {
   myFile >> object;
   insert(v, object, U)
   }
}

insert 只是另一个函数,它将遍历数据并将数据插入到我的数据结构中。 我只是想确保这是传递数据的最佳方式(如果它有效的话)。

Simple question, hopefully an easy way and just want to verify I'm doing it the correct / efficient way.

I have a class T object, which is typically put into a vector that is created in my main() function. It can be any kind of data, string, int, float.. etc. I'm reading from a file... which is inputted from the user and passed onto the function. Here is my basic read in function:

template <class T, class U>
void get_list(vector<T>& v, const char *inputFile, U)
{
ifstream myFile;
T object;

myFile.open("inputFile")
while(!myFile.eof())
   {
   myFile >> object;
   insert(v, object, U)
   }
}

insert is just another function that will go through and insert the data into my data structure. I just want to make sure this is the best way to pass that data on if it will even work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱倦旧时光 2024-07-15 10:00:19

您犯了在该条件下测试 eof 的老错误。
除非您尝试读取文件末尾,否则不会设置 EOF。 所以这个方法会在向量中插入一个你不想要的额外值。

template <class T, class U>
void get_list(vector<T>& v, const char *inputFile, U)
{
    ifstream myFile("inputFile");  // Why hard code this?
                                   // When you pass inputFile as a parameter? 
    T object;


    while(myFile >> object)  // Get the object here.
                             // If it fails because of eof() or other
                             // It will not get inserted.
    {
         insert(v, object, U)
    }
}

You made the old mistake of testing against eof in the condition.
EOF is not set until you try and read past the end of file. So this method will insert one extra value into the vector that you don't want.

template <class T, class U>
void get_list(vector<T>& v, const char *inputFile, U)
{
    ifstream myFile("inputFile");  // Why hard code this?
                                   // When you pass inputFile as a parameter? 
    T object;


    while(myFile >> object)  // Get the object here.
                             // If it fails because of eof() or other
                             // It will not get inserted.
    {
         insert(v, object, U)
    }
}
酒废 2024-07-15 10:00:19

看起来它会工作得很好,我想说这可能是最好的方法。 但你为什么在这里问而不是自己测试呢?

It looks like it will work fine, and I would say this is probably the best way to do it. But why are you asking here instead of just testing it yourself?

怂人 2024-07-15 10:00:19

不要在 while 条件中的流上使用 .eof()。 仅当尝试读取文件末尾之后,该条件才会评估为 true。 更好的方法是

while(myFile >> object)
    insert(v, object, U);

您对 U 的使用有缺陷。 我不知道它是用来做什么的。 一次它被用作类型,但另一次你将它作为值传递给 insert 函数。

Don't use .eof() on a stream in a while-condition. The condition will evaluate to true only after an attempt to read past the end of the file has been made. a better way is to do

while(myFile >> object)
    insert(v, object, U);

Your use of U is flawed. I have no idea what it is used for. One time it is used as a type, but then another time you pass it to the insert function as a value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文