user specifies data file to open

This is a discussion on user specifies data file to open within the C++ Programming forums, part of the General Programming Boards category; I know how to open a file if the name is coded into the source code, but how so I ...

  1. #1
    Unregistered
    Guest

    user specifies data file to open

    I know how to open a file if the name is coded into the source code, but how so I use a user-entered file and path with

    ifstream datafile;
    datafile.open("x:\\file.dat");

    where x:\\file.dat is entered by the user of the program at runtime

    thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    ifstream takes a char * as the first argument for the open method. Have the user enter the filename into a char array and use that as the argument.
    Code:
    char array[BUFSIZ];
    cin.getline ( array, BUFSIZ );
    datafile.open ( array );
    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest

    char array as argument not working

    using a char array as the argument for ifstream is not working for me. the program never opens the file and it just hangs. is there a special way to format the text that I input while the program is running? I am entering the file name as I would in the source, but omitting the quotes ("). It works if I hard code the file name into the source.

  4. #4
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    Here's a small program...

    -----
    #include<iostream.h>
    #include<fstream.h>
    #include<stdio.h>
    #include<dir.h>
    void main()
    {
    char path[13],name[13]
    ofstream fout;

    cout<<"Enter path : ";
    gets(path);
    cout<<"Enter filename : ";
    gets(name);

    chdir(path);
    fout.open(name);
    fout<<"FILE HAS BEEN EDITED";
    fout.close();
    }
    -----
    hope this helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-31-2009, 12:53 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21