Hi everybody, I have a problem with output files. I have installed Code::Blocks and MinGW, replacing my old Borland compiler. I am trying to make a program that uses arrays and files:

#include <iostream>
#include <fstream.h>
#include <conio.h>
#include <string>
#include <stdlib.h>
#include <cassert>

using namespace std;

void Save(ofstream& f, int Array[], int NumUsed, char fileName[], int Cursor);
void Load(ifstream& f, ofstream& g, int Array[], int NumUsed, int& Cursor, char fileName[]);
void Insert(int Array[], int& NumUsed, int& Position, int Num);
void Delete(int Array[], int& NumUsed, int Position);
void Sort(int Array[], int NumUsed, bool Ascending = false);
int Search(int Array[], int NumUsed, int Num);
int SmallestIndex(int Array, int NumUsed, int Index);
string getBoolString(bool B);
void displayArray(int Array[100], int numUsed, int Cursor);

int main()
{
	char fileName[27] = "Num.txt", ch;
	ifstream Input;
	ofstream Output;
	int numUsed = 0, Cursor = 0;
	int Array[100] = {-1};
	bool Circulating = false, Descending = true;

	Load(Input, Output, Array, numUsed, Cursor, fileName);

	while (true)
	{
      clrscr();

		cout << "[F]ilename   [I]nsert   [S]earch   [D]elete   S[o]rt   [C]ircular? "
				<< getBoolString(Circulating)
				<< "   Descending? "
				<< getBoolString(Descending)
				<< "   E[x]it"
				<< endl << endl;

		cout << "{";

		while (Array[numUsed] != -1)
		{
			numUsed++;
		}

        displayArray(Array, numUsed, Cursor);

      cout << "}" << endl << endl;
		ch = getch();
		switch (toupper(ch))
		{
		case 'C':    //Circulating?
			if (Circulating)
				Circulating = false;
			else
				Circulating = true;
			break;
		case 0:    //Special key pressed
			ch = getch();
			switch (ch)
			{
			case 'P':     //Down
				Descending = true;
				break;
			case 'H':     //Up
				Descending = false;
				break;
			case 'K':     //Left
				if (Cursor > 1)
				{
					Cursor--;
				}
				else
				{
					if (Circulating)
					{
						Cursor = numUsed;
					}
				}
				gotoxy(Cursor, 1);
				break;
			case 'M':     //Right
				if (Cursor < numUsed)
				{
					Cursor++;
				}
				else
				{
					if (Circulating)
					{
						Cursor = 1;
					}
				}
				gotoxy(Cursor, 1);
				break;
			}
			break;
		case 'F':
			cout << "\n\nCurrent Filename:" << fileName;
			cout << "\nNew Filename: ";
			cin >> fileName;
         break;
      case 'X':
			Save(Output, Array, numUsed, fileName, Cursor);
         exit(1);
			break;
		}
		cout << "\n--------------------------------------------\n";
	}
}

void Save(ofstream& f, int Array[], int NumUsed, char fileName[], int Cursor)
{
	f.open(fileName);

	if (f.fail())
	{
		cout << endl << endl << "Error writing file!";
		getch();
		exit(1);
	}

    cout << f.failbit;

	for (int i = 0; i < NumUsed; i++)
	{
		f << Array[i] << " ";
	}

	f.close();

	f.open("Array.ini");

	f << fileName;
	f << Cursor;
}

void Load(ifstream& f, ofstream& g, int Array[], int NumUsed, int& Cursor, char fileName[])
{
	int i = 0;

	f.open("Array.ini");

	if (f.fail())  //Array.ini does not exist
	{
		g.open("Array.ini");
		g << "Num.txt" << ' ';
		g << -1;

		g.close();
		g.clear();
	}

	f >> fileName;
	f >> Cursor;

	f.close();
	f.clear();

	f.open(fileName);

	if (f.fail())  //Num.txt does not exist
	{
		g.open("Num.txt");
		g << -1;
		f.close();
		f.open("Num.txt");
	}

    while ((f >> Array[i]) && (Array[i] != -1))
    {
        f >> Array[i];
        i++;
    }


   f.close();
   g.close();
}


string getBoolString(bool B)
{
	if (B)
		return "Yes";
	else
		return "No";
}

void displayArray(int Array[100], int numUsed, int Cursor)
{
		for (int j = 0; j < numUsed; j++)
		{
			if (j != Cursor)
				cout << Array[j] << ' ';
			else
				cout << '[' << Array[j] << "] ";
		}
}

This compiles fine, but when i exit and call the save function, f.fail evaluates to true. I have tried the bad function and it returns false, so i know that badbit is not the problem, so failbit must be true. I have displayed failbit, and it's value is 4. Why does failbit evaluate to true?

Sorry for the title, i messed up...

I'm not sure right off hand what is going on, but here is a couple of things that I like to try when the unexpected starts to happen in my file i/o operations:

  • try opening your file in binary mode
  • it seems like you might be using your ofstream object in other places, be sure to clear() your object after each use (resets error state flags)

Before you open it, check if the file is already opened at your save function.

I have displayed failbit, and it's value is 4. Why does failbit evaluate to true?

To answer just this question, an integer is always true unless it is zero。

I don't know if that helps though。 I'm going to do some studying of your code to fully understand it。

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.