Hello guys!

I'm facing a problem with IO using files.
Here is the simple code that's kepping me awake:

Files::Files()
{
	numbers.open("textfile.txt");
	if ( numbers.good() )
	{
		cout << "Object from Files class ok!" << endl;
	}
	else
	{
		cout << "Object from Files class NOT ok!" << endl;
	}
}

'numbers' is a fstream object. According to MSDN, there's no default open mode for fstream, but some of the various 'open' definitions, the default is in and out. Confused.

That's my class constructor. The Files class only writes and reads numbers from the file above.
My problem lies here: numbers.open("textfile.txt"); , this method doesn't create the file if it doesn't exist. Already tried with numbers.open("textfile.txt", ios::in | ios::out); and still no luck.
I can't use ios::trunc or ios::app since is a must to write anywhere in file and don't lose it's content. And I can't manually create the file since the user can select different files to save/load at runtime.

I'm using Visual Studio 2008 on Windows 7 x64. Since that code is so simple, I'm starting to think it's a VS2008 problem or such;

Please advise!

ps: Taken from MSDN:

"The member function opens the file with filename filename, by calling fopen(filename, strmode). strmode is determined from mode &~(ate & | binary):

  • ios_base::in becomes "r" (open existing file for reading).
  • ios_base::out or ios_base::out | ios_base::trunc becomes "w" (truncate existing file or create for writing).
  • ios_base::out | app becomes "a" (open existing file for appending all writes).
  • ios_base::in | ios_base::out becomes "r+" (open existing file for reading and writing).
  • ios_base::in | ios_base::out | ios_base::trunc becomes "w+" (truncate existing file or create for reading and writing).
  • ios_base::in | ios_base::out | ios_base::app becomes "a+" (open existing file for reading and for appending all writes)."

"

So ios_base::in | ios_base::out should work, but... no... snif...

Recommended Answers

All 5 Replies

Can you post more of your class and perhaps a few lines of your text file?
It's not readily apparent (at least not to me anyway) what the problem is from the bit you posted.

Files.h:

#pragma once
#include <iostream>
#include <fstream>

using namespace std;

class Files
{
private:
	fstream numbers;
public:
	Files();
	~Files();
	void Reset();
	void Write(int position, int number);
	void Read(int* position, int* number);
};

Files.cpp

#include "Files.h"

Files::Files()
{
	this->numbers.open("textFile.txt", ios_base::in | ios_base::out);
	if ( this->numbers.good() )
	{
		cout << "Object from Files class is ok." << endl;
	}
	else
	{
		cout << "Object from Files class is NOT ok." << endl;
	}
}

Files::~Files()
{
	numbers.close();
	cout << "Object from Files class destroyed! Mua haha" << endl;
}

void Files::Reset()
{
	// Those are tied together, but better safe than sorry
	this->numbers.seekg(ios::beg);
	this->numbers.seekp(ios::beg);
}

void Files::Write(int position, int number)
{
	this->numbers << position << number;
}

void Files::Read(int* position, int* number)
{
	this->numbers >> *position >> *number;
}

source.cpp

#include "Files.h"

int main(int argc, char* argv[])
{
	cout << "Files test\n" << endl;
	Files file;
	int position = 1;
	int number;
	file.Reset(); // Could skip that
	int internalPosition = 1;
	while ( 1 )
	{
		cout << "Type number for position nº " << posicao << ": ";
		cin >> number;
		cin.ignore(100, '\n');
		if ( number > 0 )
		{
			file.Write(position, number);
		}
		else if ( number < 0 ) break; // Negative numbers stop the while
		position++;
		internalPosition++;
	}
	file.Reset();
	internalPosition = position;
	while ( internalPosition > 0 )
	{
		file.Read(&position, &number); // This call must move! Maybe inside the else
		if ( position != internalPosition )
		{
			cout << "Position nº "<< internalPosition << " -> " << 0;
		}
		else 
		{
			cout << "Position nº "<< position << " -> " << number;
		}
		internalPosition--;
	}
	int pause;
	cin >> pause;
	return 0;
}

Apologize if some names are different, i had to translate this. This program records the position and number the user inputs if it's greater than 0, but if it's 0, skips; than prints out those numbers, including the non-recorded zeros.

That's a standalone program I'm toying to include in a bigger project, that's why some explanation and the "choose file" code is missing.

Check out the third post of this thread. It may hold a solution for you.
I tried the ios_base::in | ios_base::out | ios_base::app approach and the compiler did not complain but it does not seem to work either (which is what is discussed for the rest of that thread -- but I don't know if it still works on the Microsoft compilers today as they were speaking of VC++ 6).
So basically the thread was suggesting if it doesn't open, open one with out only, close that one and reopen it (presumably you can open for in and out at that point).
Hope this is what you were looking for -- there may be a more straightforward solution but I don't know one offhand.

Well, that worked. Thank you very much! ^^

Glad it did!

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.