2013-09-30 55 views
0

因此,我所做的是這樣的;是否有可能在代碼(C++ I/O文件)中使用輸出文件作爲輸入文件

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

基本上我想做什麼與InputFile「warehouse.txt」和存儲內容的陣列讀取,然後添加此陣列和一個額外的數組OUTPUTFILE「updated.txt」。

然後我想使用'updated.txt'作爲輸入文件,如上面的代碼所示,這是允許的,我基本上想要將updated.txt中的所有數據存儲到一個大數組中,即將兩個數組結合起來,這是允許的嗎?我試了一下,我的編譯器似乎搞砸了,我正在閱讀有關使用矢量,而不是努力去理解它們。謝謝。

這裏是我的整體代碼,我想要做的是基本上從輸入文件中獲取'fruitname'及其相應數量的輸入文件。在extraarray中存儲額外的條目,然後將這兩個數組放在輸出文件中,如上所述,然後使用該輸出作爲輸入文件,這樣我就可以聚合數據。

問題:

當我嘗試從updated.txt存儲陣列我COUT的顯示,我在的地方應該是什麼fruitname和數量獲得隨機數。

#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++; 
    } 
} 
+3

要回答你的問題,是的。 – Paddyd

+1

是的,您應該可以,但取決於您想要完成的工作,似乎將輸出保存在內存中比稍後寫入更容易。 – nhgrif

+0

@Paddyd:看起來像是對我的回答。 –

回答

0

「我想要做的就是從inputfile中 'warehouse.txt' 讀」

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

... 「到OUTPUTFILE 'updated.txt'」

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

... 「然後我想用'updated.txt'作爲輸入文件'〜>創建另一個實例的ifstream E:如果你使用std::fstream

{ 
    std::ifstream ifs2("updated.txt"); 
    // reading from ifs2 ... 
} 
0

是的,這是可能的使用只有兩個文件流。例如:

#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; 
}