Could use some help, please.

Hi,

First let me say thank you in advance to anybody who is willing to take the time to help me out.

I am a VB6 guy from way back trying to update my programming skills. I thought I would try C++. I'm working with Visual C++ 2008.

It's been going pretty well but I have run into a difficulty. It's probably a very simple thing for experienced programmers but C++ is great divergence from VB6 and I'm a stuck.

My goal is to output a simple log file. I thought this would work best via a source (cpp) file as opposed to rewriting it for every form. I have created the .h header file and the .cpp file but can't seem to make it work. I've rewritten it 50 times at this point and google searched and dug through my book but can't seem to figure it out.

I will throw some code out here, it's probably as scrambled as my brain at this point so please consider it a starting point.

Here is my CPPLog.cpp source file: (what do I actually need to #include and where and how do I need to innitialize the string?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdafx.h"
#include "CPPLog.h"
#include <iostream>
#include <string>
using namespace System::IO;
using namespace System;
 
String ^ sLog;
 
void pLogWriter(String ^ sLog){
        StreamWriter^ LogFile = gcnew StreamWriter("LogFile.txt");
        LogFile->WriteLine(sLog);
        LogFile->Close();
        } 



Here is my CPPLog.h header file: (I get an error saying invalid use of void. Plus I read on some web site you can simply use the keyword "string" in the declaration but don't know if this is correct.)

1
2
3
4
5
6
#ifndef CPPLOG_H
#define CPPLOG_H
 
void pLogWriter(string);
 
#endif  



Here is my main form:

1
2
3
#include "CPPLog.h"
 
pLogWriter("Test"); 


Thanks again for your help, I greatly appreciate it.
Last edited on
I hope you are aware that you are using C++/CLI which is quite different from normal C++.
Sorry, I'm new to the forum. I thought this area was for beginners. I will figure out where to post this question. Thanks for letting me know.

Or am I misunderstanding your remark?

Is the syntax I'm using is wrong?

OKay, I'm slow, I admit it, but not hopeless. I really didn't realize there was a difference in the syntax between cli and windows forms and didn't realize it would make a difference in the source and header files. That would explain why I have been getting confused reading different things.
Last edited on
Hiya,
yea like Peter said, that's not c++. For your log file you might want to check out this link:
http://www.cplusplus.com/doc/tutorial/files/

and refer back to this:
http://www.cplusplus.com/doc/tutorial/

when you need to familiarise yourself with the syntax.
Thanks mutexe, I will check those links out.
Last edited on
CPPLog.h
1
2
3
4
5
6
7
#ifndef CPPLOG_H
#define CPPLOG_H
 
// void pLogWriter(string);
void pLogWriter( System::String^ ); // ^
 
#endif  



CPPLog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "stdafx.h"
#include "CPPLog.h"

using namespace System::IO;
using namespace System;
 
// String ^ sLog; // *** no ***
 
void pLogWriter( System::String^ sLog ) {

        System::IO::StreamWriter^ LogFile = 
            gcnew System::IO::StreamWriter( "LogFile.txt", true ); // true: if the file exists, append 
        try { LogFile->WriteLine(sLog); } // alas, there is no RAII. so, a try-finally construct
        finally { LogFile->Close(); }
}
Thank you JLB, I greatly appreciate the help!

Can't wait to give this a try!
Awesome, thank you again!
Topic archived. No new replies allowed.