Hello folks my fstream system appears to have failed!

it all worked until i split the file up into *.H and *.CPP.

// header file

#include <string>
#include <fstream>

#ifndef SCANNER_H
#define SCANNER_H


void openfile(void);

bool check(string token);
bool testing(char sym);
string getsym(void);

#endif

the .cpp file:

string getsym(void)
{
	myfile.open("hello.txt");
	bool ReserveWord= false;
	bool space = false;
	string r_token, ksym, test;
	string id = "ID";
	char next_sym;

	if(myfile.is_open())
	{
		next_sym = myfile.get();
		space=testing(next_sym);
		if(space)
		{
			next_sym = myfile.get();
			space = testing(next_sym);
			while(space)
			{
				next_sym = myfile.get(); 
				space = testing(next_sym);
			}
		}

... whilst thats not complete i can only assume its myfile.open(). However if i put that as a global or create a function which opends an iostream to a file, it fails

I've tried the following:

void openmyfile(void)
{
myfile.open("hello.txt");
}

and then called my getsym function, but it doesnt work.... can anyone offer any help?

heres main:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

#include "scanner.h"



string sym;


int main()
{
	sym = getsym();
	cout << sym;


return 0;
}

Recommended Answers

All 2 Replies

You should post your errors. It makes it much easier to figure out what's going on.

Your .cpp file needs to #include your .h file.

If you have a global variable in one file, to make it visible in another file, you have to declare it as extern.

int variable;  // original file
extern int variable;  // another file that can now access variable

there was no errors jiust logical ones since the file wasn't outputing to the screen

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.