File Input Error Help

K

kazack

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

enum Triangles{Scalene,Isosceles,Equalateral,Invalid,Hold};
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
(e-mail address removed)
 
J

Jon Bell

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

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