# Ofstream file doesn't output via drag&drop

This topic is 3793 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi all, i know this is pretty much a (probably stupid easy) solution to this and i feel dumb asking it but i have the code below that takes a file, does some sort on it then outputs the sorted results to a new file. when i send in the file (through the argument list via the windows command line) the output file is created and i am able to write the results to it. however if i drag the file in a windows explorer onto the .exe the output.txt is not even created at all and therefore nothing is written to it. what is the solution to this? thanks!
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>

using namespace std;
//using System;

ifstream infile;
ofstream outfile;
double num;
vector<double> a;

int main(int argc, char **argv)
{

infile.open(argv[1]);

if(!infile)
{
cout << "could not open file " << argv[1] << endl;
return 0;
}

while(infile)
{
infile >> num;
a.push_back(num);
}

cout << "file size is: " << a.size() << endl;
outfile.open("output.txt");
cout << "sorting data...";
sort(a.begin(), a.end());
cout << "done." << endl;

cout << "writing sorted data to output.txt...";
for(int i = 0; i < a.size(); i++)
outfile << a << endl;
cout << "done." << endl;

infile.close();
outfile.close();

return 0;
}



##### Share on other sites
Are you sure the file isn't created? When you drag and drop a file onto an application, the current working directory is probably not the directory of the source file or the directory of the executable. You should probably explicitly set the current working directory to either that of the source file or the executable so you can reliably determine where the file is being created.

##### Share on other sites
Quote:
 Original post by SiCraneAre you sure the file isn't created? When you drag and drop a file onto an application, the current working directory is probably not the directory of the source file or the directory of the executable. You should probably explicitly set the current working directory to either that of the source file or the executable so you can reliably determine where the file is being created.

when i run it from the windows cmd line it is created from the same directory as the .exe. i have tested this and it works. that is by itself with the file and the .exe the output.txt file is created and written to. why wouldnt the same work for dragging the file over the .exe? where else could it possibly store it? i thought by default with streams it was either whether the source was located or the .exe.

##### Share on other sites
If you don't specify a path for the file, then streams will use the current working directory for locating or creating the file. When you run your program from the command line the current working directory is whatever directory you ran the program from. When you drag and drop a file on the executable, it's effectively random.

##### Share on other sites
Quote:
 Original post by SiCraneIf you don't specify a path for the file, then streams will use the current working directory for locating or creating the file. When you run your program from the command line the current working directory is whatever directory you ran the program from. When you drag and drop a file on the executable, it's effectively random.

well - i thought it might have been random too. but i just finished searching for the file and it does not exist on the machine. not anywhere. really weird.

##### Share on other sites
Quote:
Original post by OpenGL_Guru
Quote:
 Original post by SiCraneIf you don't specify a path for the file, then streams will use the current working directory for locating or creating the file. When you run your program from the command line the current working directory is whatever directory you ran the program from. When you drag and drop a file on the executable, it's effectively random.

well - i thought it might have been random too. but i just finished searching for the file and it does not exist on the machine. not anywhere. really weird.

You might not have had permissions to create the file in the "effectively random" place. But in any case, what I assume you should be doing is to parse the argv[1] to figure out the path to the input file, and then qualify the output path the same way.

Try using a batch file to run the program; drag the text file onto the .bat, and have the bat forward %1 to your program's argv. That way you can see any output messages from the program (if you run 'pause' after your program), and possibly output additional debugging messages.

BTW, there's no reason for using globals here, since there's only one function in the first place. Also, stream objects are normally most easily 'opened' through their constructors (i.e. passing the appropriate parameter to the constructor) and 'closed' through their destructors (i.e. without you writing code for it at all). There's also no need to return 0 explicitly from main(). You should also return a non-zero value in the "error" cases, so that batch files can check for errors :)

#include <algorithm>#include <vector>#include <iostream>#include <fstream>using namespace std;int main(int argc, char **argv) {  ifstream infile(argv[1]);  if(!infile) {    cout << "could not open file " << argv[1] << endl;    return -1;  }        double num;  vector<double> a;  while (infile >> num) { a.push_back(num); }  cout << "file size is: " << a.size() << endl;  ofstream outfile("output.txt");  cout << "sorting data...";  sort(a.begin(), a.end());   cout << "done." << endl;   cout << "writing sorted data to output.txt...";  for (int i = 0; i < a.size(); ++i) { outfile << a << endl; }  cout << "done." << endl;}

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
14

• 14
• 10
• 23
• 9
• 33
• ### Forum Statistics

• Total Topics
632631
• Total Posts
3007536
• ### Who's Online (See full list)

There are no registered users currently online

×