鱼C论坛

 找回密码
 立即注册
查看: 2102|回复: 4

[已解决]为什么会显示找不到文件呢

[复制链接]
发表于 2020-5-13 10:34:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. // Exercise 14.6 Solution: Ex14_06.cpp
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <fstream>
  5. #include <string>
  6. #include <cstdlib> // exit function prototype
  7. using namespace std;

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

  9. int main()
  10. {
  11.    int masterAccount; // holds account from old master file
  12.    int transactionAccount; // holds account from transactions file
  13.    double masterBalance; // holds balance from old master file
  14.    double transactionBalance; // holds balance from transactions file
  15.    string masterFirstName; // first name from master file
  16.    string masterLastName; // last name from master file

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

  21.    // terminate application if old master file cannot be opened
  22.    if ( !inOldMaster )
  23.    {
  24.       cerr << "Unable to open oldmast.dat\n";
  25.       exit( 1 );
  26.    } // end if
  27.    
  28.    // terminate application if transactions file cannot be opened
  29.    if ( !inTransaction )
  30.    {
  31.       cerr << "Unable to open trans.dat\n";
  32.       exit( 1 );
  33.    } // end if

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

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

  43.    // read from master file until end of transactions file reached
  44.    while ( !inTransaction.eof() )
  45.    {
  46.       inOldMaster >> masterAccount >> masterFirstName
  47.          >> masterLastName >> masterBalance;
  48.       
  49.       // display accounts from master file until
  50.       // number of new account is reached
  51.       while ( masterAccount < transactionAccount && !inOldMaster.eof() )
  52.       {
  53.          printOutput( outNewMaster, masterAccount, masterFirstName,
  54.             masterLastName, masterBalance );
  55.          inOldMaster >> masterAccount >> masterFirstName
  56.             >> masterLastName >> masterBalance;
  57.       } // end while

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

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

  67.       // if matching account found, update balance and output account info
  68.       if ( masterAccount == transactionAccount )
  69.       {
  70.          masterBalance += transactionBalance;
  71.          printOutput( outNewMaster, masterAccount, masterFirstName,
  72.             masterLastName, masterBalance );
  73.       } // end if

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

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

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

  87. // function to display output
  88. void printOutput( ofstream &oRef, int mAccount, string mfName,
  89.    string mlName, double mBalance )
  90. {
  91.    // set output format
  92.    cout << fixed << showpoint;
  93.    oRef << fixed << showpoint;

  94.    // display account number, name and balance
  95.    oRef << mAccount << ' ' << mfName << ' ' << mlName << ' '
  96.       << setprecision( 2 ) << mBalance << '\n';
  97.    cout << mAccount << ' ' << mfName << ' ' << mlName << ' '
  98.       << setprecision( 2 ) << mBalance << '\n';
  99. } // end function printOutput
复制代码


这是ex-14-06的代码
所有文件和源代码cpp文件都是放在同一个文件夹下的
最佳答案
2020-5-13 10:37:18
看下你这个代码是在哪个平台下编译的··在windows下,生成的exe会放到debug文件夹下,而你的.dat文件是和代码在同一个文件下,这样肯定找不到。如果在Linux下,这个路径可以指定。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-13 10:37:18 | 显示全部楼层    本楼为最佳答案   
看下你这个代码是在哪个平台下编译的··在windows下,生成的exe会放到debug文件夹下,而你的.dat文件是和代码在同一个文件下,这样肯定找不到。如果在Linux下,这个路径可以指定。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

是在windows下的,那是应该把文件移到debug文件夹下吗还是?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-13 10:44:16 | 显示全部楼层
是的,放到和exe在同一个文件夹下。你也可以用全局路径试试...
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

好的谢谢你
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 06:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表