【问题标题】:Can't read the data of all lines无法读取所有行的数据
【发布时间】:2018-10-18 10:43:54
【问题描述】:

我的代码无法读取所有行的数据。

void read(string name, student *sv, int n) {
ifstream file (name, ios::in);
string name, sex;
int dy, mth, year, i = 0;
float a, b;
while (file >> name >> dy >> mth >> year >> sex >> a >> b) {
    cout << name << dy << mth << year << sex <<  a <<  b << endl;
    sv[i].name = name;
    sv[i].date.day = dy;
    sv[i].date.month = mth;
    sv[i].date.year = name;
    sv[i].sex = sex;
    sv[i].math = a;
    sv[i].physics = b;
    ++i;
}
file.close();

我的数据:

Johns 3 6 1999 Male 5 7
Jeam  3 7 1998 Male 8 7
Jes   7 9 1999 Male 5 9

当我调试此代码时,它无法读取最后一行( Jes 7 9 1999 Male 5 9 )。所以 struct sv 没有最后一个值。

【问题讨论】:

  • 您的输入文件的最后一行是否有换行符(\n 或 \r\n)?
  • 这本质上应该可以正常工作。我不知道变量“nam”从何而来,也不知道 d.close() 在做什么。我也强烈反对隐藏变量名,但是如果你的 *sv 指向一个适当大小的数组,那么循环本质上应该可以工作。
  • 最后一行没有换行符,只有“endl”其他行。谢谢 Oncaphiilis。但是,我的代码被编辑了。如何使用 while ( filename >> variable ){ } 语法读取最后一行。

标签: c++ file-io


【解决方案1】:

主要问题是这一行:

while (file >> name >> dy >> mth >> year >> sex >> a >> b) {

当您到达文件的最后一行时,您会读取所有这些变量,但您也会到达文件的末尾,因此整个表达式将转换为 false,您将不会在最后一行执行代码

试试这样的:

std::string line;
std::getline(file, line);
while (file && !line.empty())
{
    std::cout << line << std::endl;

    //parse line and do stuff

    std::getline(file, line);
}

【讨论】:

  • 我不知道那样......那样,如何将从文件中读取的值分配给新变量......例如:读取(Johns 3 6 1999 Male 5 7)行然后 name = John, day = 3, ....
  • 你可以使用字符串流。 stringstream 的用法与文件流的用法相同。阅读 C++ 文档:ru.cppreference.com/w/cpp/io/basic_stringstream
  • 再次阅读文档。这是另一个包含示例的链接:ru.cppreference.com/w/cpp/io/basic_istream/operator_gtgt
  • void docfile(string namefile) { cout > 姓名 >> 日 >> 月 >> 年 >> 性别 >> 一 >> 二; cout
  • @J.Bie,给我你的程序或错误信息的输出。这段代码似乎没问题(using namespace std; 除外)。
【解决方案2】:

试试这个:

// main.cpp
#include <fstream>
#include <ios>
#include <iostream>
#include <string>

struct student {
  std::string name;
  std::string sex;
};

void read(std::string fname, student *sv) {
  std::ifstream file(fname.c_str(), std::ios_base::in);
  std::string name, sex;
  int i = 0;
  while (file >> name >> sex) {
    std::cout << name << " " << sex << std::endl;
    sv[i].name = name;
    sv[i].sex = sex;
    ++i;
  }
  file.close();
  std::cout << i << std::endl;
}

int main(int argc, char **argv) {
  student sv[10];
  std::string fname(argv[1]);
  read(fname, sv);
}

构建:

g++ -o test main.cpp

测试输入文件:

ABC Male
DEF Female
GHI Unknown
KLM Whoknows

运行:

./test test.txt

输出:

ABC Male
DEF Female
GHI Unknown
KLM Whoknows
4

【讨论】:

  • @J.Bie 你不明白哪一部分? (bạn ko hiểu chỗ nào?)
  • 构建:g++ -o test main.cpp
  • Em 测试代码 như bro thì nó không nhận ạ =(( tại sao phải dùng trực tiếp std:: mà không khai báo using namespace std; ngay từ đầu ạ?
  • @J.Bie Bạn dùng gì để build? Mình đang nói dùng g++ tren Linux。 Còn bạn dùng using namespace std; thì cũng được mà。 Mìnhthththíchdùngcùngvớistdvìvớidựánlớnvànnnhiều平台(移动,Linux,Windows)Cóthểhọsẽcónngtêntrùng,dùngstdđểchỉrõlàmìnhdəngthưviệncchncủac++
  • à vâng =) em dùng VS của Win ạ =) cám ơn bro nhiều ạ
猜你喜欢
  • 2020-08-15
  • 2016-07-10
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 2018-02-01
  • 2012-11-18
相关资源
最近更新 更多