-4

So basically with my code I take inputfile (warehouse.txt) add extra info to it (fruitname, fruitquantity) from an array then put all the info from this file (warehouse.txt) PLUS the extra info from array to an output file (updated.txt).

Now the issue is I want to use this output file ("updated.txt") as an input file and in my code you'll see how I've done this. The problem is that when I open the file (updataed.txt) to try and use it an input file the strategic cout says it can't open file so it cant aggregate my data (main aim of program to sort all files so there's only one fruit name with its corresponding quantity): (look at lines 35 and 36 and tell me please if I can/cant do that to an inputfile?)

Run the program yourselves and you'll see that it didn't open updated.txt, just make a quick txt file warehouse.txt e.g

{
apple 4
apple 2
pear 3
mango2
pear 3
}

My code currently looks like this:

#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

typedef struct items {

    string name;
    int quantity;
} items_t;

void fileopenchecker(ifstream &FILE);
void printfile(items_t fruit [], int entries);
int readfromfile(ifstream &FILE, items_t fruit []);
int extrarray(items_t fruit []);
void writetooutputfile(ofstream &OFILE, items_t fruit [], int size);
int aggregatedataA(items_t overallfruit [], items_t samefruit [], items_t uniquefruit [], int num);
int aggregatedataB(items_t overallfruit [], items_t samefruit [], items_t uniquefruit [], int num);

int main()
{
    const int MAX_SIZE = 150;
    int Nfruit = 0;
    int Nextrafruit = 0;
    int total;
    int A, B;
    ifstream infile("warehouse.txt"), ffile("updated.txt");
    ofstream outfile("updated.txt"), overallfile("FINAL.txt");
    items_t extrafruit[MAX_SIZE], fruit[MAX_SIZE], overallfruit[MAX_SIZE], samefruit[MAX_SIZE], uniquefruit[MAX_SIZE];

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

    fileopenchecker(ffile);
    total = readfromfile(ffile, overallfruit);
    A = aggregatedataA(overallfruit, samefruit, uniquefruit, total);
    B = aggregatedataB(overallfruit, samefruit, uniquefruit, total);
    ffile.close();
    writetooutputfile(overallfile, samefruit, A);
    writetooutputfile(overallfile, uniquefruit, B);
    overallfile.close();
    return 0;
}

int aggregatedataA(items_t overallfruit [], items_t samefruit [], items_t uniquefruit [], int num){
    int i, j, x = 0, y = 0;
    for (i = 0; i < num; i++){
        for (j = 0; j < num; j++){
            if (overallfruit[i].name == overallfruit[j].name){
                samefruit[x].name = overallfruit[i].name;
                samefruit[x].quantity = overallfruit[i].quantity + overallfruit[j].quantity;
                x++;
            }
            else{
                uniquefruit[y].name = overallfruit[i].name;
                uniquefruit[y].quantity = overallfruit[i].quantity;
                y++;
            }
        }
    }
    return x;
}

int aggregatedataB(items_t overallfruit [], items_t samefruit [], items_t uniquefruit [], int num){
    int i, j, x = 0, y = 0;
    for (i = 0; i < num; i++){
        for (j = 0; j < num; j++){
            if (overallfruit[i].name == overallfruit[j].name){
                samefruit[x].name = overallfruit[i].name;
                samefruit[x].quantity = overallfruit[i].quantity + overallfruit[j].quantity;
                x++;
            }
            else{
                uniquefruit[y].name = overallfruit[i].name;
                uniquefruit[y].quantity = overallfruit[i].quantity;
                y++;
            }
        }
    }
    return y;
}

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

void printfile(items_t fruit [], int entries){

    int i;
    cout << "Printing from file!" << endl;

    for (i = 0; i < entries; i++){
        cout << fruit[i].name << "," << fruit[i].quantity;
    }
}

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

1 回答 1

0

至少在我看来,您的代码有足够多的问题,重新开始可能比尝试从当前起点修复问题更容易。

如果有任何选择,我会从使用std::mapor开始std::unordered_map。我可能还会跳过交互式输入(笨拙且无用,除非您愿意花费大量工作来编写一个像样的文本编辑器),而只是从许多不同的文件中获取输入,将它们合并在一起,然后生成您的最终输出。

我认为这个一般顺序上的某些内容可能至少是一个合理的起点:

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

void process_file(char const *name, std::map<std::string, int> &inv) {
    std::ifstream in(name);
    std::string fruit;
    int qty;

    while (in >> fruit >> qty)
        inv[fruit] += qty;
}

typedef std::pair<std::string, int> item;

namespace std {
    std::ostream &operator<<(std::ostream &os, item const &t) {
        return os << t.first << "\t" << t.second;
    }
}

int main(int argc, char **argv) {
    std::map<std::string, int> inventory;

    for (int i = 1; i < argc - 1; i++) 
        process_file(argv[i], inventory);

    std::ofstream out(argv[argc-1]);
    std::copy(std::begin(inventory), std::end(inventory),
        std::ostream_iterator<item>(out, "\n"));
}
于 2013-09-24T16:15:17.033 回答