1

昨日、C++でなければならない新しいアカウントプログラムの大きな契約について投稿しました。私の質問は終わりましたが、多くのエラーがあったためだと思います。多くの作業と地元のC++専門家からの相談により、私は元のコードを修正しました。

#include <accounting.h>
#include <stdio.h>

char *main()
{
    accounting bank = 100debits;
    bank = bank + 200debits;
    return printf("bal: %accounting\n", bank);
}

そして、私たちが定義したいくつかのクラスを持つ新しいバージョンはうまく機能しますが、唯一の問題は、C++がファイルに新しい行を書き込めないことです。以下のコードはそのまま機能しますが、コメント行を戻すとファイルに出力されません。

#include <stdlib.h> 
#include <stdio.h>
#include <cstring>
#define accounting float
#define print_accounting(x)  x "%0.2f"
#define debits * 1.0F
#define credits * -1.0F

int main()
{
    accounting bank = 100 debits;
    bank = bank + 200 debits;
    char my_bal[((unsigned short)-1)];
    sprintf(my_bal, print_accounting("bal:"), bank);
    char write_file[((unsigned short)-1)];
    write_file[NULL] = 0;
    strcat(write_file, "@echo ");
    strcat(write_file, my_bal);
//  strcat(write_file, "\n");  -- Wont work --
    strcat(write_file, " > c:\\SAP_replace\\bal.txt");
    system(write_file);
    return 0;
}
4

1 に答える 1

4

echoファイルの最後に自動的に改行を書き込みます。

2 つの改行が必要な場合は、次のような別の行を追加します。

system ("echo. >>c:\SAP_replace\\bal.txt");

現在のsystem()呼び出しの後。

または、別のプロセスを生成して出力を行うという古風なアイデア全体を破棄し、代わりに を使用iostreamsしてジョブを実行することもできます。これは、C++ で行うべき方法です。たとえば、次のようになります。

#include <iostream>
#include <fstream>
int main (void) {
    float fval = 0.123f;
    std::ofstream os ("bal.txt");
    os << "bal: " << fval << '\n';
    os.close();
    return 0;
}

出力:

bal: 0.123
于 2012-12-14T08:26:43.403 に答える