1 Replies - 2225 Views - Last Post: 26 October 2006 - 01:11 AM Rate Topic: -----

#1 lostmountainman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-October 06

File Input/Void Function Problem

Posted 25 October 2006 - 08:12 PM

Heres the code i have so far, seems like i just keep doing circles when trying to make this work, but before i post the code the thing im trying to do is be able to call a file from the main function into my void function so i can run getline, find, and string functions on it. I have tried several ways but it doesnt seem to be working at all and i get different errors everytime.
#include <iostream> // For cin, cout, etc.
#include <fstream>  // For File input, output, etc.
#include <iomanip>  // For setprecision and setw
#include <string>   // For C++ string
using namespace std;

void scanner1(); // Void Functions for spam scanner
void scanner2();
void scanner3();

int main ()
{
	string name1;
	string name2;
	string spam1;
	string spam2;
	string spam3;

	cout<< "MAIN: Please enter the name of the filter file:"; //Calling File with Filter Data
	cin>> name1;
	cout<< endl;
	ifstream fin1(name1.c_str() );

	cout<< "MAIN: Please enter the name of the mail file:"; //Calling File with Mail Data
	cin>> name2;
	cout<< endl;
	ifstream fin2(name2.c_str() );

	cout<< "MAIN: About to call the scanfile function..."<< endl; //Prompt letting user know function is about to run

	fin1>> spam1;
	fin1>> spam2;
	fin1>> spam3;

	scanner1(string);//Void Functions that contains spam scanner
	scanner2(string);
	scanner3(string);

	return 0;

}

void scanner1 (string spam1) //Spam scanner
{
	ifstream fin2(name2.c_str() );

	cout<< "SCANFILE FUNCTION: Searching "<< name2<< " for the word "<< spam1<< "..."<< endl; //Search Mail Data for 1st group of spam data

	return;
}

void scanner2 (string name2, string spam2)
{
	string name1;

	ifstream fin1(name1.c_str() ); //Reopening Files
	ifstream fin2(name2.c_str() );
	 //Declaring all needed variables

	fin1.ignore(265, '/n'); //Skipping 1st line of spam word(s)/phrase(s) in the stream
	
	fin1>> spam2; //Inputing spam word(s)/phrase(s) into stream

	cout<< "SCANFILE FUNCTION: Seaching "<< name2<< " for the word "<< spam2<< "..."<< endl; //Search Mail Data for 2nd group of spam data
}

void scanner3 (string name2, string spam3)
{
	string name1;
	ifstream fin1(name1.c_str() ); //Reopening Files
	ifstream fin2(name2.c_str() );
 //Declaring all needed variables

	fin1.ignore(265, '/n'); //Skipping 1st line of spam word(s)/phrase(s) in the stream
	fin1.ignore(265, '/n'); //Skipping 2nd line of spam word(s)/phrase(s) in the stream

	fin1>> spam3; //Inputing spam word(s)/phrase(s) into stream

	cout<< "SCANFILE FUNCTION: Searching "<< name2<< " for the word "<< spam3<< "..."<< endl; //Search Mail Data for 3rd group of spam data
}


and here is what my output is supposed to resemble...if im asking for to much help just let me know then possibly give me some guidelines on how to fix my problem

MAIN: Please enter the name of the filter file:filter.txt



MAIN: Please enter the name of the mail file:mail.txt



MAIN: About to call the scanfile function...

SCANFILE FUNCTION: Searching mail.txt for the word offer...

Line # Position

1 -1

2 -1

3 -1

4 -1

5 7



SCANFILE FUNCTION: Searching mail.txt for the word mortgage...

Line # Position

1 -1

2 -1

3 -1

4 -1

5 -1



SCANFILE FUNCTION: Searching mail.txt for the word drugstore@spamsender.net...

Line # Position

1 6

2 -1

3 -1

4 -1

5 -1



Press any key to continue . . .

Is This A Good Question/Topic? 0
  • +

Replies To: File Input/Void Function Problem

#2 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: File Input/Void Function Problem

Posted 26 October 2006 - 01:11 AM

View Postlostmountainman, on 25 Oct, 2006 - 08:12 PM, said:

Heres the code i have so far, seems like i just keep doing circles when trying to make this work, but before i post the code the thing im trying to do is be able to call a file from the main function into my void function so i can run getline, find, and string functions on it. I have tried several ways but it doesnt seem to be working at all and i get different errors everytime.

and here is what my output is supposed to resemble...if im asking for to much help just let me know then possibly give me some guidelines on how to fix my problem.

The problem you are having is this:
You have misunderstood variable scoping...

Variables declared:
* outside main() are global (and dangerous).
* within the { and } braces of a code block (including nested braces) are local to that region.

so...
int main ()
{
	string name1;
	string name2;
	string spam1;
	string spam2;
	string spam3;
...
}
variables there are local to main... functions that are called have no access to them in the current code.

Your choices are:
  • pass the fin1 and fin2 variables
  • pass the variables by value
  • make them global
The preference would be to pass the finX as the files are already opened.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1