C++ 从文件输入到类对象数组c++;

C++ 从文件输入到类对象数组c++;,c++,arrays,class,object,C++,Arrays,Class,Object,我不知道如何输入我的数据。对不起,我是一个傻瓜,我发现像这样的例子太复杂了,我不明白到底发生了什么。我知道这甚至不接近正确,但我不知道如何设置文件的输入。我的文件数据如下所示: 986 8 432 24 132 100 123 89 329 50 503 30 783 78 822 32 233 56 322 74 #include <iostream> #include <fstream> using namespace std; // Thi

我不知道如何输入我的数据。对不起,我是一个傻瓜,我发现像这样的例子太复杂了,我不明白到底发生了什么。我知道这甚至不接近正确,但我不知道如何设置文件的输入。我的文件数据如下所示:

986  8
432  24
132  100
123  89
329  50
503  30
783  78
822  32
233  56
322  74
#include <iostream>
#include <fstream>
using namespace std;


// This program defines a class called Inventory that has itemnumber (which 
// contains the id number of a product) and numofitem (which contains the 
// quantity on hand of the corresponding product)as private data members.
// The program will read these values from a file and store them in an 
// array of objects (of type Inventory).  It will then print these values
// to the screen.

// Example: Given the following data file:
//     986 8
//     432 24
// This program reads these values into an array of objects and prints the
// following:
//     Item number 986 has 8 items in stock
//     Item number 432 has 24 items in stock


const int NUMOFPROD = 10;   // This holds the number of products a store sells

class Inventory
{
public:

   void getId(int item);      // This puts item in the private data member 
                              // itemnumber of the object that calls it.
   void getAmount(int num);   // This puts num in the private data member
                              // numofitem of the object that calls it.
   void display();            // This prints to the screen 
                              // the value of itemnumber and numofitem of the 
                              // object that calls it.



private:

   int  itemNumber;         // This is an id number of the product
   int  numOfItem;          // This is the number of items in stock 

};


int main()
{

   ifstream infile;       // Input file to read values into array
   infile.open("inventory.dat");

   // Fill in the code that declares an array of objects of class Inventory
   // called products. The array should be of size NUMOFPROD
   Inventory products[NUMOFPROD];

   int pos;                   // loop counter
   int id;                    // variable holding the id number
   int total;                 // variable holding the total for each id number

   // Fill in the code that will read inventory numbers and number of items  
   // from a file into the array of objects. There should be calls to both  
   // getId and getAmount member functions somewhere in this code.
   // Example: products[pos].getId(id); will be somewhere in this code
   pos = 0;
   while(NUMOFPROD > pos++ && infile >> products[pos])
   {
        id = products[pos];
        products[pos].getId(id);


       //products[pos].getAmount(total);
   }
    infile.close();
   // Fill in the code to print out the values (itemNumber and numOfItem) for 
   // each object in the array products.
   // This should be done by calling the member function display within a loop
   pos = 0;
   while(NUMOFPROD > pos++)
   {
       products[pos].display();
   }

   return 0;

}


// Write the implementations for all the member functions of the class.

void getId(int item)
{
    itemNumber = item;

}   
void getAmount(int num)
{
    numOfItem = num;
}
void display()
{
    cout << itemNumber << " ";
}
到目前为止,我的程序是这样的:

986  8
432  24
132  100
123  89
329  50
503  30
783  78
822  32
233  56
322  74
#include <iostream>
#include <fstream>
using namespace std;


// This program defines a class called Inventory that has itemnumber (which 
// contains the id number of a product) and numofitem (which contains the 
// quantity on hand of the corresponding product)as private data members.
// The program will read these values from a file and store them in an 
// array of objects (of type Inventory).  It will then print these values
// to the screen.

// Example: Given the following data file:
//     986 8
//     432 24
// This program reads these values into an array of objects and prints the
// following:
//     Item number 986 has 8 items in stock
//     Item number 432 has 24 items in stock


const int NUMOFPROD = 10;   // This holds the number of products a store sells

class Inventory
{
public:

   void getId(int item);      // This puts item in the private data member 
                              // itemnumber of the object that calls it.
   void getAmount(int num);   // This puts num in the private data member
                              // numofitem of the object that calls it.
   void display();            // This prints to the screen 
                              // the value of itemnumber and numofitem of the 
                              // object that calls it.



private:

   int  itemNumber;         // This is an id number of the product
   int  numOfItem;          // This is the number of items in stock 

};


int main()
{

   ifstream infile;       // Input file to read values into array
   infile.open("inventory.dat");

   // Fill in the code that declares an array of objects of class Inventory
   // called products. The array should be of size NUMOFPROD
   Inventory products[NUMOFPROD];

   int pos;                   // loop counter
   int id;                    // variable holding the id number
   int total;                 // variable holding the total for each id number

   // Fill in the code that will read inventory numbers and number of items  
   // from a file into the array of objects. There should be calls to both  
   // getId and getAmount member functions somewhere in this code.
   // Example: products[pos].getId(id); will be somewhere in this code
   pos = 0;
   while(NUMOFPROD > pos++ && infile >> products[pos])
   {
        id = products[pos];
        products[pos].getId(id);


       //products[pos].getAmount(total);
   }
    infile.close();
   // Fill in the code to print out the values (itemNumber and numOfItem) for 
   // each object in the array products.
   // This should be done by calling the member function display within a loop
   pos = 0;
   while(NUMOFPROD > pos++)
   {
       products[pos].display();
   }

   return 0;

}


