473,657 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with a Work around

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.d at";
12 if ((_access(filen ame,0))!=-1)
13 {
14 cout << filename << " Exists\n";
15 }
16 else
17 {
18 cout << filename << " Does Not Exist\n";
19 }
20 inData.open(fil ename);

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.
Jul 19 '05 #1
4 2744
kazack <ka****@talon.n et> wrote in message
news:33******** ************@ne ws1.epix.net...
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.d at";
12 if ((_access(filen ame,0))!=-1)
13 {
14 cout << filename << " Exists\n";
15 }
16 else
17 {
18 cout << filename << " Does Not Exist\n";
19 }
20 inData.open(fil ename);

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.


'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(sizeo f(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

Jul 19 '05 #2
>[...]

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.d at";
string filename;
cout << "Please Enter The Data File You Would Like To Use: ";
cin >> filename;
12 if ((_access(filen ame,0))!=-1)
if ((_access(filen ame.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(fil ename);


inData.open(fil ename.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.
Jul 19 '05 #3
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_ifst ream<char,struc t
std::char_trait s<char> >::open(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" <sp@mmers.rot-in-hell> wrote in message
news:cP******** ***********@new s20.bellglobal. com...
[...]

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.d at";


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


if ((_access(filen ame.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(fil ename);


inData.open(fil ename.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.

Jul 19 '05 #4

"kazack" <ka****@talon.n et> wrote in message
news:AF******** ************@ne ws1.epix.net...
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_ifst ream<char,struc t
std::char_trait s<char> >::open(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.
No problem.

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.


Just so that you know: many environments (all that I used anyway, including
VisualC++, which is what you seem to be using) set the errno value to the
status of the "open" operation on a stream. ("errno" is a global integer
variable that contains the error code of the most recent operation that
failed). You can use that to display an *appropriate* error message without
hard-coding any text of your own:

#include <cerrno>
// ...
ifstream inData;
// ...
ifstream.open (filename.c_str ());
if (!inData)
{
int err = errno;
// Print out the reason for failure
cerr << filename << ": " << strerror (err) << endl;
return 1;
}
// ...

D.
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
4091
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can easily understand a newbie using bound controls or someone with a tight deadline. I guess I need...
3
4336
by: Steve | last post by:
i pulled an example off the web and modified it somewhat and whats killimg me is that sometimes it works and sometimes it doesnt. the following is the only line that allows this thing to work over and over, ive tried untold number of combinations , some have been left in as comments all will work right at the start but fail for no apparent reason later 'strsql = "insert into (orderinfofkey, lstring, link) values (1,'test', 'test2')" ...
48
3223
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and "huge" Indy is both as a project, in support, development, and use. But Indy is new to the .net world. Indy is a HUGE library implementing over 120 internet protocols and standards and comes with complete source. Its an open source project, but not...
7
2356
by: Jack Addington | last post by:
I've got a fairly simple application implementation that over time is going to get a lot bigger. I'm really trying to implement it in a way that will facilitate the growth. I am first writing a WinForms interface and then need to port that to a web app. I am kinda stuck on a design issue and need some suggestions / direction. Basically I have a business layer that I want to use to process any dataentry logic (row focus changes, data...
9
2382
by: Mickey Segal | last post by:
The long-simmering Eolas patent dispute: http://www.microsoft.com/presspass/press/2003/oct03/10-06EOLASPR.mspx has led to an optional Microsoft Update: http://support.microsoft.com/kb/912945/en-us that creates non-JavaScript problems that can be fixed using JavaScript. With the Microsoft update installed, Java applets (as well as other content such as Flash videos) are unable to receive user input until an activating click or key press....
12
1680
by: johannblake | last post by:
First off, I am NOT a beginner. I have lots of experience developing professional web sites and am a professional software developer. Unfortunately I've been out of web site development for the past 2 years but would like to get back into it. What I need is some advice to get me up-to-date on what is the best (or one of the best) ways of developing web sites and web applications. The following are my requirements: * Static web pages...
1
3217
by: james.ii.turner | last post by:
I understand why I am getting this error, I just need to find a work around for it. Here is what I'm trying to do: There is a textbox on my form that OnClick will ask the user for a new value, I want to take this value and update the textbox with this value. I can't use the recordsource option on this textbox because the Form ControlSource is constantly changing. Here is the code I'm using: Dim MyResponse As String Dim MyNewVal As...
92
4345
by: Ray | last post by:
I just moved to another company that's mainly a Java/.NET shop. I was happy to find out that there's a movement from the grassroot to try to convince the boss to use a dynamic language for our development! Two of the senior developers, however, are already rooting for Ruby on Rails--although they haven't tried RoR themselves. When I suggested Django, they went like, "what's that?". I said, "It's like the Python counterpart of RoR".
20
4256
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site is structured as an upside-down tree, and (if I remember correctly) never more than 4 levels. The site basically grew (like the creeping black blob) ... all the pages were created in Notepad over the last
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.