K
kazack
I posted a similiar question in this newsgroup already and got an answer
which I already knew but didn't get the answer I was looking for so I am
reposting the code and question differently in the hope that someone could
help me out. As said in an earlier post I am new to c++, this is alot
harder to do than VB. I am for the most part self taught with what I
already know and looking to learn more. The book I am using to teach myself
from is called: Programming and Problem Solving with C++ Third Edition by
Nell Dale, Chip Weens and Mark Headington.
Some of the items that I have had problems with I was able to go into some
old c books I have and was able to understand the concepts easier that way,
but in this case I have no clue.
The problem was a very easy problem. I got the problem finished with no
problem, but to teach myself I have to push the limits and make it do more
than what the problems say. And in this case I do not want the filename
hardcoded into the program, I want the user to enter the filename of what
they want to use, and check to see if the file exists which is what lines 12
through 20 does. I have read the web in several different places and have
been told that there is no standard way to do this, that this is OS
independant. I understand this, but I am not asking how you check for file
existance I am asking for help wiht a minor modification to the below code
to get working what I need accomplished. Any and all comments and
suggestions would be appreciative.
01 #include <iostream>
02 #include <fstream>
03 #include <string>
04 #include <io.h>
05 #include <cstring>
06 using namespace std;
07 enum Triangles
08 void main()
09 {
10 ifstream inData;
11 const char* filename = "filename.dat";
12 if ((_access(filename,0))!=-1)
13 {
14 cout << filename << " Exists\n";
15 }
16 else
17 {
18 cout << filename << " Does Not Exist\n";
19 }
20 inData.open(filename);
This code works fine but what I want to do with it is change line 11 to
define nothing, just have it declared. Then in between lines 11 and 12
prompt the user for a filename and store it in filename. With the way the
code is now if you just add in a cin statement it comes up with an error the
following error:
----------------------------------------------------------------------------
-----------------------------------------------
This error happens if I change line 11 to:
const char* filename;
add In between Line 11 and 12:
cout << "Please Enter The Data File You Would Like To Use: ";
cin >> filename;
error C2679: binary '>>' : no operator defined which takes a right-hand
operand of type 'const char *' (or there is no acceptable conversion)
----------------------------------------------------------------------------
-------------------------------------------------
Is there a way to accomplish this using the bove code? I really need or
would like to keep lines 12 through 19.
I was thinking about going another way around this but would rather use the
above instead of using completely new code.
The new idea is to:
1. prompt the user for filename
2. use a system call to do a directory search for that filename and print
the directory scan to a file.
3. open the new temp file with directory results in it
4. search the temp file for the file the user entered when they were
prompted.
5. delete temp file
If there is no work around for the code above, What are comments on the new
idea?, or other suggestions to accomplish what I need.
which I already knew but didn't get the answer I was looking for so I am
reposting the code and question differently in the hope that someone could
help me out. As said in an earlier post I am new to c++, this is alot
harder to do than VB. I am for the most part self taught with what I
already know and looking to learn more. The book I am using to teach myself
from is called: Programming and Problem Solving with C++ Third Edition by
Nell Dale, Chip Weens and Mark Headington.
Some of the items that I have had problems with I was able to go into some
old c books I have and was able to understand the concepts easier that way,
but in this case I have no clue.
The problem was a very easy problem. I got the problem finished with no
problem, but to teach myself I have to push the limits and make it do more
than what the problems say. And in this case I do not want the filename
hardcoded into the program, I want the user to enter the filename of what
they want to use, and check to see if the file exists which is what lines 12
through 20 does. I have read the web in several different places and have
been told that there is no standard way to do this, that this is OS
independant. I understand this, but I am not asking how you check for file
existance I am asking for help wiht a minor modification to the below code
to get working what I need accomplished. Any and all comments and
suggestions would be appreciative.
01 #include <iostream>
02 #include <fstream>
03 #include <string>
04 #include <io.h>
05 #include <cstring>
06 using namespace std;
07 enum Triangles
08 void main()
09 {
10 ifstream inData;
11 const char* filename = "filename.dat";
12 if ((_access(filename,0))!=-1)
13 {
14 cout << filename << " Exists\n";
15 }
16 else
17 {
18 cout << filename << " Does Not Exist\n";
19 }
20 inData.open(filename);
This code works fine but what I want to do with it is change line 11 to
define nothing, just have it declared. Then in between lines 11 and 12
prompt the user for a filename and store it in filename. With the way the
code is now if you just add in a cin statement it comes up with an error the
following error:
----------------------------------------------------------------------------
-----------------------------------------------
This error happens if I change line 11 to:
const char* filename;
add In between Line 11 and 12:
cout << "Please Enter The Data File You Would Like To Use: ";
cin >> filename;
error C2679: binary '>>' : no operator defined which takes a right-hand
operand of type 'const char *' (or there is no acceptable conversion)
----------------------------------------------------------------------------
-------------------------------------------------
Is there a way to accomplish this using the bove code? I really need or
would like to keep lines 12 through 19.
I was thinking about going another way around this but would rather use the
above instead of using completely new code.
The new idea is to:
1. prompt the user for filename
2. use a system call to do a directory search for that filename and print
the directory scan to a file.
3. open the new temp file with directory results in it
4. search the temp file for the file the user entered when they were
prompted.
5. delete temp file
If there is no work around for the code above, What are comments on the new
idea?, or other suggestions to accomplish what I need.