Wee little issue with using I/O files in C++ (strategic couts didnt help) -
so code take inputfile (warehouse.txt
) add info (fruitname
, fruitquantity
) array put info file (warehouse.txt
) plus info array output file (updated.txt
).
now issue want use output file ("updated.txt") input file , in code you'll see how i've done this. problem when open file (updataed.txt
) try , use input file strategic cout
says can't open file cant aggregate data (main aim of program sort files there's 1 fruit name corresponding quantity): (look @ lines 35 , 36 , tell me please if can/cant inputfile?)
run program yourselves , you'll see didn't open updated.txt
, make quick txt file warehouse.txt
e.g
{ apple 4 apple 2 pear 3 mango2 pear 3 }
my code looks 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); = 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; (i = 0; < num; i++){ (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; (i = 0; < num; i++){ (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 not detected!" << endl; exit(1); } else{ cout << "your file detected" << endl; } } void printfile(items_t fruit [], int entries){ int i; cout << "printing file!" << endl; (i = 0; < 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 add entries file? (yes-->1 no-->0)" << endl; cin >> runner; if (runner == 0){ break; } //take itemname , quantity , stores in array. cout << "enter name of fruit , 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++; } }
at least in opinion, code has enough problems it's easier start on try fix things current starting point.
given choice in matter, i'd start using std::map
or std::unordered_map
. i'd skip interactive input (clumsy , useless unless you're willing put quite bit of work writing decent text editor) , take input number of different files, merge together, , produce final output.
i think on general order might @ least reasonable starting point:
#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; (int = 1; < 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")); }
Comments
Post a Comment