473,396 Members | 2,013 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,396 software developers and data experts.

File Input Error Help

#include <iostream>
#include <fstream>
#include <string>
#include <io.h>
#include <cstring>
using namespace std;

enum Triangles{Scalene,Isosceles,Equalateral,Invalid,Ho ld};
Triangles Entered = Hold;
void NotValid(int, int, int);
void Equal(int,int,int);
void Isos(int,int,int);
void triangle(int,int,int,int);

void main()
{
ifstream inData;
int sidea;
int sideb;
int sidec;

char* filename;
// I have 2 options here char* filename = "c:\filename";
// or const char* filename ="c:\filename";
//either one will work when checking for file existance.
// but can not use it with a cin statement below.

cout << "Please Enter The Data File: ";
cin >> filename;
//the above line does not work I have tried numerous
//things and can not figure out how to get it to work.
//I do not want the filename hardcoded into the
//program!!


//The bit of code below works only if you use the
//char* filename. I have tried creating another variable
// and using strcpy and that does not work it keeps
//coming up with can not convert blah blah blah.

if((_access(filename,0))!=-1)
{
cout << filename << " Exists\n";
}
else
{
cout << filename << " Does Not Exist\n";
}
inData.open(filename);
I am new to c++ and it is more difficult than VB. If someone can help me
out with a solution that does what I want I would be very greatful.

My object is to:
1. Ask the user for the filename.
2. check for existance
3.If the file does not exist let the user create it or let
them use another data file.

I was thinking as an alternative checking for file existance using a system
call for a wildcard dat file and read them into an array and let the user
select the file or create a new one. But I thought the option above I am
trying to use would be more efficient and easier.

Thank You,
Shawn Mulligan
ka****@talon.net

Jul 19 '05 #1
1 2285
In article <ZO********************@news1.epix.net>,
kazack <ka****@talon.net> wrote:

[snip]
void main()
Obligatory comp.lang.c++ admonishment: The C++ standard requires that
main return int, not void.

[snip]
char* filename;
This allocates memory for a pointer which is presumably intended to point
to the beginning of an array of characters. However, it does *not*
allocate memory for the characters themselves. This pointer is not
initialized to any particular value, and therefore points to some random
location in memory.

[snip]
cin >> filename;
Here you are entering a sequence of characters and trying to store them
beginning at the random memory location mentioned above, likely one which
you do not have privilege to access. Kaboom.

[snip]
inData.open(filename);


Since you are using #include <string>, you have available to you the
standard C++ string data type. Use it! Then you don't have to worry
about allocating memory to put the characters into; strings handle their
own memory allocation.

string filename;

cin >> filename;

inData.open (filename.c_str());

You need the extra business in the open statment because it expects a
C-style "string"; c_str() gives you a pointer to a C-style string that
contains a copy of the contents of the real string.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #2

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

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
1
by: jase_dukerider | last post by:
Hi I have an assignment to hand in shortly for which I am after some guidance. The task is to read a WAV file, request a fade in /out time for the track from the user and the do the fade by...
5
by: Codeman II | last post by:
Hi there, I am building a form where the user must upload a picture and fill in his details. Now I have a problem as all of this is on the same form. How will I be able to have the Browse...
18
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't...
4
by: pank7 | last post by:
hi everyone, I have a program here to test the file IO(actually output) with buffer turned on and off. What I want to see is that there will be obvious differece in time. Here I have an input...
2
by: slacker | last post by:
By clicking a link on a website I set a USERINFO cookie and then I redirect to a program which ultimately executes an XSL file which dynamically builds an input form. The input form, while having...
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"...
1
by: chrisj | last post by:
I'm using freeASPupload and got some assistance integrating to a Member script. It works successfully. In this modified version there are two groups that use this upload script. Members of one...
4
cassbiz
by: cassbiz | last post by:
Could use some help here. This script is carrying over an image just fine but the text isn't coming over. can you see why it is not working???? from the form I want to carry over two lines of...
6
Jacotheron
by: Jacotheron | last post by:
I need a PHP script that can upload music files (mp3). The script is for a home project I have started a while ago. I have a MySQL database of all the music that I have. Other computers on the...
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: 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...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.