Hi,

been programming all day long, and tryed to compile my program, everything is fine, except some kind of nonsense with ofstream..

These are the problems, and i don't know why ofstream says such things, because in my other programs everything is fine with seekg, or tellg, and everything else which here is a problem

pro1.cpp: In function ‘void issaugoti(irasas*, int&)’:
pro1.cpp:140: error: no matching function for call to ‘std::basic_ofstream<char, std::char_traits<char> >::open()’
/usr/include/c++/4.4/fstream:696: note: candidates are: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
pro1.cpp:142: error: ‘struct std::ofstream’ has no member named ‘seekg’
pro1.cpp:148: error: ‘struct std::ofstream’ has no member named ‘tellg’

Function in which are these problems:

void issaugoti (irasas * issaugomas, int &kiekis)
{
	string sPB;
	bd(sPB);
	ofstream osB(sPB.c_str());
	if (osB.open())
	{
		osB.seekg(0, ios::end);
		time_t sek = time(0);
		tm *t = localtime(&sek);
		for (int i = 0; i < kiekis+1; i++)
		{
			int data = t->tm_year + t->tm_mon + t->tm_mday + t->tm_hour + t->tm_min + t->tm_sec;
			issaugomas[i].sDBID = sPB.c_str + osB.tellg() + data; //tellg pasako vietą, nuo kur reikia skaityti duomenis
			osB << issaugomas[i].sDBID << endl;
			osB << issaugomas[i].sAG << endl;
			osB << issaugomas[i].sP << endl;
			osB << issaugomas[i].sZ << endl;
			osB << issaugomas[i].sIM << endl;
			osB << issaugomas[i].sI << endl;
			osB << data;
		}	
		kiekis = 0;	
		osB.close();
	}
	else
	{
		cout << "Klaida: byla " << sPB << " negali būti atidaryta" << endl;
	}
}

Recommended Answers

All 9 Replies

you probably didn't include <fstream> and add using std::ofstream;

i have included :) i am using ifstream in another function - and there everything is okay - no errors (in the same program, i mean). I have added

using namespace std;

- isn't that enough, should i additionaly use

using std::ofstream;

?

Post the entire program so that we can compile it.

