File Open

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xiaolim
    New Member
    • Jul 2008
    • 18

    File Open

    hi there, if i want to prompt user to locate the file and open, how would i do it?
    for example:

    #include <iostream>
    #include <fstream>
    using namespace std;

    int main () {
    ofstream myfile;
    myfile.open ("example.txt") ;
    myfile << "Writing this to a file.\n";
    myfile.close();
    return 0;
    }

    if i want to prompt user to open "example.tx t" not open within the code, how would i code it so that when this programe is run, the 1st thing will ask for user to select file location and open, then continue the process such as writing "Writing this to a file". thanks
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    If u want to do this with browser window then you shuld try to use VC++ MFC/COM

    Raghu

    Comment

    • xiaolim
      New Member
      • Jul 2008
      • 18

      #3
      nope, i dont want to do it with browser, just want to construct a simple .cpp then run. any idea?

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        xiaolim, have you learned about std::string and std::cin (and input in general). What you would do is ask the user (through std::cout), take in the response to a string with std::cin, and then use the string when specifying which file to open.

        Comment

        • arnaudk
          Contributor
          • Sep 2007
          • 425

          #5
          Or you could do it by supplying the filename as a command-line argument:
          [code=cpp]
          int main(int argc, char *argv[])
          {
          if (argc == 2)
          { // One command line argument was supplied: argv[1] is
          // a string containing the first command line argument
          std::cout << "Output will be written to: " << argv[1] << std::endl;
          }
          else
          { // error: no arguments/too many arguments
          std::cerr << "Usage: " << argv[0] << " filename." << std::endl;
          return EXIT_FAILURE;
          }

          // etc...
          }
          [/code]

          Comment

          • xiaolim
            New Member
            • Jul 2008
            • 18

            #6
            hmmm. i did learned about it, but i am still very lost, but thanks, i will read up more on it.. i am sorry, very new to this.

            hi arnaudk, i dont really get it, i did learn but i am very new to it. may i have more example ? such as a working simple example will do a great help, thanks. ^^ sorry for the troble

            Comment

            • arnaudk
              Contributor
              • Sep 2007
              • 425

              #7
              You can almost compile the code I gave you, just #include <iostream> and provide a return value for main(). Use myfile.open (argv[1]); to open the file.
              As for the arguments of main(), argv is an array of (null-terminated) strings containing the command-line arguments you supplied when you execute the program, and argc is an integer which tells you the size of that array. Even if you don't supply any comand-line arguments, the size will still be 1 because the first element, argv[0], is the always name of your program. The best way to understand all this is to just compile a simple program and try it out, give it a shot and repost if you have trouble
              Check google for more info and many examples.

              Comment

              • xiaolim
                New Member
                • Jul 2008
                • 18

                #8
                hi, i've tried it. it seems like i still can't understand. could your explain a bit more detail?

                Comment

                • xiaolim
                  New Member
                  • Jul 2008
                  • 18

                  #9
                  oh.i got it already, have another question to ask
                  Code:
                  #include <iostream> 
                  #include <cstring>  
                  #include <fstream>
                  #include <conio.h>
                  
                  using namespace std;
                  
                  
                  int main(void)
                  {
                      int total=0;
                      int upper=0;
                      int lower=0;
                      int digits=0;
                      int blanks=0;
                      int eosp=0;
                      int others=0;
                      
                      char buf[80];
                      ifstream fin;
                      string fname;
                      
                      cout << "Welcome\n" << endl;
                      cout << "This programe will analyze the file content &" << endl;
                      cout << "compute the statistics of the file you input.\n\n\n\n\n" << endl;
                      system("pause");
                      system("cls");
                      
                      do 
                         {
                          cout << "Enter input data file name:\n";
                          cin >> fname;     // Get the file name.
                          fin.open(fname.c_str());  // Convert to C-string and open it.
                          if (!fin) 
                               {          // Will fail if didn't exist.
                                  cout << "Unable to open " << filename << endl;
                                  cin.get();
                                  system("cls");
                               } 
                          } while(!fin);
                          
                        
                      cout << "Total number of characters: " << total << endl;
                      cout << "Number of uppercase characters: " << upper << endl;
                      cout << "Number of lowercase characters: " << lower <<endl;
                      cout << "Number of decimal digits: " << digits << endl;
                      cout << "Number of blanks: " << blanks << endl;
                      cout << "Number of end-of-sentence punctuation marks: "<< eosp << endl;
                      cout << "Others: " << others << endl;
                      cout << "" << endl;
                  
                         
                         system("pause");
                         
                  return 0;
                  }
                  inside the my code, i wanted it to detect the uppercase, lowercase,digit s,blanks, punctuations and others such as spaces. I've declare all the variables and assign them to initial 0.so after the file is open, the program should be able to read all the lines in the file and detects and then display them out as accordingly. any guide or help? an example will be more then enough. thank you very much

                  Comment

                  • arnaudk
                    Contributor
                    • Sep 2007
                    • 425

                    #10
                    It sounds like you'll want to read the file character by character. For this, check out
                    istream::read. To see if a character is upper/lower case, number, etc have a look at cctype.

                    As for accepting command line arguments, did you really try to understand it yourself with google? Compile this simple program as example.exe:
                    [code=cpp]
                    #include <iostream>

                    int main(int argc, char* argv[])
                    {
                    for(int i = 0; i < argc; ++i)
                    {
                    std::cout << "Command-line argument " << i << " is: " << argv[i] << std::endl;
                    }
                    return 0;
                    }
                    [/code]
                    Then run it:
                    [code=text]
                    C:\>example.exe apple pear banana
                    Command-line argument 0 is: example.exe
                    Command-line argument 1 is: apple
                    Command-line argument 2 is: pear
                    Command-line argument 3 is: banana
                    [/code]
                    As you can see, the arguments you supply to example.exe are stored in the array argv.

                    Comment

                    • xiaolim
                      New Member
                      • Jul 2008
                      • 18

                      #11
                      yes, is alright already, thanks for the help from you guys, i figured it out, thanks alot. ^^^

                      Comment

                      Working...