I'm having trouble with this program from right out of the book. Its just supposed to write an integer array to a file that it creates. I've compiled it without any errors, but when I try to run it, it fails and tries to debug. can anyone help me figure out whats wrong?
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
const int ROWS = 3,
COLS = 3;
int num[ROWS][COLS] = {34, 55, 1899,
77, 999, 2837,
78, 3882, 1};
fstream writeFile;
writeFile.open ("testFile.txt", ios::in); // To test if the file already exists
if (writeFile.fail())
{
writeFile.open ("testFile.txt", ios::out | ios::in);
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
writeFile << setw(8) << num[row][col];
} writeFile << endl;
}
writeFile.close();
}
else
{
writeFile.close(); // Is there another way to add ios::out with out closing the file first?
writeFile.open ("testFile.txt", ios::out | ios::in);
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
writeFile << setw(8) << num[row][col];
} writeFile << endl;
}
writeFile.close();
}
return 0;
}
these are the errors after running the program.
'messing around.exe': Loaded 'C:\Users\Tyler\Documents\Visual Studio 2005\Projects\messing around\debug\messing around.exe', Symbols loaded.
'messing around.exe': Loaded 'C:\Windows\System32\ntdll.dll', No symbols loaded.
'messing around.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', No symbols loaded.
'messing around.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc80.debugcrt_1fc8b3b9a1e18e3b_8.0.50727.762_none_24c8a196583ff03b\msvcp80d.dll', Symbols loaded.
'messing around.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc80.debugcrt_1fc8b3b9a1e18e3b_8.0.50727.762_none_24c8a196583ff03b\msvcr80d.dll', Symbols loaded.
'messing around.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', No symbols loaded.
The program '[6212] messing around.exe: Native' has exited with code 0 (0x0).