Jump to content
  • Advertisement
Sign in to follow this  
pex22

fopen,fputs,fclose function = function

This topic is 5036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm not trying to post too much (/long) topics, but I do things, then change some, and I back to the beginning. I don't know why it doesn't work:
// Engine
void Log::export(const char* t)
{
	file = fopen(t,"wt");
	exported=1;
	begin();
}
void Log::closeExport()
{
	if (exported)
	{
		fclose(file);
		exported=0;
	}
}
void Log::begin()
{
	enabled=1;
	for (int i=0;i<5;i++)
	{
		switch (c)
		{
		case LDATE: text+="[Date]"; break;
		case LTIME: text+="[Time]"; break;
		case LACTION: text+="[Action]"; break;
		case LSUBJECT: text+="[Subject]"; break;
		case LRETURN: text+="[Return]";
		}
		if (i+1!=5)
			text+='\t';
	}
	last = text;
	fputs(last.c_str(),file);
}

// Test
int main(int argc, char* argv[])
{
	log.export("log.text");
	//Window win;
	//win.create();
	//win.run();
	return 0;
}
log.text is empty. Why? Ok, and another question: How can I make a function that will define an other function? like in GLUT. so I can do: void a() {puts("Lala");} void b() /* or { }? */; void main () {function(a); b(); // same as a()} Thanks, pex.

Share this post


Link to post
Share on other sites
Advertisement
a) Your code doesn't even compile. Provide a full example

b) In C and C++, you can't.

What you can do, however, is to keep a function pointer around.

int foo(int i) { return i+1;}

void (*bar)(int);

bar = &foo;

bar(10); // calls foo(10)


Be warned, however, that pointers to non-static member functions are very different entities than pointers to free functions or to static member functions. You cannot assign one to the other.

struct Foo
{
int foo;
int Bar(int i) { return i + foo; }
};

Foo f;
Foo* pf = f;
f.foo = 10;
int (Foo::* ptr)(int);
ptr = &Foo::Bar;

(f.*ptr)(25); // calls f.Bar(25)
(pf->*ptr)(50); // falls pf->Bar(50)



[/source]

Share this post


Link to post
Share on other sites
a) I can't understand it. now it doesn't work, but when i compressed everything to 1 file it works as it should.
so i guess i cant compress it:
console.cpp:
// console.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include "c:\\pex\\tribox\\stdafx.h"
//#include <stdio.h>
#pragma comment(lib,"c:\\pex\\tribox\\release\\tribox.lib")

int main(int argc, char* argv[])
{
log.export("log.text");
//Window win;
//win.create();
//win.run();
return 0;
}


log.h:

#ifndef LOG_H
#define LOG_H
#pragma once

#include "stdafx.h"

class Log
{
public:
Log() {for (int i=0;i<5;i++) c=i; enabled=exported=0;}
~Log() {closeExport(); delete file;}
void add(int);
void begin();
// *not used code snipped*
void export(const char*);
void closeExport();
private:
string text,last;
FILE* file;
int enabled;
int exported;
int c[5];
};
static Log log;
enum LogColumns
{
LDATE,LTIME,LACTION,LSUBJECT,LRETURN
};
#endif

log.cpp:

#include "stdafx.h"
#include "log.h"

#ifndef LOG_CPP
#define LOG_CPP

void Log::export(const char* t)
{
file = fopen(t,"wt");
exported=1;
begin();
}
void Log::closeExport()
{
if (exported)
{
fclose(file);
exported=0;
}
}
void Log::begin()
{
enabled=1;
for (int i=0;i<5;i++)
{
switch (c)
{
case LDATE: text+="[Date]"; break;
case LTIME: text+="[Time]"; break;
case LACTION: text+="[Action]"; break;
case LSUBJECT: text+="[Subject]"; break;
case LRETURN: text+="[Return]";
}
if (i+1!=5)
text+='\t';
}
last = text;
fputs(last.c_str(),file);
}
void Log::add(int t)
{
if (!enabled)
return;
last='\t';
last+=t+'0';
text+=last;
fputs(last.c_str(),file);
}
#endif

I hope there is nothing missing here.

b) Yes, thats what I meant! thanks! [smile]

pex.

Share this post


Link to post
Share on other sites
In log.h, you have static Log log;. It doesn't mean what I think you think it means. If you do that, each CPP file ("translation unit") that #include "log.h" will have its own copy of the log variable.

You don't need #include guards for a cpp file.

Also, export is a reserved C++ keyword (although very few compilers actually implemented it).

After I tweaked the code a bit, the log header actually did appear in "log.text". Did you expect something else?

What did you mean by "but when i compressed everything to 1 file" ?

Share this post


Link to post
Share on other sites
he prolly means when he put all the .h & .cpp in a single .cpp file (wich explains why it fix his static keyword problem since there is only one filescope anyway in that case)

Share this post


Link to post
Share on other sites
I removed the 'static' keyword, but it still doesn't work.
Anything else?

EDIT: Now I get a warning that the second definition of class Log log is ignored. So I should bring back the static, no?

thx,
pex.

[Edited by - pex22 on January 7, 2005 11:44:59 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by pex22
EDIT: Now I get a warning that the second definition of class Log log is ignored. So I should bring back the static, no?


No, you declare it extern in the header and provide a definition in a single .cpp file.

As I said earlier, it works for me (the log header does appear). What happens on your system?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!