Thread: Command Line Code

  1. #1
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Command Line Code

    Hi. I am trying to open and read a file in a certain way. This is what I have, which works:
    Code:
    #include<fstream>
    #include<iostream>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
      ifstream the_file;
      the_file.open(argv[1]);
      char x;
      the_file.get(x);
      while(x!=EOF)
      {
         cout<<x;
         the_file.get(x);
      }
       the_file.close();
       return 0;
    }
    Is anyone familiar with doing something similar with the following:
    ifstream in(string);
    where string is a C-style string (that is a character array whose last character is '\0') that specifies a filename. Note that the values of argv[i] are all C-style strings. ...To get a C-style string from a C++ string called s, you call s.c_str().
    I am not familiar with this. Thanks in advance. Steve

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Do you have a specific question? You're not really telling us what you don't understand.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I fail to see any difference, except the fact that you're opening the file in the constructor instead of in a second function call.

    C++ string:
    #include <string>
    std::string s;
    s.c_str() <-- Gets you the C-string version of the contents of s.

    What are you trying to ask anyway??

    **EDIT**
    If you're trying to ask how to open a file using a c++-style string:
    Code:
    std::string s = argv[0];
     
    std::ifstream file;
    file.open(s.c_str());
     
    or
     
    std::ifstream file(s.c_str());
    There's really no point in this example. It's a good example of bad code, since you have absolutely no need of storing argv[0] in a std::string, and will only waste CPU cycles and memory constructing the C++ string object, and then convert it right back into a C-string (a lesser version that you can't modify) to open the file, when you could have just opened the file with the original string (as your original code did).
    Last edited by Hunter2; 09-10-2004 at 05:11 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    When I open a file I use a function.

    Code:
    bool bLoadFile(char *file_name)
    {
    
        ifstream fin(file_name);
    
        // Stuff with file
    
        fin.close();
    
    }
    What is C++?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is anyone familiar with doing something similar with the following
    Yes.

    >the_file.open(argv[1]);
    Does argv[1] exist? Is the file successfully opened? Better check and be sure.

    >the_file.get(x);
    >while(x!=EOF)
    >{
    > cout<<x;
    > the_file.get(x);
    >}
    This can be compressed, and should be since not many people are fully versed in the various uses of get.

    >the_file.close();
    In this case there's no need. The destructor will handle closing the file for you.

    Here's how I would have done it (copious comments added for your benefit):
    Code:
    #include <cstdlib>
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main ( int argc, char *argv[] )
    {
      // Verify that there are at least two arguments
      if ( argc < 2 ) {
        cerr<<"usage: $ prog <filename>"<<endl;
        return EXIT_FAILURE;
      }
    
      // Open the second argument as a file
      ifstream in ( argv[1] );
    
      // Make sure it's actually a file :-)
      if ( !in.is_open() ) {
        cerr<<"Error opening file"<<endl;
        return EXIT_FAILURE;
      }
    
      char ch;
    
      // Notice how the return value of get is the condition
      while ( in.get ( ch ) )
        cout.put ( ch );
    
      // Flush the stream and make sure the
      // next program starts on a fresh line
      cout<<endl;
    
      return EXIT_SUCCESS;
    }
    My best code is written with the delete key.

  6. #6
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Clarification

    Thanks for the swift reply folks. I appreciate it. I'm sorry that I wasn't being as clear as I should have been. I meant to ask - I want to know how to accomplish this task using a C-style string. As I am not familiar with these, I needed some ideas. I will go ahead with what you all have suggested, and if I have further questions, I will post. Thanks again. Steve

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I want to know how to accomplish this task using a C-style string.
    argv[1] is a C-style string. A C-style string is simply an array where the last character is '\0', used to mark the end of the string. The only real issue you'll encounter at first is when using the C++ string class with a function that expects a C-style string. In that case you need to call the c_str member function to get the C-string representation of the C++ string:
    Code:
    #include <fstream>
    #include <string>
    
    std::string s ( "somefile" );
    std::ifstream in ( s.c_str() );
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM