File Input Error Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kazack

    File Input Error Help

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

    enum Triangles{Scale ne,Isosceles,Eq ualateral,Inval id,Hold};
    Triangles Entered = Hold;


    void NotValid(int, int, int);
    void Equal(int,int,i nt);
    void Isos(int,int,in t);
    void triangle(int,in t,int,int);

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

    char* filename;
    // I have 2 options here char* filename = "c:\filenam e";
    // or const char* filename ="c:\filenam e";
    //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(fil ename,0))!=-1)
    {
    cout << filename << " Exists\n";
    }
    else
    {
    cout << filename << " Does Not Exist\n";
    }
    inData.open(fil ename);


    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
    kazack@talon.ne t



  • Jon Bell

    #2
    Re: File Input Error Help

    In article <ZOinb.5980$Bv6 .1850150@news1. epix.net>,
    kazack <kazack@talon.n et> wrote:

    [snip]
    [color=blue]
    >void main()[/color]

    Obligatory comp.lang.c++ admonishment: The C++ standard requires that
    main return int, not void.

    [snip]
    [color=blue]
    > char* filename;[/color]

    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]
    [color=blue]
    > cin >> filename;[/color]

    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]
    [color=blue]
    > inData.open(fil ename);[/color]

    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 <jtbellap8@pres by.edu> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA

    Comment

    Working...