Part of the files cannot be reached under the same folder

Hi all,

I have a few files in CSV format under a folder, I tried to combine these CSV files as one. I applied a for loop to process the files one by one; however, part of the files cannot be reached. All of the files were produced by the same program, so i think that there is no encoding issue.
Can anyone help me? I want to figure out what's wrong. My source codes are in below.

thanks in advance.

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
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <iostream>
#include <fstream> /* ifstream class */
#include <string> /* string class */
#include <cstdlib> /* atoi */
#include <sstream> /* istringstream class */
#include <string.h>


using namespace std; 

#define filename1 "1dtrainingdata.csv"


int getdir(string dir, vector<string> &files);
int main()
{ 
	struct stat buf;
	string path = "d:\\GC_Vectors";
	int i,j,ROW,COL,ROW_1;
	string **arr = NULL;
	string filename;
	
	fstream fin;
	fstream fin_1;
	ofstream fp1(filename1); 
	
	string dir = string("d:\\GC_Vectors");
   
    
    vector<string> files = vector<string>();
    getdir(dir, files);    
 
    
    	
	for(i=0; i<files.size(); i++)
	{ 
   
    ROW=0;
    COL=0;
    ROW_1=0;
    filename = files[i];
    cout << "stage 1 " << filename << endl;                          
	        
	
	if(stat(filename.c_str(), &buf) != -1)
	{
	
		if (filename == "."||filename == "..")
		{
			continue;
		}
		else
		{
			fstream fin(filename.c_str(),ios::in);
			cout << "stage 2 open " << filename <<endl; 
			
			string line;
			int linecount=0;
	
		while (getline (fin, line, '\n'))  
		{
		linecount++;
		
		istringstream stream(line);
		string item;
		int itemcount=0;
									
			while (getline (stream, item, ',')) 
			{		
					itemcount++;					
			}						
				ROW = itemcount;															
		}	
		COL = linecount;
		cout << COL << " " << ROW << endl;
		system("pause");
		
		fin.close();
		fin.clear(); 
	

	fstream fin_1(filename.c_str(), ios::in);
	
	string outputstring, string_1;
	arr = new string*[COL];
	
	for (i=0; i<COL; i++)
	{
		arr[i] = new string [ROW];		
	}
	
	for(i=0; i<COL; i++)
	{
		getline (fin_1, string_1, '\n');
		istringstream stream(string_1);
		
		for(j=0; j<ROW; j++)
		{		
			getline (stream, outputstring, ',');				
			arr[i][j] = outputstring; 
									
		}	
		
		
	}			
	
	fin_1.close();
	fin_1.clear();

		
	ROW_1 = ROW - 5;
	

    if(!fp1)
	{
        cout<< "stage 4 Fail to open file: " << endl;        
    }
    else
    {
    	for (i=0; i<COL; i++)
    	{
    		for(j=0; j<ROW_1; j++)
    		{
    			fp1 << arr[i][j] << ",";
    					
			}
			fp1 << endl;
		}		
	}
		
	}	
	delete [] arr;		
	
}
else
{
	cout << "file is unable to open " << filename <<endl;
	system("pause");
	continue;
}
}
	
	fp1.close();
	fp1.clear(); 
	
	return 0;
}



int getdir(string dir, vector<string> &files){
    DIR *dp;
    struct dirent *dirp;
    if((dp = opendir(dir.c_str())) == NULL)
	{
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }
    while((dirp = readdir(dp)) != NULL) 
	{
        files.push_back(string(dirp->d_name));
    }
    closedir(dp);
    return 0;
}


Last edited on
Why are you making this so complicated? The CSV format is just plain text, it literally stands for "Comma Separated Value". Just read from the source file and append to the destination. Also, please use code tags, as you can see not doing so strips out your white-space and as a result makes your post unnecessarily difficult to read.
Last edited on
however, part of the files cannot be reached.

What cannot be reached? Some of the actual files, or the data inside the files? Is it that the last five items on each line are missing.
Hi Chervil, thanks for your reply.
I am unable to reach part of the actual files.

I tried to figure out what's wrong by applying "cerr << "Error: " << strerror(errno);", and got a message "no such file or directory" for those files cannot be opened.
This message makes me confused; any idea for this?
Hi Computergeek01.
I am not familiar with this interface, could you guide me how to use code tags?
thanks in advance
I tried to figure out what's wrong by applying "cerr << "Error: " << strerror(errno);", and got a message "no such file or directory" for those files cannot be opened.

I'm not sure what it means either. Maybe there are some other types of files (subdirectories, hidden files, etc.) in the given directory. I tried a much-simplified version of your code with no problems, but my folder contained just three files of type .csv and nothing else.

Maybe this example might help, though it doesn't do exactly what you want,
http://www.cplusplus.com/forum/beginner/10292/#msg47965
Last edited on
@ OP: It looks like you're trying to use Linux libraries in a Windows environment. Are you running this in Cygwin?
Hi Computergeek01, no, I run it on Windows 10.


How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

Hi Chervil, thanks it work.

I think that I figure out what's wrong in my codes.
I open the input file twice through fstream fin and fin_1; it runs correctly in stage one (fstream fin) but stage two (fstream fin_1).

any ideas for this?
thanks

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
fstream fin_1(filename.c_str(), ios::in);
	
	string outputstring, string_1;
	arr = new string*[COL];
	
	for (i=0; i<COL; i++)
	{
		arr[i] = new string [ROW];		
	}
	
	for(i=0; i<COL; i++)
	{
		getline (fin_1, string_1, '\n');
		istringstream stream(string_1);
		
		for(j=0; j<ROW; j++)
		{		
			getline (stream, outputstring, ',');				
			arr[i][j] = outputstring; 
									
		}	
		
		
	}			
	
	fin_1.close();
	fin_1.clear();
Last edited on
Topic archived. No new replies allowed.