Thread: path

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    32

    path

    Hi evryone
    I using MVC++ and built my project using Win32 app.
    My program do not have any interfaces. it reads files and it put the output in another file. ( I do not mind put interface only to enter the path)

    the thing is I made my program read the input file from specific directory and put the output in the specify directory.
    the probelm now is that I would like the user to inter the dirctory of the input file and the output file is this possible if yes, please tell me how to do it, I do not have any idea.

    Code:
    "C:\\Wadi\\In\\Log_01.csv" my in file directory
    "C:\\Wadi\\Out\\Daily_Summary_%d.csv" my out file directory
    now I would like to enable the user to enter his path for the input file and the output file.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) Learn to read input from the user and store it in a variable. Start by reading in a number and then try reading in a word and finally a whole sentence.

    2) When you accomplish (1), ask the user for a full path/filename and read it into a variable of the appropriate type. The variable type you need can be determined by checking the required argument types for opening a file.

    3) Instead of opening your file using a string literal(e.g. "C:\\Folder1\\file1.txt"), use the variable containing the user input.
    Last edited by 7stud; 01-27-2006 at 04:20 AM.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    32
    Hi and thaks for your reply, I think I did not get the full idea of your post, can you clarify it please.

    thank you

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) Can you write a simple program that asks the user to input a number and then your program displays the number?

    2) Can you write a simple program that asks the user to input a word, and then your program displays the word?

    3) Can you write a simple program that asks the user to input a sentence, and then your program displays the sentence?
    Last edited by 7stud; 01-27-2006 at 05:37 AM.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    32
    thank you I learned new thing from you today.
    the probelm now is that I would like the user to inter the dirctory of the input file and the output file is this possible if yes, please tell me how to do it, I do not have any idea.
    part of my program is

    Code:
     if ( foo(-1, "C:\\Wadi\\In\\Log_%d.csv", filename, sizeof filename) ) //this function to bring file according the day number.
    	 { 
           ifstream input_file(filename);  // the I opened that file
    	    if(!input_file.is_open())
    		{
    	       return 1;
    		}
            else// do calculation
    
    	 {.............}
          and have 
      
    if( strcmp( filename, "C:\\Wadi\\In\\Log_01.csv" ) == 0 ) // to check if the input file ( day 1) to open new out put file
      {
        sprintf (buffer, "C:\\Wadi\\Out\\Daily_Summary_%d.csv", month); // new out put file for the new month
    	ofstream outfile(buffer, ios::out | ios::app);
    
          outfile << sums[i]/count<<"," << max[i]<<","<< min[i]<<",";
    
    ......}
    any way all this is not a problem( I do not use Ms-dos window ) I stopped that window
    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    I do not needed because I reading from a file and get the result in the out file.

    by the way I run my program daily to make a summary report, I made that using Window xp task scheduler.

    again my probelm is that I am going to give my project to some people to run it in their computer , so they would like to spacify the path of the input file and the outfile themself.

    so as I do not have any idea how to do that, I seeked your help.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    just do normal console reads and writes using iostream cin and cout functions.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main()
    {
      string filename;
      cout << "Enter a path and filename";
      getline(cin,filename);
      //
      ifstream in(filename.c_str());
      // blabla
    }
    Last edited by Ancient Dragon; 01-27-2006 at 07:08 AM.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Why can't you read the full path name that the user enters into a variable, and use that variable to open the input file?

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    32
    Quote Originally Posted by Ancient Dragon
    just do normal console reads and writes using iostream cin and cout functions.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main()
    {
      string filename;
      cout << "Enter a path and filename";
      getline(cin,filename);
      //
      ifstream in(filename.c_str());
      // blabla
    }
    if I am going to use this way, thats mean every time window xp scheduler task runthe project.exe the user have to enter the path and I do not want something like this, I am not sure if I express myself well. I would like the user to enter the path only one time then the computer will remeber this path and work every day without asking for the path again.

    thank you for all the help

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    That's a simple matter of only asking the user for the filename containing the input data after you try to open a certain file for reading, e.g. filename.txt. If filename.txt can't be opened for reading because it doesn't exist, then it's the first time the program has run, so then you ask the user for the filename containing the input data and then write that file name to filename.txt. The next time the program is run, when you try to open filename.txt, the file will be found, so you should skip the prompt for user input, and filename.txt will be opened to retrieve the filename of the file containing the input data.
    Last edited by 7stud; 01-27-2006 at 08:02 AM.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    32
    I am sure you are angery from me keep asking. sorry for that.

    I know it is an easy thing to ask the user to enter the whole path and the file name. but
    thats mean every time window xp scheduler task run the project.exe the user have to enter the path and I do not want something like this.
    I would like the user to enter the path only one time then the computer will remeber this path and work every day without asking for the path again.

    my project is going to put in a computer and make window xp task scheduler run it once a day, it will read different input file every day( day1.txt,.....) according to the day of the current date. but all the file ( day1.txt.....) are kept the same path ( which the user have to give his path to the project when He run it for the first time, then it will be left to work according to the schedular and the path he told it previously. so it will run every day by itself leaving it......
    Last edited by yes; 01-27-2006 at 08:19 AM.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I would like the user to enter the path only one time then the computer will remeber this path and work every day without asking for the path again.
    That is the question I answered in my last post. Are you aware that in C++ there is a different result when you try to open a file for reading that doesn't exist than when you try to open a file for reading that does exist? If you know that, then you should ask yourself, "how can I use that to my advantage?" The files with the input data exist at all times, so there is never a point where they might not exist, so they are of no help. However, the path name is not known the first time the program is run, but it is known every time the program is run after that. So, what if the first time the program is run, you write the path name to a new file? Your program can check whether that new file exists or not. If the new file does exist, then you can choose not to ask the user for the path name. If the new file doesn't exist, then that means it is the first time the program has been run, so you can prompt the user for the path name. Then, create a new file and write the path name to the file.
    Last edited by 7stud; 01-27-2006 at 06:15 PM.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I would like the user to enter the path only one time then the computer will remeber this path and work every day without asking for the path again.
    There are several ways to do that -- the most common is to save the filename in the registry or a file with a known filename, such as your program's *.ini file. When the program starts, if the file that contains the filename does not exist, the ask the user to enter the filename and save it in the ini file, registry of somewhere else. If the file that contains the filename (or registry etc) DOES exist, just read the filename and use it.

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    32
    Hi Ancient dragon
    I would like to use the Idea of creating .ini file, but to be hounest this is the first time I am going to use it. so if you have any link I can learn from it how to create .ini file, that would help me so much.

    1. from what I read over the internet it in a txt file saves as .ini
    2. structure of this file is
    path.ini file name
    structure
    [inpath]c:\wadi\In\logos.csv[inpath]
    [outpath]c:\wadi\out\summary.csv[outpath]

    3. reading that file with stream is it the same
    Code:
         ifstream input_file(filename);  
    	 if(!input_file.is_open())
    	 {
    	   return 1;
    	 }
         else
    	 {
    how to get the path from that file
    
    }
    thank you so much if you can clarify these points and any link I would benefit from.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Shortest Path Maze Solver (Breadth Search Help)
    By Raskalnikov in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 07:41 PM
  3. clipping path
    By stanlvw in forum Windows Programming
    Replies: 0
    Last Post: 07-23-2008, 11:47 PM
  4. Path Finding Using Adjacency List.
    By Geolingo in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2005, 02:34 PM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM