GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Feb-2004, 21:29
cameron cameron is offline
New Member
 
Join Date: Feb 2004
Posts: 11
cameron is on a distinguished road

compilation error


im using microsoft visual C++ 6.0 to compile my c++ programs.
I have never done any C++ programming before. I'm a java programmer instead. However, I'm on attachment and was given C++ work to do.

May I know how do you solve this?
These are part of my codes:
CPP / C++ / C Code:
#include <fstream.h>
#include <iostream.h>
std::ofstream testFile("example.txt", ios::app);
testFile<<"hello";
testFile.close();
these are the errors i got after compiling:

error C2143: syntax error : missing ';' before ':'
error C2065: 'testFile' : undeclared identifier
error C2297: '<<' : illegal, right operand has type 'char [6]'
error C2224: left of '.close' must have struct/union type

if i do not use namespace, they cant identify ofstream. I need help urgently.
Last edited by dsmith : 19-Feb-2004 at 21:53. Reason: Added C syntax highlighting
  #2  
Old 19-Feb-2004, 21:42
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
You need a main() function. The program will not execute without one.

CPP / C++ / C Code:
#include <fstream.h>
#include <iostream.h>

void main()
{
  std::ofstream testFile("example",ios::app);
  testFile << "hello";
  testFile.close();
}
  #3  
Old 19-Feb-2004, 22:46
cameron cameron is offline
New Member
 
Join Date: Feb 2004
Posts: 11
cameron is on a distinguished road
these codes are part of a main method...
something like
int main(---)
{
//other codes
//my codes
//other codes
return 0;
}
  #4  
Old 19-Feb-2004, 22:48
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Could you please provide the code?
  #5  
Old 19-Feb-2004, 22:51
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Cameron. I don't know if it will help or not, but let me point you to this thread here. I give a sample of basic file i/o using C++.
  #6  
Old 19-Feb-2004, 23:17
cameron cameron is offline
New Member
 
Join Date: Feb 2004
Posts: 11
cameron is on a distinguished road
u sure u want the full codes?
it's really really long....
actually im testing the ofstream thing as i'm actually doing motion tracking and i need to capture some measurements into a file.
so i'm juz testing to see whether the file i/o works or not.
this is the main method
CPP / C++ / C Code:
#include <fstream.h>
#include <iostream.h>
#include <stdio.h> 
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "birdnet.h"
#include "sockport.h"

#ifdef _WIN32
#include <windows.h> 
HANDLE hStdout, hStdin; 
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
#else

#endif


#define TRUE		1	/* true value		*/
#define FALSE		0	/* false value		*/



#define TCP_PORT	6000	// moitonstar tcp port
#define UDP_PORT	5000	// motionstar udp port





/* table to convert format to data format byte */
/* required in setup */
short rtbl[16] = {0x00,0x31,0x32,0x93,0x64,0xc5,0,0x47,0x78,0,0,0,0,0,-1,-1};