#include <iostream>
#include <fstream>
#include <string.h>
#include <cstring>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
//
using namespace std;
//
#define MAXBD 8388608 //65536
//
bool iseiti = false;
//
struct irasas
{
	string sDBID;
	string sAG; // atlikėjas/grupė
	string sP; // albumo pavadinimas
	string * sZ; // žanras (ar keli žanrai)
	string * sIM; // išleidimo metai (jeigu perleistas, tai kelios datos)
	string * sI; // įvertinimas (ar keli įvertinimai)
};
struct db_bylos
{
	string sBV; //Bylos vardas
	size_t stBD; //Bylos dydis
	int iIS; //Įrašų skaičius
};
//
void bd (string&);
void naujas_irasas (irasas*, int&);
void issaugoti (irasas*, int&);
void meniu ();
void submeniu ();
void pasirinkimas (char);
main ()
{
	string sPB;
	cout << setw(70) <<"---== Sveikas prisijungęs prie muzikinės duombazės ==---" << endl;
	do
	{
		meniu();
	}
	while(iseiti != true);
}
//
void bd (string &sPB) //bylos pavadinimas/ bylų duomenys 
{
	string sFS, sBIK;
	int iFS;
	ifstream osP("pagr"); //Pagrindinis
	if (osP.is_open())
	{
		getline (osP, sFS); //Failų skaičius
		iFS = atoi(sFS.c_str());
		db_bylos dbbB[iFS]; //pagal failų skaičių sukuriamas reikiamas kiekis struktūrų
		for (int i = 0; i < iFS; i ++)
		{
			getline (osP, dbbB[i].sBV); //Gauname bylos vardą
			ifstream isF(dbbB[i].sBV.c_str()); //atidarome bylą
			if (isF.is_open())
			{
				isF.seekg(0, ios::end); // keliauje į bylos pabaigą
				dbbB[i].stBD = isF.tellg(); // sužinome bylos dydį
				isF.seekg(0, ios::beg); // grįžtame į pradžią
				getline (isF, sBIK); // sužinome kiek byla talpina įrašų
				dbbB[i].iIS = atoi (sBIK.c_str());
				if (dbbB[i].stBD < MAXBD) //maximalaus dydžio konstanta 
				{
					sPB = dbbB[i].sBV;
				}
				isF.close();
			}
			else
			{
				cout << "Klaida: byla " << dbbB[i].sBV << " negali būti atidaryta" << endl;
			}
		}
		osP.close();
	}
	else
	{
		cout << "Klaida: nerastas pagrindinis nustatymų failas" << endl;
		//system("PAUSE");
		//return 0;
	}
}
//
void naujas_irasas (irasas * naujas, int &iIK)
{
	string sZ, sIM, sI, sT;
	static int siNI = 0;
	int i = 0;
	naujas = new irasas [siNI+1];
	cout << "Atlikėjas / Grupė: ";
	cin >> naujas[siNI].sAG;
	cout << "Albumo pavadinimas: ";
	cin >> naujas[siNI].sP;
	cout << "Žanras (kelis žanrus prašome atskirti \";\" ženklu): ";
	cin >> sZ;
	istringstream issT1(sZ);
	while (getline(issT1, sT, ';'))
	{
		naujas[siNI].sZ = new string [i+1];
		naujas[siNI].sZ[i] = sT;
		cout << naujas[siNI].sZ[i] << endl;
		i++;
	}
	i = 0;
	cout << "Išleidimo metai (kelis perleidimo metus prašome atskirti \";\" ženklu): ";
	cin >> sIM;
	istringstream issT2(sIM);
	while (getline(issT2, sT, ';'))
	{
		naujas[siNI].sIM = new string [i+1];
		naujas[siNI].sIM[i] = sT;
		i++;
	}
	i = 0;
	cout << "Įvertinimas (skirtingus įvertinimus prašome atskirti \";\" ženklu): ";
	cin >> sI;
	istringstream issT3(sI);
	while (getline(issT3, sT, ';'))
	{
		naujas[siNI].sI = new string [i+1];
		naujas[siNI].sI[i] = sT;
		i++;
	}
	iIK = siNI;
	siNI++;
	submeniu();
}
void issaugoti (irasas * issaugomas, int &kiekis)
{
	string sPB;
	bd(sPB);
	ofstream osB(sPB.c_str());
	if (osB.open())
	{
		osB.seekg(0, ios::end);
		time_t sek = time(0);
		tm *t = localtime(&sek);
		for (int i = 0; i < kiekis+1; i++)
		{
			int data = t->tm_year + t->tm_mon + t->tm_mday + t->tm_hour + t->tm_min + t->tm_sec;
			issaugomas[i].sDBID = sPB.c_str + osB.tellg() + data; //tellg pasako vietą, nuo kur reikia skaityti duomenis
			osB << issaugomas[i].sDBID << endl;
			osB << issaugomas[i].sAG << endl;
			osB << issaugomas[i].sP << endl;
			osB << issaugomas[i].sZ << endl;
			osB << issaugomas[i].sIM << endl;
			osB << issaugomas[i].sI << endl;
			osB << data;
		}	
		kiekis = 0;	
		osB.close();
	}
	else
	{
		cout << "Klaida: byla " << sPB << " negali būti atidaryta" << endl;
	}
}
void meniu ()
{
	cout << "Jūsų pasirinkimai:\n"
		<< "n: naujas įrašas\n"
		<< "p: paieška\n"
		<< "i: išeiti\n"
		<< ":=> ";
	pasirinkimas ('m');
}
void submeniu ()
{
	cout << "Pasirinkite ką norite daryti toliau:\n"
		<< "n: naujas įrašas\n"
		<< "s: išsaugoti pakeitimus\n"
		<< "i: išeiti\n"
		<< ":=> ";
	pasirinkimas ('s');
}
void pasirinkimas(char funkcija)
{
	char cI;
	static irasas * naujas;
	int iIK;
	cin >> cI;
	switch (cI)
	{
		case 'n' :
			naujas_irasas (naujas, iIK);
			break;
		case 's' : 
			issaugoti (naujas, iIK);
			break;
		case 'p' :
		case 'i' :
			iseiti = true;
			break;
		default :
			cout << "Klaida: bloga įvestis: tokios komandos nėra" << endl;
			if (funkcija = 'm')
			{
				meniu ();
			}
			else
			{
				submeniu ();
			} 
			break;
	}
}

Comments are in lithuanian, so you can ignore them. The program is not finished yet ant yet i have a lot of things to do.. But this is a strange problem, and i want to know what causes it.

What compiler are you using? VC++ 2010 does not allow you to declare variables like you did on line 58 because iFS must be a constant. The new c++ standards will probably allow it, but for now it's not permitted except as a compiler extension to the current standards. That error is most likely what's causing the problems you described.

Iam using gcc compiler: gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5). And it allows me to use such things. Ok. i'll try now to use a constant.

Edit: tryed a constant - same sh**. Am, and have you noticed, that it is not the function which causes problems ? with this function everything is ok.

For line 140, you want is_open() for sure.

EDIT: Also, the g in seekg is for "get pointer," which belongs with ifstream only. The corresponding functions end in p for the ofstream. (see http://www.cplusplus.com/reference/iostream/ofstream/ midpage).

Iam using gcc compiler: gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5). And it allows me to use such things. Ok. i'll try now to use a constant.

Edit: tryed a constant - same sh**. Am, and have you noticed, that it is not the function which causes problems ? with this function everything is ok.

See my edit above. I don't want to speak for him, but I don't think that AD was saying this was the cause of the problem, but he was offering you some advice on your code.

For line 140, you want is_open() for sure.

EDIT: Also, the g in seekg is for "get pointer," which belongs with ifstream only. The corresponding functions end in p for the ofstream. (see http://www.cplusplus.com/reference/iostream/ofstream/ midpage).

Thanks, now I will know that if/of streams has different functions :) thanks again for your help

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.