File Rename Program

Hi there,

I'm having issues with the following code. My intention is for it to rename a file based on the content of the header. The problem is that in the code block that evaluates the header (lines 34 through 46), it determines the first if statement (line 40) to be true every time and assigns "netBranchUsers.txt" to newFile. I added "cout << line" (line 39) to verify the header was actually being read from each file rather than repeated from the previous file, so I'm not quite sure what else could be causing the problem. I'm using DevC++ 4.9.9.2 and my output is at the end of this post. Any help is greatly appreciated.

Aaron

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
79
80
81
82
83
84
85
#include <dirent.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
1    DIR * dir = opendir("C:\\resourceOne\\muu\\dataFiles");
2    ifstream inFile;
3    string cmd, 
4           line, 
5           oldFile,
6           filePath,
7           justPath = "C:\\resourceOne\\muu\\dataFiles\\",
8           newFile;
9	
10	if (!dir)
11		cout << "Error opening directory." << endl;
12	else
13	{
14	    struct dirent * entry;
15		
16	    while(entry = readdir(dir))
17	    {            
18          //read the value into fileName variable        
19          oldFile = entry->d_name;
20          int loc = oldFile.find(".lpt");
21          
22          //if the file has an .lpt extension
23          if (loc > 1) 
24          {
25 	          cout << "\nfound file: " << oldFile << "\ndetermining file 
26                name..." << endl;
27 	          filePath = justPath + oldFile;
28 	          cout << filePath << endl;
29                inFile.open( filePath.c_str() );
30              
31            if (!inFile.is_open())
32               cout << "Failed to open file." << endl;
33             
34            //determine file name based on header row
35            else
36            {
36               getline(inFile, line);
38                // next line for testing only //
39                cout << line << endl;
40                if (line.find("IPAYS") > 0)
41                 newFile = "netBranchUsers.txt";
42                else if (line.find("COMPANY_NAME", 0))
43                  newFile = "contacts.txt";
44                else if (line.find("HOT_FLAG", 0))
45                  newFile = "debitCards.txt";
46          }
47            
48            // if the new file name is empty string, print error
49            if (newFile.size() == 0)
50                cout << "File read error." << endl;
51
52            else
53            {
54                cmd = "copy " + filePath + " " + newFile;       
55                // uncomment to print cmd
56                cout << cmd << endl;
57                // execute cmd
58                //system(cmd.c_str()); 
59            }
60            
61            // close the current file
62            inFile.close();              
63
64           // clear variables for next iteration
65           cmd.clear(); 
66           line.clear();
67           oldFile.clear();
68           newFile.clear();
69           filePath.clear();
70           }
71    }
72    closedir(dir);
73  }
  system("PAUSE");
  return 0;
}



found file: 2318.lpt
determining file name...
C:\resourceOne\muu\dataFiles\2318.lpt
"ACCOUNT_NUM","STATUS_CODE","CUSTOMER","REL_CODE","OPEN_TLR","LST_FM_DATE","OPEN
_DATE","CLOSED_DATE","PROD_TYPE","CUR_BAL","ORIG_BAL","INT_RATE","LOC_AVAIL"
copy C:\resourceOne\muu\dataFiles\2318.lpt netBranchUsers.txt

found file: 3186.lpt
determining file name...
C:\resourceOne\muu\dataFiles\3186.lpt
"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","DRIVER
S_LIC_STATE","CLOSED_DATE","USER_CODE1","USER_CODE2","USER_CODE3","USER_CODE4","
USER_CODE5","SSN"
copy C:\resourceOne\muu\dataFiles\3186.lpt netBranchUsers.txt

found file: 5535.lpt
determining file name...
C:\resourceOne\muu\dataFiles\5535.lpt
"ACCOUNT_NUM","STATUS_CODE","CUSTOMER","REL_CODE","OPEN_TLR","LST_FM_DATE","OPEN
_DATE","CLOSED_DATE","PROD_TYPE","CUR_BAL","REF_TLR"
copy C:\resourceOne\muu\dataFiles\5535.lpt netBranchUsers.txt

found file: 6816.lpt
determining file name...
C:\resourceOne\muu\dataFiles\6816.lpt
"ACCOUNT_NUM","STATUS_CODE","CUSTOMER","REL_CODE","OPEN_TLR","LST_FM_DATE","OPEN
_DATE","CLOSED_DATE","IPAYS","ENOTE","ESTMT","BILLPAY","SPECOCC","CUR_BAL"
copy C:\resourceOne\muu\dataFiles\6816.lpt netBranchUsers.txt

found file: 7785.lpt
determining file name...
C:\resourceOne\muu\dataFiles\7785.lpt
"ACCOUNT_NUM","STATUS_CODE","CUSTOMER","REL_CODE","OPEN_TLR","LST_FM_DATE","OPEN
_DATE","CLOSED_DATE","PROD_TYPE","CUR_BAL","REF_TLR"
copy C:\resourceOne\muu\dataFiles\7785.lpt netBranchUsers.txt

found file: 8216.lpt
determining file name...
C:\resourceOne\muu\dataFiles\8216.lpt
"ACCOUNT_NUM","STATUS_CODE","CUSTOMER","REL_CODE","OPEN_TLR","LST_FM_DATE","OPEN
_DATE","CLOSED_DATE","PROD_TYPE","CUR_BAL","ORIG_BAL","INT_RATE","LOC_AVAIL"
copy C:\resourceOne\muu\dataFiles\8216.lpt netBranchUsers.txt

found file: 8916.lpt
determining file name...
C:\resourceOne\muu\dataFiles\8916.lpt
"ACCOUNT_NUM","STATUS_CODE","CUSTOMER","REL_CODE","OPEN_TLR","LST_FM_DATE","OPEN
_DATE","CLOSED_DATE","PROD_TYPE","HOT_FLAG","CUR_BAL"
copy C:\resourceOne\muu\dataFiles\8916.lpt netBranchUsers.txt

found file: 8918.lpt
determining file name...
C:\resourceOne\muu\dataFiles\8918.lpt
"ACCOUNT_NUM","STATUS_CODE","CUSTOMER","REL_CODE","OPEN_TLR","LST_FM_DATE","OPEN
_DATE","CLOSED_DATE","PROD_TYPE","CUR_BAL","REF_TLR"
copy C:\resourceOne\muu\dataFiles\8918.lpt netBranchUsers.txt
Press any key to continue . . .
If find() fails to find the string you put in, I'm pretty sure it returns string::npos, which greater than zero. Try checking if the return value != npos instead.
Excellent Zhuge. That did it, thank you.

Aaron
Topic archived. No new replies allowed.