将 istream 运算符>>转换为 istream getline

Converting istream operator >> to istream getline

本文关键字:gt istream getline 转换 运算符      更新时间:2023-10-16

我正在尝试使用istream& getline而不是istream& operator从文件中读取行。

结果cpp

#include "Result.h"
Result::Result()
{
    Coursename[0] = '';
}
Result::Result( const char * nam, unsigned scor )
{
    strncpy( Coursename, nam, CourseNameSiz );
    score = scor;
}
//istream& getline( istream & input, Result & Re, ',' )
istream & operator >>( istream & input, Result & Re )
{
  //getline(Re.Coursename, input, ',');
  input >> Re.Coursename >> Re.score;
  return input;
}
ostream & operator <<( ostream & os, const Result & Re )
{
  os << "  Unit Name:  " << Re.Coursename << 'n'
     << "  Result: " << Re.score << 'n';
  return os;
}
int Result::GetScore() const
{
  return score;
}
inline void Result::SetScore( unsigned scor )
{
  score = scor;
}

结果头

#ifndef RESULT_H
#define RESULT_H
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
const unsigned CourseNameSiz = 10;
class Result
{
    public:
        Result();
        Result( const char * nam, unsigned scor );
        int GetScore() const;
        // Get the number of s.
        void SetScore( unsigned scor );
        // Set the number of score.
        //string Result:GetCourseName() const;
        friend ostream & operator <<( ostream & os, const Result & Re );
        //friend istream & getline( istream & input, Result & Re, ',' );
        friend istream & operator >>( istream & input, Result & Re );
    private:
        char Coursename[CourseNameSiz];
        //string Coursename[CourseNameSiz]; // Unit name, C style string
        int  score; // number of scores
        string str;
}; 
#endif // RESULT_H

这一切都是我想要的,但我需要改变它,所以我想使用istream&带有分隔符的getline。代码中的大多数注释都是我尝试更改为getline方法。我制作了一个字符串数组,作为一个测试,我试图将读取文件中的第一项放入其中,但显然不起作用。

我只是想在这里找一点指导。提前感谢

编辑::输入文件:rinput.txt

31525 1 4
JPN_101 3 Japanese, 90,
ICT 4 example1, 91,
TIC 4 example2, 92,
CIT 4 example3, 93

因此,在上面写着示例1和91的地方,这是我试图使用带有分隔符的getline的两个项目,因为我想在我的读取文件中使用空格

其他信息在不同的类中读取,当我让这个类工作时,我会将其更改为getline方法。

输出文件:routput.txt

Student ID: 31525
Semester:   1
  Unit:  JPN_101
  Credits: 3
  Unit Name:  Japanese
  Result: 90
  Unit:  ICT
  Credits: 4
  Unit Name:  example1
  Result: 91
  Unit:  TIC
  Credits: 4
  Unit Name:  example2
  Result: 92
  Unit:  CIT
  Credits: 4
  Unit Name:  example3
  Result: 93
Number of courses = 4
Total credits     = 15

这是im使用带有数组字符的运算符方法时的输出。

includeResult.h|28|error: expected identifier before ','|
includeResult.h|28|error: expected ',' or '...' before ','|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

其指向作为friend istream & getline( istream & input, Result & Re, ',' ); 的头文件行28

我相信我只是不明白为什么把它改成字符串数组(string Coursename[CourseNameSiz];),然后把operator方法改成getline方法,并不能像运算符方法那样返回输入。

我希望我解释得对,我请人翻译一下,帮我把它说得更好。

感谢

此行

friend istream & getline( istream & input, Result & Re, ',' );

不会编译,因为您有一个常量作为第三个参数,而不是参数声明。

在您的情况下,因为您在方法内部的getline调用中硬编码',',所以根本不需要传入逗号。

friend istream & getline( istream & input, Result & Re);

类似地,当您定义函数时,您不需要函数定义中的','

istream& getline( istream & input, Result & Re)
{
  getline(Re.Coursename, input, ',');
  return input;
}

请注意,在对getline的内部调用中仍然需要逗号。

这应该可以修复您列出的编译器错误,如果代码不起作用,您仍然需要调试代码。