Reputation: 21
Yesterday I post about a big contract for a new account program that must be in C++. My question closed but I think because it had to many errors. With much work and consuling from local C++ expert I've fixed the original codes we had:
#include <accounting.h>
#include <stdio.h>
char *main()
{
accounting bank = 100debits;
bank = bank + 200debits;
return printf("bal: %accounting\n", bank);
}
And new version with some classes we defined works well but the only problem is C++ can't write a new line to file. The codes below work as is but I get no output to file if I put back the comment line.
#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;
}
Upvotes: 1
Views: 1458
Reputation: 882806
echo
will automatically write a newline to the end of the file.
If you want two newlines, just add another line similar to:
system ("echo. >>c:\SAP_replace\\bal.txt");
after the current system()
call.
Or you could throw away the entire archaic idea of spawning another process to do output, and instead use iostreams
to do the job. That's the way you should be doing it in C++, something like:
#include <iostream>
#include <fstream>
int main (void) {
float fval = 0.123f;
std::ofstream os ("bal.txt");
os << "bal: " << fval << '\n';
os.close();
return 0;
}
which outputs:
bal: 0.123
Upvotes: 4