|
Apologies for the shouting but this is important.
When answering a question please:
- Read the question carefully
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar
- If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome
- If the question is inappropriate then click the 'vote to remove message' button
Insults, slap-downs and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|
|
For those new to message boards please try to follow a few simple rules when posting your question.- Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
- Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
- Keep the subject line brief, but descriptive. eg "File Serialization problem"
- Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
- Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
- Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
- If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode HTML tags when pasting" checkbox before pasting anything inside the PRE block, and make sure "Ignore HTML tags in this message" check box is unchecked.
- Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
- Please do not post links to your question in one forum from another, unrelated forum (such as the lounge). It will be deleted.
- Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
- If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
- No advertising or soliciting.
- We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|
|
i'm just testing to run prog using parameters, but why i got error when debugging?
screenshot[^]
this is my code:
#include <iostream>
int main (int argc, char * argv []) {
if (argc > 0) {
std::cout << *argv[1] << std::endl;
}
std::cin.ignore(std::numeric_limits<int>::max(), '\n');
std::cout << "press ENTER to close program.";
std::cin.get();
return 0;
}
|
|
|
|
|
If you call the program via:
myprog.exe
then argc will equal 1 , and argv[1] will not exist, hence the exception. Remember, array indexes start from zero, not one.
|
|
|
|
|
i know, but how to respond users if there's argument(s) submitted?
|
|
|
|
|
You have two choices:
1. Your program needs specific argument values which you must check by iterating over the items in argv . The actual number and type is for you to decide.
2. Your program does not accept command line arguments in which case you just ignore them. You may wish to display a warning message if argc is greater than 1.
|
|
|
|
|
1. you mean like loop? i tried this but still error:
#include <iostream>
int main (int argc, char * argv []) {
int itr = 0;
if (argc > 0) {
while (itr <= argc) {
std::cout << argv[itr] << std::endl;
++itr;
}
}
std::cin.ignore(std::numeric_limits<int>::max(), '\n');
std::cout << "press ENTER to close program.";
std::cin.get();
return 0;
}
2. so how to make my program accept argument(s)?
|
|
|
|
|
You did not mention what error you received, but I assume it is because you are still trying to access an item which does not exist. The values for argc and argv are as follows:
1. argv is an array of strings, which will always contain at least one item: the name of the executable that is used to initiate the program.
2. argc contains the number of items in the argv array. So it will always be at least 1.
Let's look at a couple of examples:
1. A simple command line call to run my program, which is called Test.exe produces the following:
C:\Users\user1\Documents\Code\C++>Test.exe
argc = 1
argv[0] = Test.exe
There are no input parameters, argc equals 1 as there is only one item in argv , and that is argv[0] which contains the program name. If I use the full path to run the program the output will be:
C:\Users\user1\Documents\Code\C++>C:\Users\user1\Documents\Code\C++\Test
argc = 1
argv[0] = C:\Users\user1\Documents\Code\C++\Test
If we add some parameters to the command line we will get something like
C:\Users\user1\Documents\Code\C++>Test.exe one two "three and a half"
argc = 4
argv[0] = Test.exe
argv[1] = one
argv[2] = two
argv[3] = three and a half
So argv now contains 4 items, the program name followed by each item that is separated by a space or tab. Not that argv[3] contains four words, since they were delineated by double quotes in the command line.
The code to list these values is as follows:
std::cout << "argc = " << argc << std::endl;
for (int i = 0; i < argc; ++i)
{
std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
}
Generally you are not interested in the program name so you can start the above loop from 1 rather than 0.
The actual values in each parameter are for you to decide. You can use simple strings in a specific or random order, option letters or names preceded by single or double dashes or forward slashes:
Test check \foo\bar\filename.txt
Test -c C:\user1\Documents\file.jpg
Test --check somefilename
... etc.
|
|
|
|
|
this is really embarrassing! this was my mistake...
thank you very much! i just fix the code and now it's working![^]
this is the final test code:
#include <iostream>
int main (int argc, char * argv []) {
if (argc > 1) {
std::cout << argv[1] << std::endl;
}
std::cin.ignore(std::numeric_limits<int>::max(), '\n');
std::cout << "press ENTER to close program.";
std::cin.get();
return 0;
}
really appreciate your reply!!
|
|
|
|
|
|
what is best resource to learn python language
|
|
|
|
|
you know that this is C & C++ thread, right?
|
|
|
|
|
|
Try to Google with Python and tutorial.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
The C/C++/MFC forum, of course. 
|
|
|
|
|
|
Can i Use C++ Progam to develop a Appliciton for Andriod?
|
|
|
|
|
What happened when you Googled for "C++ application development"?
|
|
|
|
|
Yes.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
|
Hi, How are you?...i hope you are doing fine... i want to ask is there any built in function to get on exact line in a file? Like:
FILE DATA:
1)Start
2)Mid
3)Username
4)END
i want to get the pointer of file handler on 3 so that i can read that specific line....
or Can we use Seekg.(); function in this case? Kindly help please
|
|
|
|
|
I don't know of such a function. If each line ends in an '\n' , you can use std::getline to read each one and count up to the line you want.
seekg moves by a specified number of characters, so it won't help unless lines are padded to a fixed length.
|
|
|
|
|
The simplest way to do this is to read the file line by line and check for the data that you are interested in. Remember a text file is just a stream of characters with no structure so you cannot easily use file positioning to get to a particular item.
|
|
|
|
|
If you have questions or comments then please use the Reply link, not the Email.
|
|
|
|
|
but i want to privately message you dont want leak my code please check the inbox kindly ... Thank you
|
|
|
|