У меня есть следующая проблема:
Когда я перетаскиваю файл в свой инструмент (exe), когда ifstream
не удается открыть файл.
Если я даю его вручную через консоль, он работает!
Я не понимаю, в чем разница, потому что я я обрезаю путь и передаю только имя файла.
Взгляните на код:
int main(int argc, char* argv[]) {
if (argc < 2) {
cout
<< "ERROR: Wrong amount of arguments! Give at least one argument ...\n"
<< endl;
cout << "\n" << "Programm finished...\n\n" << endl;
cin.ignore();
exit(1);
return 0;
}
vector<string> files;
for (int g = 1; g < argc; g++) {
string s = argv[g];
cout<<"parameter at: " << g << " = " << argv[g] << "\n" << endl;
string filename = "";
int pos = s.find_last_of("\\", s.size());
if (pos != -1) {
filename.append(s.substr(pos + 1));
// cout<<" cutted path: " << s.substr(0,s.size()-filename.size()) << endl;
// cout << "argv[1] " << argv[1] << endl;
cout << "\n filename: " << filename << "\t pos: " << pos << endl;
files.push_back(filename);
}
files.push_back(s);
}
for (unsigned int k = 0; k < files.size(); k++)
{
cout << "files.at( " << k << " ): " << files.at(k).c_str() << endl;
Converter a(files.at(k).c_str());
a.getCommandsFromCSV();
a.saveConvertedFile();
}
cout << "\n" << "Programm finished...\n\n" << endl;
cin.ignore();
return 0;
}
Он уже терпит неудачу в конструкторе:
Converter::Converter(const char* file) {
filename = file;
myfile.open(filename.c_str(), ios_base::in);
cout << (myfile ? "open successful on constructor " : "some error on constructor");
cin.ignore();
trace_raw = "";
}
У вас есть идеи, почему?
ОБНОВЛЕНИЕ:
Файл как параметр теперь работает. Решение состояло в том, чтобы оставить полный путь.
Во всяком случае, у меня такая же ошибка в жестко запрограммированном файле. Я подумал, что это может быть то же самое, поэтому я добавил .\
в начало имени файла... безуспешно.
Код:
void GenericCommandConverter::getATCommandsFromCSV() {
cout << "\t| +++++++++++getATCommandsFromCSV() started+++++++++++++ |"
<< endl;
/*
* CSV file name is hardcoded
*/
string filename_csv = ".\\test.csv";
string commands = "";
int pos_start = 0;
int pos_end = 0; // "|"
int substrLength = 0;
int separator_count = 0;
char c;
vector<string> lines;
vector<string> commandList;
vector<vector<string> > linesSeparated;
ifstream csvFile;
csvFile.open(filename_csv.c_str(), ios_base::in);
cout << (myfile ? "open successful on getATCommandsFromCSV " : "some error on getATCommandsFromCSV ");
cin.ignore();
...
UPDATE2: Решение было таково: при перетаскивании файла в exe "корневая" папка меняется на ту, из которой исходит перетащенный файл. Предоставление жестко закодированному файлу пути из *.exe решило эту проблему!
file
, чтобы увидеть, одинаково ли значение в обеих ситуациях. - person Kiril Kirov   schedule 03.02.2011