栗粒粒 发表于 2020-5-13 10:34:10

为什么会显示找不到文件呢

// Exercise 14.6 Solution: Ex14_06.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib> // exit function prototype
using namespace std;

void printOutput( ofstream&, int, string, string, double ); // prototype

int main()
{
   int masterAccount; // holds account from old master file
   int transactionAccount; // holds account from transactions file
   double masterBalance; // holds balance from old master file
   double transactionBalance; // holds balance from transactions file
   string masterFirstName; // first name from master file
   string masterLastName; // last name from master file

   // file streams for input and output files
   ifstream inOldMaster( "oldmast.dat" );
   ifstream inTransaction( "trans.dat" );
   ofstream outNewMaster( "newmast.dat" );

   // terminate application if old master file cannot be opened
   if ( !inOldMaster )
   {
      cerr << "Unable to open oldmast.dat\n";
      exit( 1 );
   } // end if
   
   // terminate application if transactions file cannot be opened
   if ( !inTransaction )
   {
      cerr << "Unable to open trans.dat\n";
      exit( 1 );
   } // end if

   // terminate application if new master file cannot be opened
   if ( !outNewMaster )
   {
      cerr << "Unable to open newmast.dat\n";
      exit( 1 );
   } // end if

   // display account currently being processed
   cout << "Processing...\n";
   inTransaction >> transactionAccount >> transactionBalance;

   // read from master file until end of transactions file reached
   while ( !inTransaction.eof() )
   {
      inOldMaster >> masterAccount >> masterFirstName
         >> masterLastName >> masterBalance;
      
      // display accounts from master file until
      // number of new account is reached
      while ( masterAccount < transactionAccount && !inOldMaster.eof() )
      {
         printOutput( outNewMaster, masterAccount, masterFirstName,
            masterLastName, masterBalance );
         inOldMaster >> masterAccount >> masterFirstName
            >> masterLastName >> masterBalance;
      } // end while

      // tell user if account from transactions file does not match
      // account from master file
      if ( masterAccount > transactionAccount )
      {
         cout << "Unmatched transaction record for account "
            << transactionAccount << '\n';

         // get account and balance from transactions file
         inTransaction >> transactionAccount >> transactionBalance;
      } // end if

      // if matching account found, update balance and output account info
      if ( masterAccount == transactionAccount )
      {
         masterBalance += transactionBalance;
         printOutput( outNewMaster, masterAccount, masterFirstName,
            masterLastName, masterBalance );
      } // end if

      // get next account and balance from transactions file
      inTransaction >> transactionAccount >> transactionBalance;
   } // end while

   // output remaining accounts to new master file
   while ( !inOldMaster.eof() )
   {
      inOldMaster >> masterAccount >> masterFirstName
         >> masterLastName >> masterBalance;

      if (!inOldMaster.eof())
         printOutput( outNewMaster, masterAccount, masterFirstName,
            masterLastName, masterBalance );
   } // end while
} // end main

// function to display output
void printOutput( ofstream &oRef, int mAccount, string mfName,
   string mlName, double mBalance )
{
   // set output format
   cout << fixed << showpoint;
   oRef << fixed << showpoint;

   // display account number, name and balance
   oRef << mAccount << ' ' << mfName << ' ' << mlName << ' '
      << setprecision( 2 ) << mBalance << '\n';
   cout << mAccount << ' ' << mfName << ' ' << mlName << ' '
      << setprecision( 2 ) << mBalance << '\n';
} // end function printOutput

这是ex-14-06的代码
所有文件和源代码cpp文件都是放在同一个文件夹下的

上善若水··· 发表于 2020-5-13 10:37:18

看下你这个代码是在哪个平台下编译的··在windows下,生成的exe会放到debug文件夹下,而你的.dat文件是和代码在同一个文件下,这样肯定找不到。如果在Linux下,这个路径可以指定。

栗粒粒 发表于 2020-5-13 10:41:34

上善若水··· 发表于 2020-5-13 10:37
看下你这个代码是在哪个平台下编译的··在windows下,生成的exe会放到debug文件夹下,而你的.dat文件是和 ...

是在windows下的,那是应该把文件移到debug文件夹下吗还是?

上善若水··· 发表于 2020-5-13 10:44:16

是的,放到和exe在同一个文件夹下。你也可以用全局路径试试...

栗粒粒 发表于 2020-5-13 11:51:16

上善若水··· 发表于 2020-5-13 10:44
是的,放到和exe在同一个文件夹下。你也可以用全局路径试试...

好的谢谢你
页: [1]
查看完整版本: 为什么会显示找不到文件呢