char *formattxt[16] = { "No Data",
						"Position",
						"Angles",
						"Matrix",
						"Position/Angle",
						"Position/Matrix",
						"No Data",
						"Quaternion",
						"Position/Quaternion",
						"No Data",
						"No Data",
						"No Data",
						"No Data",
						"No Data",
						"No Data",
						"No Data"};
	
	extern int errno;
	
	/* server socket address */
	struct sockaddr_in client,server,broadcast;
	
	int 	TCPflag = TRUE;		/* TCP or UDP */
	int 	birdVerbose;		// message verbosity level
	short 	sequence = 0;	
	
	float  posScale;		/* scaling factor for positon data */
	/* = inches returned from birdnet / 32767; */
	#define angScale 180./32767.
	
	/* save stack space by making global */
	struct BN_PACKET 
	{
		struct BN_HEADER header;
		unsigned char data[2048];			/* undifferentiated packet data */
	};
	
	struct BN_PACKET response;
	
	char 	inbuf[80];
	int 	broadcastFlag = FALSE;
	
	
	
	int get_status_fbb(int cmdSocket,int addr,struct BN_BIRD_STATUS **bird);
	int set_status_fbb(int cmdSocket,int addr,struct BN_BIRD_STATUS *bird);
	int run_continuous(int cmdSocket,int dataSocket);
	int send_wakeup(int cmdSocket);
	int send_shutdown(int cmdSocket);
	int single_shot(int cmdSocket,int dataSocket);
	int get_status_all(int cmdSocket,struct BN_SYSTEM_STATUS **sys);
	int set_status_all(int cmdSocket,struct BN_SYSTEM_STATUS *sys);
	int tcpSocket_open(char *ipaddr);
	int udpSocket_open(char *ipaddr);
	int broadcastSocket_open(char *ipaddr);
	int parse_data(struct BN_PACKET *pkt);
	int send_command(int cmdSocket,int cmd,int xtype);
	int send_status(int cmdSocket,int xtype,char *data,int size);
	int get_response(int cmdSocket,int rsp);
	void parse_position(char *data);
	void parse_angles(char *data);
	void parse_matrix(char *data);
	void parse_quaternion(char *data);
	void parse_serial(char *data, int n);
	void print_header(struct BN_HEADER *header);
	void print_help(void);
	int chkkbhit(void);
	void clear_screen(void);
	void set_cursorxy(int x, int y);
	
	int main(int argc, char *argv[])
	{
		int		birdFormat = BN_POSANG;
		int 	birdsRequired = BN_MAX_ADDR;      
		float	birdRate = 86.1;
		int 	singleShots = 0;
		int		reportRate = 1;
		int 	i;
		char	ip_address[16] = "192.9.200.51";
		char	broadcast_address[16] = "192.9.200.255";
		char 	buf[10];
		int 	nDevices;
		struct 	BN_SYSTEM_STATUS *sys;
		struct 	BN_BIRD_STATUS *bird;
		char 	*cptr;
		int		cmdSocket,broadcastSocket;
		int 	receiveOnly = FALSE;
		int		on = 1;
		
		
		#ifdef _WIN32
		WORD wVersionRequested;
		WSADATA wsaData;
		int err;
		
		wVersionRequested = MAKEWORD( 2, 0 ); 
		err = WSAStartup( wVersionRequested, &wsaData );
		if ( err != 0 ) 
		{
			/* Tell the user that we couldn't find a usable */
			/* WinSock DLL.                                  */
			return(-1);
		}
		/* Confirm that the WinSock DLL supports 2.0.*/
		/* Note that if the DLL supports versions greater    */
		/* than 2.0 in addition to 2.0, it will still return */
		/* 2.0 in wVersion since that is the version we      */
		/* requested.                                        */
		
		if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 )
		{
			/* Tell the user that we couldn't find a usable */
			/* WinSock DLL.                                  */
			WSACleanup( );
			return(-1); 
		}
		
		/* The WinSock DLL is acceptable. Proceed. */
		
		hStdin = GetStdHandle(STD_INPUT_HANDLE); 
		hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
		if (hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE) 
		printf("GetStdHandle"); 
		
		
		/* Save the current text colors. */ 
		
		if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) 
		printf("GetConsoleScreenBufferInfo"); 	
		
		#else
		
		#endif 
		if ((cptr = getenv("BN_IPADDR")) != NULL)
		strcpy(ip_address,cptr);
		if ((cptr = getenv("BN_PROTOCOL")) != NULL)
		if (strcmp(cptr,"UDP") == 0)
		TCPflag = FALSE;
		if ((cptr = getenv("BN_VERBOSE")) != NULL)
		birdVerbose = atoi(cptr);
		if ((cptr = getenv("BN_RATE")) != NULL)
		birdRate = atof(cptr);
		if ((cptr = getenv("BN_DATA")) != NULL)
		birdFormat = atoi(cptr);
		
		/* process command line switches */
		for(i=1; i < argc ; i++)
		{
			cptr = *++argv;
			if (*cptr++ != '-')
			{
				printf("Bad Command Line Option - %c\n",*--cptr);
				print_help();
				exit(-1);
			}
			switch (*cptr++) 
			{
				case 'h':
				return(0);
				case 'i':
				strcpy(ip_address,cptr);
				break;
				case 'p':
				if (strcmp(cptr,"UDP") == 0)
				TCPflag = FALSE;
				break;
				case 'B':
				// Broadcast Receive only
				receiveOnly = TRUE;
				// need to make assumption about scaling
				posScale = 144./32767.;
				case 'b':
				broadcastFlag = TRUE;
				TCPflag = FALSE;  //if broadcast=>UDP
				strcpy(broadcast_address,cptr);
				break;
				case 'v':
				birdVerbose = atoi(cptr);
				break;
				case 'm':
				birdRate = atof(cptr);
				break;
				case 'r':
				reportRate = atoi(cptr);
				break;
				case 'f':
				birdFormat = atoi(cptr);
				break;
				case 'n':
				birdsRequired = atoi(cptr);
				break;
				case 's':
				singleShots = atoi(cptr);
				if(singleShots<0) singleShots = 0;
				printf("\nSINGLE SHOTS REQUESTED = %d", singleShots);
				break;
				default:
				printf("Bad Command Line Option - %c\n",*--cptr);
				print_help();
				exit(-1);
				break;
			}
		}
		// bounds check user input
		if(birdRate>144) birdRate=144;
		if(birdRate<20) birdRate=20;
		if (reportRate > 255) reportRate = 255;
		if (reportRate < 1) reportRate = 1;
		if (birdFormat < 0) birdFormat = 0;
		if (birdFormat > 15) birdFormat = 15;
		
		
		// Print out program settings
		
		/*************************************/
		/*************My Codes****************/
		std::ofstream testFile("example.txt", ios:app);
		testFile<<"hello";
		testFile.close();

		printf("IP ADDRESS of SERVER = %s\n", ip_address);
		if (TCPflag)
		printf("TCPIP Protocol\n");
		else
		printf("UDP Protocol\n");
		printf("BIRD RATE = %f\n", birdRate);
		printf("REPORT RATE = %d\n", reportRate);
		
		printf("BIRD FORMAT = %s\n", formattxt[birdFormat]);
		printf("NUMBER OF BIRDS = %d\n\n", birdsRequired);
		
		if (TCPflag)
		cmdSocket = tcpSocket_open(ip_address);
		else
		cmdSocket = udpSocket_open(ip_address);
		//	cmdSocket = socket_open(ip_address); 
		if (!cmdSocket)
		//	if (!socket_open(ip_address,broadcast_address))
		{
			print_help();  // try to be useful
			return(-1);
		}
		if (broadcastFlag)
		{
			printf("BROADCAST IP = %s\n",broadcast_address);
			broadcastSocket = broadcastSocket_open(broadcast_address);
			if (!broadcastSocket)
			//	if (!socket_open(ip_address,broadcast_address))
			{
				print_help();  // try to be useful
				return(-1);
			}
			else
			{
				i = setsockopt(broadcastSocket, SOL_SOCKET, SO_BROADCAST, (void *)&on, sizeof(on));
				if(i<0)
				{
					perror("Broadcast");
					exit(-1);
				}
			}
		}
		
		
		// if receive only then just loop getting and parsing data
		if (receiveOnly)
		{
			while(TRUE)
			{
				if (!get_response(broadcastSocket,DATA_PACKET))
				return(FALSE);
				parse_data(&response);
			}
		}
		
		/* send the wake up call */
		if (!send_wakeup(cmdSocket))
		return(-1);
		
		/* get the system status */
		if (!get_status_all(cmdSocket,&sys))
		return(-1);
		
		/* set measurement rate requested */
		
		sprintf(buf,"%06.0f",birdRate * 1000.);
		strncpy(sys->rate,buf,6);
		
		printf("Chassis# = %d, \n", sys->chassisID);
		printf("Number of devices in this chassis = %d, \n", sys->chassisDevices);
		printf("First flock address in this chassis = %d, \n", sys->firstAddress);
		printf("SERVER Software Version = %d.%d\n",sys->softRev[0],sys->softRev[1]);
		printf("\n\n");
		
		nDevices = sys->chassisDevices;  
		
		/* send the system setup */
		if (!set_status_all(cmdSocket,sys))
		return(-1);
		
		
		
		/* for 1 to n flock boards 			*/
		/* get the individual bird status 		*/
		/* modify the appropriate contents 		*/
		/* send the individual bird status packet back 	*/
		
		for(i = 2; i <= nDevices; i++) 
		{
			/* get the status of an individual bird */
			get_status_fbb(cmdSocket,i,&bird);
			
			/* change the data format to something new for all birds */
			if(i <= birdsRequired+1)
			bird->header.dataFormat = rtbl[birdFormat];
			else
			bird->header.dataFormat = 0;         /* no data */
			
			/* set new report rate for all birds */
			bird->header.reportRate = reportRate;
			bird->header.setup |= BN_FLOCK_APPENDBUT;
			
			posScale = (float)ntohs(bird->header.scaling) / 32767.;
			
			/* set the status of an individual bird */
			if (!set_status_fbb(cmdSocket,i,bird))
			return(-1);
		}
		/* get data from birds */
		
		if(singleShots==0)
		{
			if (broadcastFlag)
			run_continuous(cmdSocket,broadcastSocket);
			else
			run_continuous(cmdSocket,cmdSocket);
		}
		else
		{
			for(i = 0; i < singleShots; i++)
			if (broadcastFlag)
			single_shot(cmdSocket,broadcastSocket);
			else
			single_shot(cmdSocket,cmdSocket);
		}		
		send_shutdown(cmdSocket);
		#ifdef WIN32
		WSACleanup( );
		#endif
		return(0);
	}
