【问题标题】:How to extract info into a struct and output to a file c++如何将信息提取到结构中并输出到文件c ++
【发布时间】:2020-03-25 18:58:03
【问题描述】:

我只是在学习结构,有点卡住了。我试图从包含课程信息(课程、时间、日期等)的文件中提取信息但是到目前为止,我的代码唯一输出到新创建的文件中的只是我写的 cout 文本。关于如何解决这个问题的任何想法? 到目前为止,这是我的代码:

    #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

//Holds all the class information
struct Course{

    string courseName;
    string courseNum;
    string courseDay;
    string courseTime;
    string courseLoc;

};

//Extracts data from the file with the course information
Course getInfo(ifstream &inFile);

//Creates a file with the data from 'getInfo'
void writeInfo(ofstream &outFile, Course course);

int main(){

    ifstream inFile; //link to input file
    ofstream outFile; //link to output file
    Course course; //holds all course info

    inFile.open("Courses.txt"); //opens textfile
    outFile.open("Courses.dat"); //creates new file

    course = getInfo(inFile); //priming read

   while (inFile) {

        writeInfo(outFile, course); //write info to output file

        course = getInfo(inFile); //get info from input file

    }

    inFile.close();
    outFile.close();

}

Course getInfo(ifstream &inFile){

    Course course;

    inFile >> course.courseName >> course.courseNum >> course.courseDay;
    inFile >> course.courseTime  >> course.courseLoc;

    return course;

}

void writeInfo(ofstream &outFile, Course){
    Course course;

    outFile << "Class: " << course.courseName << " Class ID: " << course.courseNum << " Meeting Days: " << course.courseDay << " Class Time: " << course.courseTime << " Class Location: " << course.courseLoc << endl;


}

【问题讨论】:

  • 这不是您将变量传递给函数的方式。也许在尝试继续其他事情之前回顾那一章。您正在 writeInfo 函数中创建一个新的空 Course 并将其打印到您的文件中。所以很明显它会是空的。
  • 就像将void writeInfo(ofstream &amp;outFile, Course){ Course course; outFile &lt;&lt; ... } 更改为void writeInfo(ofstream &amp;outFile, Course course){ outFile &lt;&lt; ...} 一样简单,我认为这是错字。

标签: c++


【解决方案1】:

让我们仔细看看代码要求什么

void writeInfo(ofstream &outFile, // take an ofstream as a reference parameter. 
                                  // Call it OutFile
              Course) // take a Course as a parameter. Don't give it an identifier
                      // this makes the provided Course unusable inside the function
{ 
    Course course; // define a new, local Course named course that has been default 
                   // initialized

    outFile << "Class: " << course.courseName 
            << " Class ID: " << course.courseNum 
            << " Meeting Days: " << course.courseDay 
            << " Class Time: " << course.courseTime 
            << " Class Location: " << course.courseLoc 
            << endl; // print out the contents of the local Course
}

不是我们想要的。我们想打印出传入的课程。让我们实现它

void writeInfo(ofstream &outFile, // take an ofstream as a reference parameter. 
                                  // Call it OutFile
              Course course) // take a Course as a value parameter. Call it course.
                             // This will make a new course that is a copy of the Course 
                             // used as an argument by the caller. Note that a smart  
                             // compiler willeliminate copying where it can, but it might
                             // not recognize that the copy is not required here.
{ 
    outFile << "Class: " << course.courseName 
            << " Class ID: " << course.courseNum 
            << " Meeting Days: " << course.courseDay 
            << " Class Time: " << course.courseTime 
            << " Class Location: " << course.courseLoc 
            << endl; // print out the contents of the Course parameter
}

现在让我们变得更好

void writeInfo(ofstream &outFile, // take an ofstream as a reference parameter. 
                                  // Call it OutFile
              const Course & course) // take a Course as a constant reference parameter. 
                                     // Call it course
                                     // As a reference the argument will not be copied, 
                                     // and as a constant it cannot be accidentally changed 
                                     // and can accepta short-lived temporary value that 
                                     // you otherwise cannot use as an argument for a 
                                     // reference. This is handy sometimes. You see it
                                     // all the time with string literals being 
                                     // transformed into temporary std::strings
{ 
    outFile << "Class: " << course.courseName 
            << " Class ID: " << course.courseNum 
            << " Meeting Days: " << course.courseDay 
            << " Class Time: " << course.courseTime 
            << " Class Location: " << course.courseLoc 
            << endl; // print out the contents of the Course parameter
}

Why is a function without argument identifiers valid in C++? 中询问和回答了为什么 C++ 中允许使用未命名参数

【讨论】:

    猜你喜欢
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 2013-02-27
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多