Thread: Make user directory a variable?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    Make user directory a variable?

    Im trying to make it so that the user can enter the directory of a file and that this file will be read, the problem is i can seem to get the program to use a variable as the directory, this is roughly what i want to happen, with the users directory as 'direc'


    Code:
     do
    {
    	cout<<"Please Enter the Directory to your Binary File e.g. c:/Documents/file.dat:\n";
    	cin>>direc;
    	cout<<"The Directory you entered was "<<direc<<" is this correct? (y or n)\n";
    	cin>>answer;
    }
    	while (answer==n) 
    
    	
    	ifstream file(direc, ios::in|ios::binary);
    So is this possible? any hints?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So is this possible?
    Certainly.

    >any hints?
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string path;
    
      std::cout<<"Enter the path of your file: ";
    
      if ( getline ( std::cin, path ) ) {
        std::ifstream in ( path.c_str(), std::ios::binary );
    
        if ( in ) {
          char ch;
    
          while ( in.get ( ch ) )
            std::cout<< int ( ch ) <<'\n';
        }
      }
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    It's been called many things, but... "Directory"?

    On a side note, your input method is certainly annoying. I don't often get asked for "confirmation" when I try to open a file through some dialog...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. copying file from one user directory to another
    By Bargi in forum Linux Programming
    Replies: 6
    Last Post: 05-17-2009, 04:09 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. How do I make it wait for user input
    By earth_angel in forum Windows Programming
    Replies: 1
    Last Post: 06-29-2005, 01:34 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM