0

所以我做的是这个;

ifstream infile("warehouse.txt"); ffile("updated.txt");
ofstream outfile("updated.txt");

基本上我想做的是从输入文件'warehouse.txt'中读取并将内容存储在一个数组中,然后将此数组和一个额外的数组添加到输出文件'updated.txt'中。

然后我想使用'updated.txt'作为输入文件,如上面的代码所示是否允许,我基本上想将updated.txt上的所有数据存储到一个大数组中,即合并两个数组,这是否允许?我试过了,我的编译器似乎搞砸了,我正在阅读有关使用向量的信息,但很难理解它们。谢谢。

这是我想要做的总体代码,基本上是从输入文件中获取“fruitname”及其从输入文件中对应的数量。将额外的条目存储在一个额外的数组中,然后如上所述将这两个数组放入一个输出文件中,然后将该输出用作输入文件,以便我可以聚合数据。

问题:

当我尝试从 updated.txt 存储到数组时,我的 cout 显示我得到了随机数,而不是应该是什么水果名称及其编号。

#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>

using namespace std;

typedef struct items {
    string name;
    int quantity;
} items_t;

void fileopenchecker (ifstream &FILE);
int readfromfile (ifstream &FILE, items_t fruit[]);
int extrarray (items_t fruit[]);
void writetooutputfile (ofstream &OFILE, items_t fruit[], int size);
void combinearrays (ifstream &final, items_t overallfruit[], int total);

int main()
{
    const int MAX_SIZE = 150;
    int Nfruit = 0;
    int Nextrafruit = 0;
    int total;
    std::ifstream infile("warehouse.txt");
    std::ofstream outfile("updated.txt");
    std::ifstream ffile("updated.txt");
    items_t extrafruit[MAX_SIZE], fruit[MAX_SIZE], overallfruit[MAX_SIZE];

    fileopenchecker(infile);
    Nextrafruit = extrarray(extrafruit);
    Nfruit = readfromfile(infile, fruit);
    total = Nextrafruit + Nfruit;
    infile.close();
    writetooutputfile(outfile, fruit, Nfruit);
    writetooutputfile(outfile, extrafruit, Nextrafruit);

    combinearrays (ffile, overallfruit, total);

    ffile.close();

    return 0;
}

void combinearrays (ifstream &final, items_t overallfruit[], int total){
    int i;
    for(i=0; i<total; i++){
        final >> overallfruit[i].name >> overallfruit[i].quantity;
        cout << overallfruit[i].name << overallfruit[i].quantity << endl;
    }
}

void fileopenchecker (ifstream &FILE){
    if(!FILE.is_open()){
        cout << "Your file was NOT detected!" << endl;
        exit(1);
    }
    else{
        cout << "Your file was detected" << endl;
    }
}

 int readfromfile (ifstream &FILE, items_t fruit[]){

    int   entries = 0;

    while(!FILE.eof()){

        FILE >> fruit[entries].name >> fruit[entries].quantity;

        cout << fruit[entries].name << fruit[entries].quantity << endl;
        entries++;
    }
    return entries;
}

int extrarray (items_t fruit[]){
    int runner=1, exentries =0;
        while(runner==1){
            cout << "Would you like to add entries to your file? (YES-->1 NO-->0)" << endl;
            cin >> runner;

            if(runner==0){
                break;
            }

            //take the itemname and quantity and stores it in the array.
            cout << "Enter the name of the fruit and its quantity" << endl;
            cin >> fruit[exentries].name >> fruit[exentries].quantity;

            //debugging:
            cout << fruit[exentries].name << fruit[exentries].quantity << endl;
            exentries++;
        }
    return exentries;
}

void writetooutputfile (ofstream &OFILE, items_t fruit[], int size){
int entries = 0;

    while(entries < size){
        cout << fruit[entries].name << fruit[entries].quantity << endl;
        OFILE << fruit[entries].name << fruit[entries].quantity << endl;
        entries++;
    }
}
4

2 回答 2

0

“我想做的是从输入文件'warehouse.txt'中读取”

{
    std::ifstream ifs("warehouse.txt");
    // reading from ifs ...

... “输出文件'updated.txt'”

    std::ofstream ofs("updated.txt");
    // writing to ofs ...
}

... “然后我想使用'updated.txt'作为输入文件”〜>创建另一个实例ifstream

{
    std::ifstream ifs2("updated.txt");
    // reading from ifs2 ...
}
于 2013-09-30T15:35:51.813 回答
0

是的,如果您使用std::fstream. 例如:

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

int main(void)
{
    std::ifstream infile("a.txt");
    // create file for both reading and writing
    std::fstream ffile("b.txt", std::fstream::in | std::fstream::out | std::fstream::trunc);

    // read contents of file a and write to file b
    std::string line;
    while (std::getline(infile, line))
    {
        std::cout << line << std::endl;
        ffile << line << std::endl;
    }

    // flush the output to disk
    ffile.flush();

    // go back to the start of the output file before reading from it
    ffile.seekg(0);

    // read contents of output file again.
    while (std::getline(ffile, line))
    {
        std::cout << line << std::endl;
    }

    return 0;
}
于 2013-09-30T15:37:36.770 回答