【问题标题】:Loading Variable from a file从文件加载变量
【发布时间】:2023-03-14 14:48:02
【问题描述】:

4
5
6
7

#include<iostream>
#include<stdio.h>
#include <fstream>
using namespace std;

int main()
{
    int data[4], a, b, c, d, e, f;
    ifstream myfile;
    myfile.open("tera.txt");
    for (int i = 0; i < 4; i++)
    {
        myfile >> data[i];
    }
    myfile.close();
    a = data[0];
    b = data[1];
    c = data[2];
    d = data[3];
    cout << a << "\t" << b << "\t" << c << "\t" << d << "\n";
    return 0;
}

它也需要 AT 并给出垃圾值。我应该如何以及在哪里使用忽略功能来忽略 AT 值。 如果给定 BT 的另一个数组包含这样的一些值,那么还有一件事: 英国电信 如何将BT下的所有值存储在一个数组中?

【问题讨论】:

  • “我已经尽力了”请edit你的问题展示你最好的一面。
  • @DrewDormann 完成!
  • @CaptainObvlious 完成!
  • 请先研究一下。已经回答了太多关于从文件中读取的问题:"StackOverflow c++ read from file"。您可能希望通过附加“into struct”或“columns”来缩小搜索范围。
  • @ThomasMatthews 你告诉我如何忽略上述代码的 AT?

标签: c++ file file-handling filehandle


【解决方案1】:

您只需跳过第一行。您还可以添加可选的错误处理,否则读取可能会导致所有行都失败。

if (!myfile)
{
    cout << "can't open\n";
    return 0;
}

string temp;
myfile >> temp;
cout << "first line: " << temp << endl;
for (int i = 0; i < 4; i++)
{
    myfile >> data[i];
    if (myfile.fail())
    {
        cout << "error\n";
        myfile.clear();
        myfile.ignore(1000000, '\n');
    }
}

【讨论】:

  • Shani,你应该可以在这里发表评论,因为这是你自己的话题。
  • @Barmark 兄弟我无法在我尝试过的评论中标记你,可能你有一些隐私
  • 我做了一些编辑,看看是否有效。在您获得更多积分之前,您无法在其他主题中发表评论。显然,您可以在此主题中发表评论,因为它是您的。
猜你喜欢
  • 2018-03-25
  • 2018-11-03
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-07
  • 2023-04-05
相关资源
最近更新 更多