I am trying to design this code that will allow me to input a file, but it not be previously "hardwired" into the code, the user needs to input it, and I have almost figured it out, but i keep getting this error saying cannot convert from char to const char. If anyone can help me with this problem it would be appreciated.
#include <iostream> // For cin, cout, etc.
#include <fstream> // For File input, output, etc.
#include <iomanip> // For setprecision and setw
#include <string> // Allows for conversion to C String type data
using namespace std;
void scanner(); // Void Function for spam scanner
int main ()
{
char x;
cout<< "MAIN: Please enter the name of the filter file:";
cin>> x;
ifstream fin(x);
7 2342 arne 315
Expert 100+
I am trying to design this code that will allow me to input a file, but it not be previously "hardwired" into the code, the user needs to input it, and I have almost figured it out, but i keep getting this error saying cannot convert from char to const char. If anyone can help me with this problem it would be appreciated.
#include <iostream> // For cin, cout, etc.
#include <fstream> // For File input, output, etc.
#include <iomanip> // For setprecision and setw
#include <string> // Allows for conversion to C String type data
using namespace std;
void scanner(); // Void Function for spam scanner
int main ()
{
char x;
cout<< "MAIN: Please enter the name of the filter file:";
cin>> x;
ifstream fin(x);
The problem is that the ifstream constructor expects a "const char *", not a "char". Try: -
int main ()
-
{
-
string name;
-
-
cout<< "MAIN: Please enter the name of the filter file:";
-
cin >> name;
-
-
ifstream fin( name.c_str() );
-
-
...
-
Thanks that helped me through something I've been trying to figure out for 2 days now. Ive come to another bump in my code, but this one is a little bit different, I've got my void function now but I'm trying to make the name that I typed in for the file in the main function, I be able to call it into my void function.
#include <iostream> // For cin, cout, etc.
#include <fstream> // For File input, output, etc.
#include <iomanip> // For setprecision and setw
#include <string> // Allows for conversion to C String type data
using namespace std;
void scanner(); // Void Function for spam scanner
int main ()
{
string name1;
string name2;
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
scanner(); //Void Function that contains spam scanner
return 0;
}
void scanner () //Spam scanner
{
string name1; //Declaring all needed variables
string name2;
string spam1;
string spam2;
string spam3;
ifstream fin1(name1.c_str() ); //Reopening Files
ifstream fin2(name2.c_str() );
fin1>> spam1; //Inputing spam word(s)/phrase(s) into stream
fin1>> spam2;
fin1>> spam3;
cout<< "SCANFILE FUNCTION: Searching "<< name2<< " for the word "<< spam1<<"..."<< endl; //Search Mail Data for 1st group of spam data
Thanks that helped me through something I've been trying to figure out for 2 days now. Ive come to another bump in my code, but this one is a little bit different, I've got my void function now but I'm trying to make the name that I typed in for the file in the main function, I be able to call it into my void function. - #include <iostream> // For cin, cout, etc.
-
#include <fstream> // For File input, output, etc.
-
#include <iomanip> // For setprecision and setw
-
#include <string> // Allows for conversion to C String type data
-
using namespace std;
-
-
void scanner(); // Void Function for spam scanner
-
-
int main ()
-
{
-
string name1;
-
string name2;
-
-
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
-
-
scanner(); //Void Function that contains spam scanner
-
-
return 0;
-
-
}
-
-
void scanner () //Spam scanner
-
{
-
string name1; //Declaring all needed variables
-
string name2;
-
string spam1;
-
string spam2;
-
string spam3;
-
-
ifstream fin1(name1.c_str() ); //Reopening Files
-
ifstream fin2(name2.c_str() );
-
-
fin1>> spam1; //Inputing spam word(s)/phrase(s) into stream
-
fin1>> spam2;
-
fin1>> spam3;
-
-
cout<< "SCANFILE FUNCTION: Searching "<< name2<< " for the word "<< spam1<<"..."<< endl; //Search Mail Data for 1st group of spam data
Also, my spam1 which is supposed to be a word coming from the file didnt show up either, could this be happening because of my first problem there with name2, so if name2 doesn't show up then its not actually calling that file and without the file then there would be no word to get?
arne 315
Expert 100+
Thanks that helped me through something I've been trying to figure out for 2 days now. Ive come to another bump in my code, but this one is a little bit different, I've got my void function now but I'm trying to make the name that I typed in for the file in the main function, I be able to call it into my void function.
You mean you want to open the file and access its data in the scanner() functions?
One possibility would be to ask for the file names in the scanner() function, i.e. move the code from main() to scanner().
You could also ask for the names in main() and pass them to scanner(), like so -
void scanner( string a, string b ) {
-
-
ifstream fin1( a.c_str() );
-
ifstream fin2( b.c_str() );
-
-
...
-
}
-
This would be called in main like this
You could also pass the fstreams, i.e. open the files in main() and only pass the streams. But you may start with option 1 or 2 if they fit your needs.
arne 315
Expert 100+
Also, my spam1 which is supposed to be a word coming from the file didnt show up either, could this be happening because of my first problem there with name2, so if name2 doesn't show up then its not actually calling that file and without the file then there would be no word to get?
To read data from your file consider the following code snippet: -
string tmp_str;
-
while( getline( fin1, tmp_str ) ) {
-
-
// you have one line tmp_str now ...
-
}
-
I took your advice and got this far, but now when i try to input name1 and name2 in the void function-functions, it shows up syntax error C2660 :"scanner1": does not take 2 arguments - #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;
-
-
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
-
-
scanner1(name1, name2);//Void Function that contains spam scanner
-
scanner2(name1, name2);
-
scanner3(name1, name2);
-
-
return 0;
-
-
}
-
-
void scanner1 (string name1, string name2) //Spam scanner
-
{
-
ifstream fin1(name1.c_str() ); //Reopening Files
-
ifstream fin2(name2.c_str() );
-
-
string spam1; //Declaring all needed variables
-
-
fin1>> spam1; //Inputing spam word(s)/phrase(s) into stream
-
-
cout<< "SCANFILE FUNCTION: Searching "<< name2<< " for the word "<< spam1<< "..."<< endl; //Search Mail Data for 1st group of spam data
-
}
-
-
void scanner2 (string name1, string name2)
-
{
-
ifstream fin1(name1.c_str() ); //Reopening Files
-
ifstream fin2(name2.c_str() );
-
-
string spam2; //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 name1, string name2)
-
{
-
ifstream fin1(name1.c_str() ); //Reopening Files
-
ifstream fin2(name2.c_str() );
-
-
string spam3; //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
-
}
arne 315
Expert 100+
I took your advice and got this far, but now when i try to input name1 and name2 in the void function-functions, it shows up syntax error C2660 :"scanner1": does not take 2 arguments
Please make your declaration -
void scanner1(); // Void Functions for spam scanner
-
match your definition -
void scanner1 (string name1, string name2) //Spam scanner
-
{
-
...
-
}
-
like so: -
void scanner1( string, string );
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Gregg |
last post by:
Hello all,
I have been banging my head over a problem that I am having reading a
comma seperated file (CSV) that can contain from 1 to 10,000 records.
My code snipit is as follows:
**Start...
|
by: Adam J. Schaff |
last post by:
I am writing a quick program to edit a binary file that contains file paths
(amongst other things). If I look at the files in notepad, they look like:
...
|
by: Brian Henry |
last post by:
first question... I have a flat file which unfortinuatly has columns
seperated by nulls instead of spaces (a higher up company created it this
way for us) is there anyway to do a readline with this...
|
by: Gregg |
last post by:
Hello all,
I have been banging my head over a problem that I am having reading a
comma seperated file (CSV) that can contain from 1 to 10,000 records.
My code snipit is as follows:
**Start...
|
by: laredotornado |
last post by:
Hi,
I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed
using Apache's apxs and the php library was installed to
/usr/local/php. However, when I set my "error_reporting"...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |