954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ file doesn't exist?... (but it does!)

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

foggy_coder
Newbie Poster
5 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

foggy_coder
Newbie Poster
5 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thanks so much! :)

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

foggy_coder
Newbie Poster
5 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You