-2

I really cannot figure out what is wrong here... I want the code to create a file when I type one argument in the terminal. It works fine if argc is set to 1, but not anything above that. Any help is much appreciated ! Cheers & a nice sunday

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

int main(int argc, char* argv[])
{
    if (argc==(2))
    {
        ofstream file("MyFile.txt"); // Creates MyFile.txt
    }

    else
    {
        cout << "type one argument !" << endl;
    }
    return 0;
}
toasted_flakes
  • 2,502
  • 1
  • 23
  • 38
  • 1
    Please show your command line inputs you are using. This looks correct. – OldProgrammer Apr 06 '14 at 16:11
  • Did you try debugging? Set a breakpoint and see what `argc` and `argv` values are. – crashmstr Apr 06 '14 at 16:14
  • It would be helpful to know the value `argc` receives before comparing it to the value of two. Is there a `cout` statement or the value from the debugger on the `if (argc == (2))` line? – CPlusPlus OOA and D Apr 06 '14 at 16:14
  • Well in the terminal I just type in something like /code.exe a as in 'a' being my second argument after my program name. Checked with debugging, when I do a cout << argc it displays '2' but the file is still not created... – user3476572 Apr 06 '14 at 16:19
  • It works fine on ubuntu/mac btw... – Rontogiannis Aristofanis Apr 06 '14 at 16:38
  • 1
    What compiler version and development system settings are being used? – CPlusPlus OOA and D Apr 06 '14 at 16:41
  • @RontogiannisAristofanis oh yeah ?? if u type in one argument it makes the file ?? – user3476572 Apr 06 '14 at 16:44
  • im using up to date code::blocks on win8.1 – user3476572 Apr 06 '14 at 16:44
  • Never tried `code::blocks` in Win8. Does the IDE allow setting a break point on the `if` statement? – CPlusPlus OOA and D Apr 06 '14 at 16:45
  • Have you checked if `file's` failbit is set? It's possible there is a permission or disk writing error. – IllusiveBrian Apr 06 '14 at 16:55
  • hmm no idea what the failbit is or where to find it :/ – user3476572 Apr 06 '14 at 16:58
  • `file.fail()` will return true if the failbit is set, so you can check with `cerr << file.fail()?"Error opening file":"";` or something similar. – IllusiveBrian Apr 06 '14 at 17:01
  • If the file already exists, then that is a problem, at least in older Windows versions it is. I am waiting for what happens when the break point is set and stepping forward from there. My system is perfectly behaving, but the compiler and IDE are different, as well as running in Win7 64-bit. – CPlusPlus OOA and D Apr 06 '14 at 17:03
  • _It works fine if argc is set to 1, but not anything above that._ Intuition suggests that, contrary to the usual behavior, your `argc`/`argv` are not getting your program name as the first argument, so when you type one argument into the terminal you're getting exactly one entry in `argv`. I'd suggest looping through `argv` and `cout`'ing each entry so you see exactly what your program is seeing. This is just a (admittedly wild) guess, though, as it doesn't match what I just saw on my Win7 box (`test.exe a` gave my program `test.exe` and `a` for `argv`). – cf- Apr 06 '14 at 17:39
  • How about ofstream file("MyFile.txt", ios::out); file << "test"; file.close(); does it create anything this way? – berkus Apr 06 '14 at 18:05
  • still no luck... damn I really cant figure this out, I'm sure it's really easy t fix as well... – user3476572 Apr 07 '14 at 13:03

1 Answers1

0

If you are using Visual Studio then answer is : go to Project/Properties, select "Debugging" tab, and enter arguments in "Command arguments" field.

Iposify
  • 663
  • 2
  • 7
  • 9