Need help with a Work around

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.
 
D

Davlet Panech

[...]
01 #include <iostream>
02 #include <fstream>
03 #include <string>
04 #include <io.h>
05 #include <cstring>
06 using namespace std;
07 enum Triangles

This is a syntax error. I assume you pasted your code without looking.
08 void main()

int main ()
09 {
10 ifstream inData;
11 const char* filename = "filename.dat";

string filename;
cout << "Please Enter The Data File You Would Like To Use: ";
cin >> filename;
12 if ((_access(filename,0))!=-1)

if ((_access(filename.c_str (),0))!=-1)
13 {
14 cout << filename << " Exists\n";
15 }
16 else
17 {
18 cout << filename << " Does Not Exist\n";

Add (after "cout..."):

return 1;
19 }
20 inData.open(filename);

inData.open(filename.c_str ());
if (!inData)
{
cout << filename << " could not be opened" << endl;
return 1;
}

Also, add "return 0;" at the end of "main()" (hint: "main()" should return 0
if your program finished without errors).

Another thing, you don't really have to use the "_access()" function. You
can just try to open the file, and print an error message if you can't (one
of the reasons for this could be that the file does not exist).

D.
 
D

David White

kazack said:
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.

OS _dependent_.
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)
-------------------------------------------------------------------------- --

'filename' is an uninitialized pointer. It could be pointing anywhere, such
as into the middle of your operating system (if it's not protected).
Wherever it is pointing is where the entered filename would go. Except that
it can't go there because it is a pointer to _const_ char. That means that
wherever it is pointing cannot be changed (a very good thing if it's
pointing into your operating system, but that doesn't help you get your
filename). Try this:

string filename;
if(cin >> filename)
{
// process filename
}

You can use a char * if you want (as opposed to a const char *), but it has
to evaluate to an address where storage is available for the filename. It
can't be just a pointer that you haven't pointed at anything.

char filename[10];
cin.width(sizeof(filename)); // ensure no overflow

if(cin >> filename)
{
// process filename
}

Even though 'filename' is an array, it evaluates to a char * that points to
the first character of the array as far as "cin >> filename" is concerned.

Of these two, the string version is better.

DW
 
K

kazack

Oh my god. I was looking at your code and said I tried that and it didn't
work I kept getting this error:
----------------------------------------------------------------------------
----------------------------------------
error C2664: 'void __thiscall std::basic_ifstream<char,struct
std::char_traits<char> >::eek:pen(const char *,int)' : cannot convert parameter
1 from 'const char *(void) const' to 'const char *'
There is no context in which this conversion is possible
----------------------------------------------------------------------------
----------------------------------------
And now I know why. The reason is with the .c_str I didn't have the ()
after it to make it look like this filename.c_str() So I did have the right
concept but just a syntax thing going on!!! Thank you so much.

The reason for the _access is because everythign I have been reading it
tells me you can only check to see if the file can be opened, if it can not
be opened it can not be determined what the specific problem is. I don't
want to say file don''t exist when it actualy does and have someone
overwrite data in a file where the file may be saveable. So I wanted to
make sure the file was or was not there before giving the user other options
like create a new file, select another file name just in case they typed in
the wrong file name, etc.

But thank you so much for your generous reply. If interested when I am done
with with my overboard for this problem I will post the code in full!!! I
don't think the code will be too bad for someone learning c++ and in it only
about a month!! The book has 18 chapters and I am going to after this going
intochapter 11. Which I am not looking forward to which is the use of
structure types, Data Abstraction and classes. I think I could actually
skip that whole chapter and leave it for if I were to work on someone elses
code that uses it. I can do the same thing without them, my code would
prolly be a bit more indepth and longer than using them but atleast it would
actually make sense when you look at the code!!! I don't know I'll see when
I start reading the material and actually see code in use and study it to
learn what is going on.

But anyways thanks alot for the help, I can't believe I actually had it
right when I tried doing it myself but had the wrong syntax. Oh well I
guess these things happen to the best of us!!!!

Shawn Mulligan

Davlet Panech said:
[...]

01 #include <iostream>
02 #include <fstream>
03 #include <string>
04 #include <io.h>
05 #include <cstring>
06 using namespace std;
07 enum Triangles

This is a syntax error. I assume you pasted your code without looking.
08 void main()

int main ()
09 {
10 ifstream inData;
11 const char* filename = "filename.dat";

string filename;
cout << "Please Enter The Data File You Would Like To Use: ";
cin >> filename;
12 if ((_access(filename,0))!=-1)

if ((_access(filename.c_str (),0))!=-1)
13 {
14 cout << filename << " Exists\n";
15 }
16 else
17 {
18 cout << filename << " Does Not Exist\n";

Add (after "cout..."):

return 1;
19 }
20 inData.open(filename);

inData.open(filename.c_str ());
if (!inData)
{
cout << filename << " could not be opened" << endl;
return 1;
}

Also, add "return 0;" at the end of "main()" (hint: "main()" should return 0
if your program finished without errors).

Another thing, you don't really have to use the "_access()" function. You
can just try to open the file, and print an error message if you can't (one
of the reasons for this could be that the file does not exist).

D.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top