473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

File Input Problems

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);
Oct 23 '06 #1
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:
Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3.     string name;
  4.  
  5.     cout<< "MAIN: Please enter the name of the filter file:";
  6.     cin >> name;
  7.  
  8.     ifstream fin( name.c_str() );
  9.  
  10.         ...
  11.  
Oct 23 '06 #2
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
Oct 23 '06 #3
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.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> // For cin, cout, etc.
  2. #include <fstream>  // For File input, output, etc.
  3. #include <iomanip>  // For setprecision and setw
  4. #include <string>   // Allows for conversion to C String type data
  5. using namespace std;
  6.  
  7. void scanner(); // Void Function for spam scanner
  8.  
  9. int main ()
  10. {
  11.     string name1;
  12.     string name2;
  13.  
  14.     cout<< "MAIN: Please enter the name of the filter file:"; //Calling File with Filter Data
  15.     cin>> name1;
  16.     cout<< endl;
  17.     ifstream fin1(name1.c_str() );
  18.  
  19.     cout<< "MAIN: Please enter the name of the mail file:"; //Calling File with Mail Data
  20.     cin>> name2;
  21.     cout<< endl;
  22.     ifstream fin2(name2.c_str() );
  23.  
  24.     cout<< "MAIN: About to call the scanfile function..."<< endl; //Prompt letting user know function is about to run
  25.  
  26.     scanner(); //Void Function that contains spam scanner
  27.  
  28.     return 0;
  29.  
  30. }
  31.  
  32. void scanner () //Spam scanner
  33. {
  34.     string name1; //Declaring all needed variables
  35.     string name2;
  36.     string spam1;
  37.     string spam2;
  38.     string spam3;
  39.  
  40.     ifstream fin1(name1.c_str() ); //Reopening Files
  41.     ifstream fin2(name2.c_str() );
  42.  
  43.     fin1>> spam1; //Inputing spam word(s)/phrase(s) into stream
  44.     fin1>> spam2;
  45.     fin1>> spam3;
  46.  
  47.     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?
Oct 23 '06 #4
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
Expand|Select|Wrap|Line Numbers
  1. void scanner( string a, string b ) {
  2.  
  3.          ifstream fin1( a.c_str() );
  4.          ifstream fin2( b.c_str() );
  5.  
  6.          ...
  7. }
  8.  
This would be called in main like this
Expand|Select|Wrap|Line Numbers
  1. scanner( name1, name2 );
  2.  
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.
Oct 23 '06 #5
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:

Expand|Select|Wrap|Line Numbers
  1. string tmp_str;
  2. while( getline( fin1, tmp_str ) ) {
  3.  
  4.         // you have one line tmp_str now ...
  5. }
  6.  
Oct 23 '06 #6
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> // For cin, cout, etc.
  2. #include <fstream>  // For File input, output, etc.
  3. #include <iomanip>  // For setprecision and setw
  4. #include <string>   // For C++ string
  5. using namespace std;
  6.  
  7. void scanner1(); // Void Functions for spam scanner
  8. void scanner2();
  9. void scanner3();
  10.  
  11. int main ()
  12. {
  13.     string name1;
  14.     string name2;
  15.  
  16.     cout<< "MAIN: Please enter the name of the filter file:"; //Calling File with Filter Data
  17.     cin>> name1;
  18.     cout<< endl;
  19.     ifstream fin1(name1.c_str() );
  20.  
  21.     cout<< "MAIN: Please enter the name of the mail file:"; //Calling File with Mail Data
  22.     cin>> name2;
  23.     cout<< endl;
  24.     ifstream fin2(name2.c_str() );
  25.  
  26.     cout<< "MAIN: About to call the scanfile function..."<< endl; //Prompt letting user know function is about to run
  27.  
  28.     scanner1(name1, name2);//Void Function that contains spam scanner
  29.     scanner2(name1, name2);
  30.     scanner3(name1, name2);
  31.  
  32.     return 0;
  33.  
  34. }
  35.  
  36. void scanner1 (string name1, string name2) //Spam scanner
  37. {
  38.     ifstream fin1(name1.c_str() ); //Reopening Files
  39.     ifstream fin2(name2.c_str() );
  40.  
  41.     string spam1; //Declaring all needed variables
  42.  
  43.     fin1>> spam1; //Inputing spam word(s)/phrase(s) into stream
  44.  
  45.     cout<< "SCANFILE FUNCTION: Searching "<< name2<< " for the word "<< spam1<< "..."<< endl; //Search Mail Data for 1st group of spam data
  46. }
  47.  
  48. void scanner2 (string name1, string name2)
  49. {
  50.     ifstream fin1(name1.c_str() ); //Reopening Files
  51.     ifstream fin2(name2.c_str() );
  52.  
  53.     string spam2; //Declaring all needed variables
  54.  
  55.     fin1.ignore(265, '/n'); //Skipping 1st line of spam word(s)/phrase(s) in the stream
  56.  
  57.     fin1>> spam2; //Inputing spam word(s)/phrase(s) into stream
  58.  
  59.     cout<< "SCANFILE FUNCTION: Seaching "<< name2<< " for the word "<< spam2<< "..."<< endl; //Search Mail Data for 2nd group of spam data
  60. }
  61.  
  62. void scanner3 (string name1, string name2)
  63. {
  64.     ifstream fin1(name1.c_str() ); //Reopening Files
  65.     ifstream fin2(name2.c_str() );
  66.  
  67.     string spam3; //Declaring all needed variables
  68.  
  69.     fin1.ignore(265, '/n'); //Skipping 1st line of spam word(s)/phrase(s) in the stream
  70.     fin1.ignore(265, '/n'); //Skipping 2nd line of spam word(s)/phrase(s) in the stream
  71.  
  72.     fin1>> spam3; //Inputing spam word(s)/phrase(s) into stream
  73.  
  74.     cout<< "SCANFILE FUNCTION: Searching "<< name2<< " for the word "<< spam3<< "..."<< endl; //Search Mail Data for 3rd group of spam data
  75. }
Oct 23 '06 #7
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

Expand|Select|Wrap|Line Numbers
  1. void scanner1(); // Void Functions for spam scanner
  2.  
match your definition
Expand|Select|Wrap|Line Numbers
  1. void scanner1 (string name1, string name2) //Spam scanner
  2. {
  3.         ...
  4. }
  5.  
like so:
Expand|Select|Wrap|Line Numbers
  1. void scanner1( string, string );
  2.  
Oct 24 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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...
12
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: ...
12
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...
5
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...
1
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"...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
0
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
0
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...
0
marktang
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,...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.