load file with specified name from command line

This is a discussion on load file with specified name from command line within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm writing a class which has the function that can load a file, it's like this: Code: A::A(){} void ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    34

    load file with specified name from command line

    Hi,

    I'm writing a class which has the function that can load a file, it's like this:

    Code:
    A::A(){}
    void A::loadFile()
    {
        ifstream fs;
        fs.open("file.txt");
        //....other stuff......
    }
    as you can see it only loads this specific file.
    Now how can I modify it so that it can load a file with the name specified from the command line.

    Many thanks!!!

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Put char array where the "file.txt" is, but make sure you get the users input before then

    Code:
    char* filename = new char[20];
    ifstream file;
    
    cout<<"Enter file name: ";
    cin>> filename;
    
    file.open(filename);
    delete [] filename; //delete it once done!

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    34
    Thanks my friend!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Although if you actually want to use the command line (e.g. type in "myprog somefile.txt"), then that's not the right way to do it.

    Command line arguments are passed to main as int argc (the number of arguments, starting with the program name itself, in this case we'd get argc == 2) and char *argv[] (the actual arguments: argv[0] == "myprog" or something similar, argv[1] == "somefile.txt").

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    34
    Thank you too Mats. That's very helpful.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by scwizzo View Post
    Put char array where the "file.txt" is, but make sure you get the users input before then

    Code:
    char* filename = new char[20];
    ifstream file;
    
    cout<<"Enter file name: ";
    cin>> filename;
    
    file.open(filename);
    delete [] filename; //delete it once done!
    This could just have been written as
    Code:
    std::string filename;
    std::ifstream file;
    
    std::cout<<"Enter file name: ";
    std::cin>> filename;
    
    file.open( filename.c_str() );
    Far better code.
    You do not require dynamic memory to do something simple as this.
    Last edited by Elysia; 11-01-2008 at 05:06 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Do c++ style strings take up less memory than c style?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Or perhaps not as efficient now that I think about it, but certainly more user friendly.
    And if you wanted to be even more efficient than your code, then a C-style array would be used. Which is not a good thing in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by scwizzo View Post
    Do c++ style strings take up less memory than c style?
    No. C++ style strings probably use a bit more memory (it does depend on the implementation - it could be implemented as a class C string if you wanted to, but most likely is that they do use a bit more memory by storing the length of the string as a size_t or similar type, which makes it likely 3 bytes longer than the minimal size of a C style string - but unless this is for a watch or calculator, I wouldn't think that saving 3 bytes is meaningfull - even mobile phones have megabytes of memory these days).

    And I doubt it executes faster either. But it's definitely easier to write and less error-prone, and that would be a distinct benefit when you are waiting for the user to type something - it would be different if this was part of the pixel-calculation inside a ray-tracing loop - then trying to save a few clock-cycles by using less good coding techniques would be worth it. But this example is something that happens once, and it's dependant on the user typing sosmtehing in, so it will take at least a few tenths of a second (fast typer) to several seconds if not minutes (slow typer or user doesn't pay attention). Saving 1 microsecond out of (say) 1 second is not really worth it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Good to know. I don't know a whole lot about the efficiency of predefined data types, but it's always good to know the difference between c and c++ besides the actual usage.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by scwizzo View Post
    Good to know. I don't know a whole lot about the efficiency of predefined data types, but it's always good to know the difference between c and c++ besides the actual usage.
    But the much more important part here is to know the difference between where it makes sense to save a few hundred clockcycles and where it doesn't make sense.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by matsp View Post
    No. C++ style strings probably use a bit more memory (it does depend on the implementation - it could be implemented as a class C string if you wanted to, but most likely is that they do use a bit more memory by storing the length of the string as a size_t or similar type, which makes it likely 3 bytes longer than the minimal size of a C style string - but unless this is for a watch or calculator, I wouldn't think that saving 3 bytes is meaningfull - even mobile phones have megabytes of memory these days).

    And I doubt it executes faster either. But it's definitely easier to write and less error-prone, and that would be a distinct benefit when you are waiting for the user to type something - it would be different if this was part of the pixel-calculation inside a ray-tracing loop - then trying to save a few clock-cycles by using less good coding techniques would be worth it. But this example is something that happens once, and it's dependant on the user typing sosmtehing in, so it will take at least a few tenths of a second (fast typer) to several seconds if not minutes (slow typer or user doesn't pay attention). Saving 1 microsecond out of (say) 1 second is not really worth it.

    --
    Mats
    Oh, but if it stores the size, then it might indeed be faster, since there is no need to search for the "end" of a string like C-style functions do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Oh, but if it stores the size, then it might indeed be faster, since there is no need to search for the "end" of a string like C-style functions do.
    Yes, but only in cases such as strcat and strlen where the length itself actually matters (and if you want to do some sort of substring or similar operations), but for most intents and purposes it is actually very little efficiency difference. A string copy still requires copying of all characters, a search for letter X will still require reading all characters (up to the point where it's either found or the end of the string), etc, etc. There are situations when C strings are inefficient - particularly using strcat a lot (but we can overcome that by tracking the end of the string in the code, if it's really important - although that would require writing our own bit of code to do that).

    I'd say generally, there's very little difference. The big benefit of C++ strings is that they are simpler to use for many things - particularly things like substrings and concatenation - and of course support variable length much better than C strings.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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