Register    Login    Forum    Search    FAQ

Board index » Programming » C++




 Page 1 of 1 [ 5 posts ] 



Author Message
 Post subject: working with argc and char*argv[]
 Post Posted: Wed Jan 30, 2013 10:46 pm 
Offline
n00b
n00b

Joined: Wed Jan 30, 2013 10:40 pm
Posts: 2
Text editor: Notepad++
I have what i think is a very simple question, as im just starting out with C++, does argv[] only work in main? if i build a class, can it use argv[]?

for example, i want to open a file and read in a list of ints, can i write that function in a class definition, or does it need to be in main and then pass a C-style string of the data to the class?


Top 
 Post subject: Re: working with argc and char*argv[]
 Post Posted: Thu Jan 31, 2013 8:43 am 
Offline
Hero of the Forum
Hero of the Forum
User avatar

Joined: Tue Nov 13, 2012 4:37 pm
Posts: 1023
Location: Serbia
Languages: C/C++ HTML CSS JavaScript PHP BasicsOf: Java Assembly SQL
Text editor: Sublime text
I think you can only use that in main's parameters, but your class function can use it if you make it avalable to it somehow.

_________________
Every problem has a solution.
.
Currently working on: RoyalWars


Top 
 Post subject: Re: working with argc and char*argv[]
 Post Posted: Thu Jan 31, 2013 11:36 am 
Offline
Mega-Power User
Mega-Power User
User avatar

Joined: Sun Jan 08, 2012 3:35 pm
Posts: 440
Location: Tonight
Languages: NASM x86_64, C, C++, D, Java, Python, Lua, HTML, CSS, PHP, JavaScript
Text editor: Geany
If you want your function or class to use argc and argv, just pass them on as arguments

cpp code:
 
#include <iostream>
 
void printArgs(int argc, char* argv[])
{
    for (int i = 0; i < argc; ++i)
    {
        std::cout << argv[i] << '\n';
    }
}
 
class ArgHolder {
public:
    ArgHolder(int argc, char* argv[]) :
        m_argc(argc),
        m_argv(argv) { }
   
    void print() const
    {
        for (int i = 0; i < m_argc; ++i)
        {
            std::cout << m_argv[i] << '\n';
        }
    }    
   
private:
    int m_argc;
    char** m_argv;
};
 
int main(int argc, char* argv[])
{
    printArgs(argc, argv);
    ArgHolder args(argc, argv);
    args.print();
}
 
 


If you just want to "read a file", as you stated, you can pass the specific argument (filename) into your function, instead of the whole argv array.

cpp code:
 
#include <fstream>
#include <iostream>
 
bool printFile(const char* filename)
{
    std::ifstream file(filename);
 
    if (!file)
    {
        std::cerr << "Failed to open file \"" << filename << "\"!" << std::endl;
        return false;
    }
 
    file.seekg(0, std::ios::end);
    std::ifstream::pos_type length = file.tellg();
    file.seekg(0, std::ios::beg);
    char* buffer = new char[length];
    file.read(buffer, length);
    std::cout.write(buffer, length);
    delete[] buffer;
    return true;
}
 
int main(int argc, char* argv[])
{
    if (argc < 2)
    {
        std::cout << "Usage: " << argv[0] << "<filename>" << std::endl;
        return 1;
    }
 
    for (int i = 1; i < argc; ++i)
    {
        if (!printFile(argv[i]))
        {
            return 1;
        }
    }
}
 
 

_________________
C++ reference and C++ FAQ
My github page (Give me some love, send me pull requests!)
Remember to use a version control system (Git, Mercurial) for your projects! They make life a whole lot easier!


Top 
 Post subject: Re: working with argc and char*argv[]
 Post Posted: Fri Feb 01, 2013 8:25 am 
Offline
Hero of the Forum
Hero of the Forum
User avatar

Joined: Tue Nov 13, 2012 4:37 pm
Posts: 1023
Location: Serbia
Languages: C/C++ HTML CSS JavaScript PHP BasicsOf: Java Assembly SQL
Text editor: Sublime text
By the way, expect the program location (first string) and the opened file (second string) what else can system pass in that string?

_________________
Every problem has a solution.
.
Currently working on: RoyalWars


Top 
 Post subject: Re: working with argc and char*argv[]
 Post Posted: Fri Feb 01, 2013 10:37 am 
Offline
Legendary User
Legendary User
User avatar

Joined: Thu Apr 28, 2011 5:20 pm
Posts: 2292
Languages: c++
Text editor: notepad++ :)
Your own arguments. When you execute console programs from console you can add parameters. For example when you compile c++ source code from console you do:
g++ main.cpp -o main.exe
The bold text is program name you want to execute and all other words are custom arguments which are passed trough argc and argv.

_________________
http://www.youtube.com/watch?v=eDn2Xp5ctQM :D


Top 
Display posts from previous:  Sort by  
 
 Page 1 of 1 [ 5 posts ] 




Board index » Programming » C++


Who is online

Users browsing this forum: No registered users and 1 guest

 
 

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: