Assistance with file input and output in C++ -
first of all, thank , hope i'm following guidelines correctly, if i'm not, i'd more happy correct it.
i need assignment have. after getting run, program won't open files need. program should open file named prog10in.txt
, output file named prog10out.txt
however, put line in tell me if failed , keeps coming up. doing in visual studio. need understand how open file whether error in code or in visual studio. program compiles without error.
the error should in getaccountinfo()
or outputfile()
.
source.cpp
:
#include <iostream> #include <string> #include "account.h" #include "bank.h" #include <cstdio> #include <fstream> #include <iomanip> using namespace std; ifstream instream; int main(int argc, char* argv[]) { account accounts[maximum_accounts]; bank customer; int accountnumber = 0; int id; string lastname; string firstname; double balance; customer.getaccountinfo(argv, accountnumber, id, lastname, firstname,balance, accounts); customer.sort(accountnumber, accounts); customer.outputfile(argv, accountnumber, accounts); }
there account.h
, account.cpp
file holds account information class.
bank.cpp
:
#include <iostream> #include <string> #include "account.h" #include "bank.h" #include <cstdio> #include <fstream> #include <iomanip> void bank::getaccountinfo(char* argv[], int& accountnumber, int& accountid, std::string& accountlastname, std::string& accountfirstname, double& accountbalance, account customers[maximum_accounts]) { std::ifstream fin; int accountindex = 0; fin.open(argv[1]); if (fin.fail()) { std::cout << "input file did not open" << std::endl; system("pause"); exit(1); } while (!fin.eof() && accountindex < maximum_accounts) { std::cout.setf(std::ios::fixed | std::ios::showpoint | std::ios::right); std::cout.precision(2); fin >> accountid >> accountlastname >> accountfirstname >> accountbalance; fin.ignore(1, ' '); customers[accountindex]._accountid = accountid; customers[accountindex]._accountlastname = accountlastname; customers[accountindex]._accountfirstname = accountfirstname; customers[accountindex]._accountbalance = accountbalance; accountindex++; accountnumber++; } std::cout << "clients: " << accountindex << std::endl; } void bank::sort(int accountnumber, account customers[maximum_accounts]) { int nextsmallest; int idtemp; std::string lastnametemp; std::string firstnametemp; double balancetemp; (int accountindex = 0; accountindex < accountnumber; accountindex++) { nextsmallest = accountindex; (int accountindex2 = accountindex + 1; accountindex2 < accountnumber; accountindex2++) { if (customers[accountindex2]._accountid < customers[nextsmallest]._accountid) { nextsmallest = accountindex2; } idtemp = customers[accountindex]._accountid; customers[accountindex]._accountid = customers[nextsmallest]._accountid; customers[nextsmallest]._accountid = idtemp; lastnametemp = customers[accountindex]._accountlastname; customers[accountindex]._accountlastname = customers[nextsmallest]._accountlastname; customers[nextsmallest]._accountlastname = lastnametemp; firstnametemp = customers[accountindex]._accountfirstname; customers[accountindex]._accountfirstname = customers[nextsmallest]._accountfirstname; customers[nextsmallest]._accountfirstname = firstnametemp; balancetemp = customers[accountindex]._accountbalance; customers[accountindex]._accountbalance = customers[nextsmallest]._accountbalance; customers[nextsmallest]._accountbalance = balancetemp; } } } void bank::outputfile(char* argv[], int accountnumber, account customers[maximum_accounts]) { int outfileindex; std::ofstream fout; fout.open(argv[2]); if (fout.fail()) { std::cout << "output file did not open" << std::endl; system("pause"); exit(1); } if (fout.fail()) { std::cout << "output failed" << std::endl; exit(0); } fout.setf(std::ios::fixed | std::ios::showpoint | std::ios::right); fout.precision(2); (outfileindex = 0; outfileindex < accountnumber; outfileindex++) { fout << std::setw(6) << customers[outfileindex]._accountid; fout << std::setw(10) << customers[outfileindex]._accountlastname; fout << std::setw(10) << customers[outfileindex]._accountfirstname; fout << std::setw(10) << customers[outfileindex]._accountbalance; fout << std::setw(10) << std::endl; } }
again, thank guys help.
i think program don't find input file. right? because output file created if not exists.
so input, if use absolute path should work unless path wrong. if using relative path. should know running program visual studio current directory (.) projects directory.
Comments
Post a Comment