Hello,

I'm pretty new to C++ and I've been trying to this program to open a file and check if it exists for the last two hours. It seems like it should be so simple but it keeps eluding me. Here is the code in main that starts to run.

#include <stdio.h>

#include <QApplication>
#include <QFile>
#include <QtXml>
#include <QString>
#include <QtDebug>

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>

using namespace std;

#include "showfile.h"

void makeSubDirectories (string v1x_dir_str, string v2x_dir_str);

RMS_ShowFile_v1 gShowFile_v1;
RMS_ShowFile_v2 gShowFile_v2;

int
main(int argc, char* argv[])
{
	QCoreApplication a(argc, argv);
	int i,j,k,l;
	string path;
	char first_line[50];
	string fileType = ".shx";
	
	if (argc < 2)
	{
		cout << "Enter showfile path: " << endl;
		cin >> path;
	}
	else path = (argv[1]);
	
	strcpy(first_line, path.c_str());
	ifstream fp;
	fp.open(first_line, ifstream::in);
		
	if (!fp) 
	{
		cout << "Invalid path to v2x showfile" << endl;
		return 0;
	}

When I go to test it in Xcode's runlog I get:
"[Session started at 2008-11-03 17:26:04 -0800.]
Enter showfile path:
showloadtest.shx //I typed this in
Invalid path to v2x showfile

showtest has exited with status 0."

It keeps saying my file does not exist but I definitely have the "showloadtest.shx" file in the same directory folder as my program. Is there something simple that I'm missing here? I've tried ifstream as well as fopen but to no avail.

Thanks in advance,

foggy_coder

Recommended Answers

All 4 Replies

If the path contains spaces then cin >> path will not work because >> stops at the first space. To use spaces call getline(cin, path)

Thanks for responding, Ancient Dragon! :)

What I typed in ("showloadtest.shx") shouldn't have any spaces in it (unless I am missing something).
I did try the getline method and it still failed to move past the same point.


Any other suggestions would be appreciated.

Carmen

Apparently the file isn't where your program thinks it is. Type the complete path to the file and it should open ok.

Thanks so much! :)

I typed in the absolute path and finally got it opened!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.