Not reading my file.

I am using xCode on mac and when I want to run my program and give me ERROR: opening file - LOCTest.cpp please some help.
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  /**********************************************************
 * Program to interactively ask the user to provide
 * a file name, and then do following tasks.
 *
 *   1. open to file to read
 *   2. count the number of lines in the file.
 *   3. display file and the count of lines.
 *   4. Repeal until user ends the program.
 
 // to open a file there are two ways
 //1.Put the file inside the project where the main.cpp file exists
 //then when the program ask you to enter a file name, you need
 //to write a file name that you had written in the main.cpp file exists somefile.cpp
 //for example: "somefile.cpp" and you write (string fileName = "somfile.cpp";) inside your int main( ) code
 
 //2. Another way is using absolute path: say you have a file somefile.cpp
 //or somefile.txt or any fileÖ in c drive, then enter c:/somefile.txt ÖÖetc.
 
 *********************************************************/
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//struct to represent a node in the list
typedef struct Node
{
    string line;
    Node * next;    //next pointer
};

//function to read the file line by line
//store the lines into the list nodes
//count the number of lines and return the
//line count.
int countLOC(ifstream & file);
int countFunloc(ifstream & file);

//main method
int main()
{
	//cout << " my function name is  " << __FUNCTION__<< endl;
    string fileName = "";
    
    do
    {
        //prompt the user to enter the file name
        cout << "Enter file name to count lines or quit: ";
        cin >> fileName;
		cout << endl;
        
        //check if its a quit otherwise count lines
        if(fileName != "quit")
        {
            //open file stream
            ifstream in(fileName.c_str(), ios::in);
            
            //check if file is open to read
            if(!in.is_open())
            {
                cout << "ERROR: opening file - " << fileName << endl;
            }
            else //file is open read the lines and output
            {
                cout << fileName << " successfully read." << endl << endl;;
                cout << fileName << " has " << countLOC(in) << " number of lines." << endl << endl;
                ifstream in(fileName.c_str(), ios::in);
                
				cout << countFunloc(in) <<" number of lines " << endl << endl;
                //	cout << fileName << " has " << countLOC(in) << " number of lines." << endl;
                
                //close the file
                in.close();
            }
        }
        else
        {
            //exit message
            cout << "\nGoodBye!! Thank you for using LOC Program\n";
        }
        cout << endl;
    }while(fileName != "quit");
    
    system("pause");
    return 0;
}

int countLOC(ifstream & file){
	//  cout <<" my function name is "<<__FUNCTION__<< endl;;
    //list head node
    Node * head = NULL;
    
    string line = "";
    int count = 0;
    
    //read the file line by line
    while(!file.eof())
    {
		//cout << "Moad" << endl;
        //read a line from file
        getline(file, line);
        
        //check if successfully read
        if(file)
        {
            
			//check if is a blank line or comment line
			if(line.empty() || line.find("//") != string::npos)
			{
				//do nothing ignore the line
			}
			else
			{
				//create new Node
				Node * newNode = new Node;
				newNode->line = line;
				newNode->next = NULL;
                
				//store into list
				if(head == NULL)
				{
					head = newNode;
				}
				else
				{
					//find the last node
					Node * cur = head;
					while(cur->next)
						cur = cur->next;
					cur->next = newNode;
					cur->next->next = NULL;
				}
				count++;
			}
        }
    }
    
    //return line count
    return count;
}
int countFunloc(ifstream & file){
	
    //list head node
    Node * head = NULL;
	//bool bol = true;
	int flag,flag1=0;
    string line = "";
    int count = 0;
    
    //read the file line by line
    while(!file.eof())
    {
		flag=0;
		//cout << "Moad" <<endl;
        //read a line from file
        getline(file, line);
        
        //check if successfully read
        if(file)
        {
            
			//check if is a blank line or comment line
			if(line.empty() || line.find("//") != string::npos)
			{
				//do nothing ignore the line
			}
			
            if(flag1 == 1)
                count++;
			if(line.find("float") !=string::npos || line.find("void") !=string::npos || line.find("int") !=string::npos && line.find("(") !=string::npos &&(line.find)(")") !=string::npos && line.find("{") !=string::npos)
				//flag=1;
				//	if(line.find("in.close")==string::npos && line.find("system")==string::npos && line.find("getline")==string::npos && line.find("cout")==string::npos && line.find("for")==string::npos && line.find("while")==string::npos && line.find("switch")==string::npos && line.find("if")==string::npos)
            {	flag1=1;
                cout <<" The function Name is "<< line << endl << endl;
                count++;}
            
            if(line.find("}")!=string::npos)
            {
                //flag
                flag1 =0;
                
            }//}
        } 
        
    }        
    
    //return line count
	cout <<" The number of line is ";
    
    return count;
}
Try writing the file's absolute path.
Topic archived. No new replies allowed.