Last edited by Garth Farley : 20-Feb-2004 at 04:31. Reason: Added C++ syntax highlighting
  #7  
Old 19-Feb-2004, 23:46
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Please enclose all code in the [c] or [c++] tags. It makes code WORLDS easier to read. The file I/O code looks fine to me, and you have the right includes. Did the code compile correctly before you added the file I/O stuff?
  #8  
Old 19-Feb-2004, 23:56
cameron cameron is offline
New Member
 
Join Date: Feb 2004
Posts: 11
cameron is on a distinguished road
oh im sorry...this is my first time here...

yep it compiled correctly....
has it got something to do with the compiler?
  #9  
Old 20-Feb-2004, 00:04
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
I spotted the likely cause of the problem. In your declaration, you typed exactly this:

std:fstream testFile("example.txt",ios:app");

You went wrong at the append modifier. it should be ios::app, not ios:app. Try it now!
  #10  
Old 20-Feb-2004, 00:09
cameron cameron is offline
New Member
 
Join Date: Feb 2004
Posts: 11
cameron is on a distinguished road
oops...my mistake..thanks for pointing it out...

hm....it still has the same errors...
this is madness~!!
don't you think so too??
arghhh
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in ukrspp21 MySQL / PHP Forum 31 04-Jul-2006 21:41
Including Maps and strings?? maddie C++ Forum 17 05-Jul-2004 06:25
error during program rjd72285 C++ Forum 0 11-Nov-2003 18:49
[script] E-mail webmaster error page BobbyDouglas PHP Code Library 0 19-Aug-2003 20:10
CD burner, focus or tracking error ShingoDrrazz Computer Hardware Forum 1 09-Aug-2003 16:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:35.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.