The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

C++ Question (fstream and args)

Sir CarcassSir Carcass I have been shown the end of my worldRound Rock, TXRegistered User regular
edited October 2012 in Help / Advice Forum
More like arghs. This may be a simple question, but does anyone know why double-clicking the exe of my program creates the output file as expected while dragging files onto it doesn't? Here is my code for reference:
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
		fstream output, input;
		string h = "this is a test";

		output.open("CombinedList.csv", fstream::out);
		output.write(h.c_str(),h.size());
	
		cout << "press any key...." << endl;
		cin.get();		

		output.close();  

		return 0;
}

I haven't gotten to the meat to this yet because I can't figure out why it's not creating a file when I drag files onto it.

Sir Carcass on

Posts

  • bowenbowen Sup? Registered User regular
    Huh, I don't see any reason why it wouldn't.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • EndEnd Registered User regular
    edited October 2012
    it's putting the file the current working directory, which is apparently different if you drag and drop a file onto the executable

    Normally it'd be the same directory the program is in, but in the drag and drop case, a little bit of research indicates it would be your home directory (C:\Users\name or C:\Document and Settings\name) instead.

    I'm a little curious why you are dragging files onto it if you're not actually looking at your command line, but perhaps that is a yet to be implemented feature for you.

    End on
    I wish that someway, somehow, that I could save every one of us
    zaleiria-by-lexxy-sig.jpg
  • bowenbowen Sup? Registered User regular
    Well that's a weird behavior.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • Sir CarcassSir Carcass I have been shown the end of my world Round Rock, TXRegistered User regular
    I know the drag and drop functionality is working correctly because I can cause the program to print the paths and file names.

  • Sir CarcassSir Carcass I have been shown the end of my world Round Rock, TXRegistered User regular
    End wrote: »
    it's putting the file the current working directory, which is apparently different if you drag and drop a file onto the executable

    Normally it'd be the same directory the program is in, but in the drag and drop case, a little bit of research indicates it would be your home directory (C:\Users\name or C:\Document and Settings\name) instead.

    I'm a little curious why you are dragging files onto it if you're not actually looking at your command line, but perhaps that is a yet to be implemented feature for you.

    The ultimate point of this exe is to merge 4 files into 1. The 4 files will be changing each week, so I want to be able to just drag and drop the current 4 onto it. But yeah, I haven't even gotten that far yet.

    I just checked, and yep, it's putting it in my users folder. How is that even a good idea.

    Is there any way around that?

  • bowenbowen Sup? Registered User regular
    Alternatively, put them into a folder and run a batch script like this:
    copy/b *.txt merged.txt
    

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • Sir CarcassSir Carcass I have been shown the end of my world Round Rock, TXRegistered User regular
    bowen wrote: »
    Alternatively, put them into a folder and run a batch script like this:
    copy/b *.txt merged.txt
    

    I also need it to strip out the first line from each file except the first one.

  • bowenbowen Sup? Registered User regular
    Ah yes this would get complicated fast then!

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • bowenbowen Sup? Registered User regular
    I'd do a ".\\CombinedList.csv" instead. Since I assume you're working on windows that should cheat it.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • Sir CarcassSir Carcass I have been shown the end of my world Round Rock, TXRegistered User regular
    bowen wrote: »
    I'd do a ".\\CombinedList.csv" instead. Since I assume you're working on windows that should cheat it.

    That doesn't seem to help, unfortunately.

    If nothing else, I guess I can hardcode a location, but having it put in the working directory would be so much better.

  • bowenbowen Sup? Registered User regular
    Ah yeah now that I think that might not work.

    argv[0] should contain a path, but may not be useful. Unfortunately working with a file system is incredibly stupid with C++ at current unless you're using OS specific libraries or Qt/Boost.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • EndEnd Registered User regular
    Yeah, argv[0] would have a path to the executable, but that isn't super reliable (although probably correct enough in this case). The GetModuleFileName() function will also do it, assuming you're on windows.

    I wish that someway, somehow, that I could save every one of us
    zaleiria-by-lexxy-sig.jpg
  • bowenbowen Sup? Registered User regular
    Yeah that'd be what I'd use (windows.h should have it)

    Though argv[0] can have relative paths. Which might be an issue with the drag and drop.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • RendRend Registered User regular
    I assume you are working in windows? Because this seems like a problem which could be easily handled by a pretty trivial linux bash script.

  • Sir CarcassSir Carcass I have been shown the end of my world Round Rock, TXRegistered User regular
    Yeah, this is in XP. I'll research the windows.h thing tomorrow.

    Thanks for all of the help so far!

Sign In or Register to comment.