// Write the implementations for all the member functions of the class.

void getId(int item)
{
    itemNumber = item;

}   
void getAmount(int num)
{
    numOfItem = num;
}
void display()
{
    cout << itemNumber << " ";
}
#包括
#包括
使用名称空间std;
//该程序定义了一个名为Inventory的类,该类具有itemnumber(它
//包含产品的id号)和numofitem(其中包含
//对应产品的现存量)作为私有数据成员。
//程序将从文件中读取这些值并将其存储在
//对象数组(库存类型)。然后它将打印这些值
//到屏幕上。
//示例:给定以下数据文件:
//     986 8
//     432 24
//该程序将这些值读入对象数组并打印
//以下:
//项目编号986有8项库存
//项目编号432有24项库存
常量int numoprod=10;//它包含一家商店销售的产品数量
班级清单
{
公众:
void getId(int item);//这会将项放入私有数据成员中
//调用它的对象的itemnumber。
void getAmount(int num);//这将num放入私有数据成员中
//调用它的对象的numofitem。
void display();//这会打印到屏幕上
//的itemnumber和numofitem的值
//调用它的对象。
私人:
int itemNumber;//这是产品的id号
int numOfItem;//这是库存中的项目数
};
int main()
{
ifstream infle;//将值读入数组的输入文件
未结库存(“inventory.dat”);
//填写声明类清单对象数组的代码
//调用的产品。数组的大小应为NUMOFPROD
库存产品[NUMOFPROD];
int pos;//循环计数器
int id;//保存id号的变量
int total;//保存每个id号的总数的变量
//填写将读取库存编号和项目编号的代码
//从一个文件到对象数组中。应该同时调用这两个对象
//getId和getAmount成员函数在这段代码中的某个地方。
//示例:products[pos].getId(id);将位于此代码中的某个位置
pos=0;
而(NUMOFPROD>pos++&infile>>产品[pos])
{
id=产品[pos];
产品[pos].getId(id);
//产品[pos].getAmount(合计);
}
infle.close();
//填写代码以打印出的值(itemNumber和numOfItem)
//阵列产品中的每个对象。
//这应该通过在循环中调用成员函数display来实现
pos=0;
而(NUMOFPROD>pos++)
{
产品[pos].display();
}
返回0;
}
//编写类的所有成员函数的实现。
void getId(int项)
{
itemNumber=项目编号;
}   
void getAmount(int num)
{
numOfItem=num;
}
无效显示()
{

cout要从文件中输入和删除,请执行以下操作:

#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    ifstream fin("YourTxtFile.txt");
    fin >> something //Fin acts just like your standard cin

    fin.close() //Don't forget to close the stream when you're done!
}
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    ofstream fout("YourTxtFile.txt");
    fout >> something //Fout acts just like your standard cout

    fout.close() //Don't forget to close the stream when you're done!
}
#包括
#包括
使用名称空间std;
int main()
{
ifstream fin(“YourTxtFile.txt”);
fin>>某些东西//fin的行为与您的标准cin一样
fin.close()//完成后不要忘记关闭流!
}
如果要输出到文件,请执行以下操作:

#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    ifstream fin("YourTxtFile.txt");
    fin >> something //Fin acts just like your standard cin

    fin.close() //Don't forget to close the stream when you're done!
}
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    ofstream fout("YourTxtFile.txt");
    fout >> something //Fout acts just like your standard cout

    fout.close() //Don't forget to close the stream when you're done!
}
#包括
#包括
使用名称空间std;
int main()
{
流文件(“YourTxtFile.txt”);
fout>>有些东西//fout的行为就像你的标准cout一样
fout.close()//完成后不要忘记关闭流!
}
你也可以同时做这两件事

#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    ifstream fin("YourTxtFile1.txt");
    fin >> something //Fin acts just like your standard cin

    fin.close() //Don't forget to close the stream when you're done!

    ofstream fout("YourTxtFile2.txt");
    fout >> something //Fout acts just like your standard cout

    fout.close() //Don't forget to close the stream when you're done!
}
#包括
#包括
使用名称空间std;
int main()
{
ifstream fin(“YourTxtFile1.txt”);
fin>>某些东西//fin的行为与您的标准cin一样
fin.close()//完成后不要忘记关闭流!
流格式(“YourTxtFile2.txt”);
fout>>有些东西//fout的行为就像你的标准cout一样
fout.close()//完成后不要忘记关闭流!
}
只是想让你知道,fin和fout可以被称为任何你想要的

如果这篇文章回答了你的问题,请把它标记为答案


谢谢

使用google protobuf!好的,谢谢,但是我如何将同一行上的两个数字输入到一个类对象数组中,如示例中所示?fin>>num1>>num2;array[I]。itemNumber=num1;array[I]。numOfItem=num2;在for循环中。或者更好的是,fin>>array[I]。itemNumber>>array[I].numOfItem;如果您将我的帖子标记为答案,我将不胜感激。哦,我明白了,您必须创建一些可以编辑私有数据的方法,否则您将永远无法更改数据。好的-我明白num1 num2示例的情况。谢谢!!编辑私有变量没有问题-只需将信息输入到程序中即可早上好!谢谢!!