Unable to open file

Hi there,

I'm having issues opening an input file stream and was hoping someone had some insight as to where I'm going wrong. The goal of this program is to determine the name of the file based on the header information, and then rename it. I've also tried specifying a constant string but I get the same result (see output at the end). If it helps at all, here is the header row from the file (custom line breaks added for readability):

"CUSTOMER_NUM","REL_CODE","COMPANY_NAME","FIRST_NAME","MIDDLE_NAME","LAST_NAME",
"TITLE","ADDRESS1","ADDRESS2","CITY","STATE","ZIPCODE","COUNTRY","EMAIL1",
"HOME_PHONE","FAX","WORK_PHONE","MOBILE","PAGER","DATE_OF_BIRTH","DRIVERS_LIC",
"DRIVERS_LIC_STATE","CLOSED_DATE","USER_CODE1","USER_CODE2","USER_CODE3",
"USER_CODE4","USER_CODE5","SSN"


I'm compiling with DevC++ 4.9.9.2. on a Windows 7 x64 machine. Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <dirent.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    DIR * dir = opendir("C:\\programDir\\dataFiles");
    ifstream inFile;
    string cmd, 
           line, 
           oldFile, 
           newFile;
	
	if (!dir)
		cout << "Error opening directory." << endl;
	else
	{
	    struct dirent * entry;
		
	    while(entry = readdir(dir))
	    {            
            //read the value into fileName variable        
            oldFile = entry->d_name;
            int loc = oldFile.find(".lpt");
            
            //if the file has an .lpt extension
            if (loc > 1) 
            {
 	          cout << "\nfound file: " << oldFile << "\ndetermining file name..." << endl;
              inFile.open( oldFile.c_str() );
              
              if (!inFile.is_open())
                 cout << "Failed to open file." << endl;
             
              //determine file name based on column headers
              else
              {
                  getline(inFile, line);
                  cout << line << endl;
                  if (line.find("COMPANY_NAME") > 0)
                    newFile = "contacts.txt";
                  else if (line.find("IPAYS") > 0)
                    newFile = "netBranchUsers.txt";
                  else if (line.find("HOT_FLAG") > 0)
                    newFile = "debitCards.txt";
              
              if (newFile.size() == 0)
                  cout << "File read error." << endl;

              else
              {
                  cmd = "ren ";          
                  cmd.append(oldFile);
                  cmd.append(" ");
                  cmd.append(newFile);
                  // just print the command
                  cout << cmd << endl;
                  // uncomment to execute command
                  //system(cmd); 
              }

                  inFile.close();
              }

             cmd.clear(); 
             line.clear();
             oldFile.clear();
             newFile.clear();
            }
	}
	closedir(dir);
    }
    system("PAUSE");
    return 0;
}


Here's my present output:


found file: 3186.lpt
determining file name...
Failed to open file.
Press any key to continue . . .


Thank so much for taking a look.

Aaron
You seem to be trying to open a file without providing the full path, just the name of the file. If the file is not in the same directory as the executable, that will not work.
I cannot believe I didn't see that. Thank you. I'll give it a try and post back if I still have issues.
Topic archived. No new replies allowed.