# C++: 2d char arrays | File handling



## RBX (Mar 9, 2012)

I have a System Software lab test this Monday and we haven't been taught a thing. I tried something but am stuck at most basic things.

Here is my program, I need to find function signatures from a C program and add them to array. Working with Java for quite long has totally erased whatever C and C++ I knew from my mind.


```
/**
 *Write a class for file handling, having functions to open/ read/ write/ close/ reset.
 */
#include<fstream>
#include<iostream>
#include<cstring>

using namespace std;

class FHandling {
	ifstream a_file;
	//char* ptr;
	char func[][80];
	char dt_type[5][7];
	
	public: FHandling(char str[]) {
		a_file(str,ifstream::in);	
		//	*dt_type = {{"void"},{"int"},{"float"},{"double"},{"char"}};
		
		dt_type = {"void","int","float","double","char"};
		/*
		dt_type[0]= "void";
		dt_type[1]= "int";
		dt_type[2]= "float";
		dt_type[3]= "double";
		dt_type[4]= "char";
		*/
	}

	public: void search() {
		int i=0;
		char str[80];
		while(!a_file.eof()) {
			a_file.getline(str,80);
			for(int j=0;j<5;j++) {
				if(strstr(str,dt_type[j])) {
					if(strstr(str,"(") && strstr(str,")") {
						strcpy(func[i],str);
						i++;
					}
				}
			}
		}
	}
	
	public: void show() {
		for(int i=0;i<func.length;i++) {
			cout<<func[i]<<"\n";
		}
	}
};	

int main(void) {
	FHandling f1("1_test.c");
	f1.search();
	f1.show();
 
 
	return 0;
}
```

First of all, I can't initialize an String array. This could be done with 'string' datatype but I want to do this with char arrays.

There also seems problem with strstr() && strstr().
also can't think for problem with line 17:a_file(str,ifstream::in);

Took help from:
Identification Of Function Signature - C And C++ | Dream.In.Code
ifstream::ifstream - C++ Reference

P.S. I kind of don't like pointers and have no idea of file handling.

Edit:
<merged>

Ok, got rid of some problems, but they still exist.

```
#include<fstream>
#include<iostream>
#include<cstring>

using namespace std;

class FHandling {
	ifstream a_file;
	char func[][80];
	char *dt_type[7];
	
	public: FHandling(char str[]) {
		char arr[5][7] = {"void","int","float","double","char"};
		a_file(str);	
		
		for(int i=0;i<5;i++) {
			dt_type[i] = arr[i];
		}
	}

	public: void search() {
		int i=0;
		char str[80];
		while(!a_file.eof()) {
			a_file.getline(str,80);
			for(int j=0;j<5;j++) {
				if(strstr(str,dt_type[j]) != NULL) {
					if(strstr(str,"(") != NULL && strstr(str,")") != NULL) {
						strcpy(func[i],str);
						i++;
					}
				}
			}
		}
	}
	
	public: void show() {
		int size = sizeof(func)/sizeof(int);
		for(int i=0;i<size;i++) {
			cout<<func[i]<<"\n";
		}
	}
};	

int main(void) {
	FHandling f1("1_test.c");
	f1.search();
	f1.show(); 
	return 0;
}
```


----------



## Liverpool_fan (Mar 9, 2012)

Try a_str.open()
Then obviously a_str>> instread of a_str to read.

P.S.: There are lots of problems here. I'll get back.

P.P.S.: Any reason for using char arrays instead of std::string? Will make it much easier.


----------



## RBX (Mar 9, 2012)

a_file.open() works. Thanks.

Tried the strings as well, but the cstring library doesn't seem to work with C++ strings.

This code complies but the .exe 'stops responding'. Now I need to look for the 'lots of other problems' you mentioned.

```
#include<fstream>
#include<iostream>
#include<cstring>

using namespace std;

class FHandling {
	ifstream a_file;
	char func[][80];
	char *dt_type[7];
	
	public: FHandling(char str[10]) {
		char arr[5][7] = {"void","int","float","double","char"};
		a_file.open(str);	
		
		for(int i=0;i<5;i++) {
			dt_type[i] = arr[i];
		} 
	}

	public: void search() {
		int i=0;
		char str[80];
		while(!a_file.eof()) {
			a_file.getline(str,80);
			for(int j=0;j<5;j++) {
				if(strstr(str,dt_type[j]) != NULL) {
					if(strstr(str,"(") != NULL && strstr(str,")") != NULL) {
						strcpy(func[i],str);
						i++;
					}
				}
			}
		}
	}
	
	public: void show() {
		int size = sizeof(func)/sizeof(int);
		for(int i=0;i<size;i++) {
			cout<<func[i]<<"\n";
		}
	}
};	

int main(void) {
	char filename[10] = "1_test.c";
	FHandling f1(filename);
	f1.search();
	f1.show(); 
	return 0;
}
```


----------



## Liverpool_fan (Mar 9, 2012)

RazorbladeXtreme said:


> a_file(str);


a_file.open(str);




> char func[][80];


Size? 



> dt_type_ = arr;
> _


_
Use strcpy



			while(!a_file.eof()) {
		
Click to expand...

use while(a_file)

*stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong




			int size = sizeof(func)/sizeof(int);
		
Click to expand...

This will create major problems, seg phaults etc.
Create a class member to store the size perhaps



RazorbladeXtreme said:



			Tried the strings as well, but the cstring library doesn't seem to work with C++ strings.
		
Click to expand...

Which cstring functionality you want to use?_


----------



## RBX (Mar 9, 2012)

Liverpool_fan said:


> Size?


I don't know why should I define that. It's used to store function signatures and I don't know the no. of functions the file contains.



Liverpool_fan said:


> Which cstring functionality you want to use?


Mosty substring searching, perhaps. Didn't look for any C++ string library.


----------



## Liverpool_fan (Mar 9, 2012)

RazorbladeXtreme said:


> I don't know why should I define that. It's used to store function signatures and I don't know the no. of functions the file contains.


Then use pointers and allocate memory in runtime. Of just use std::vector in C++.




> Mosty substring searching, perhaps. Didn't look for any C++ string library.


string::substr - C++ Reference


----------



## RBX (Mar 10, 2012)

I have implemented some changes, still using char arrays.
The problem exists in istream::getline() method or maybe the text file.
I tried placing a cout after the getline to find that it doesn't care for '\n'. Even tried placing '\n' manually as delimiting character but still no luck.

EDIT: Actually it might be taking 80 chars that I've specified, but again I have no idea. I tried another text file with random text, it took 79 chars from first line and didn't read the rest.


```
#include<fstream>
#include<iostream>
#include<cstring>
#include<vector>

using namespace std;

class FHandling {
	private: ifstream a_file;
	private: vector<char*> func;
	private: char *dt_type[7];
	
	public: FHandling(char* str) {
		char arr[5][7] = {"void","int","float","double","char"};
		a_file.open(str);
		
		for(int i=0;i<5;i++) {
			//strcpy(dt_type[i],arr[i]);
			dt_type[i] = arr[i];
		} 
	}

	public: void search() {
		char str[80];
		while(a_file) {
			a_file.getline(str,80,'\n');
			cout<<str; //Test Line
			for(int j=0;j<5;j++) {
				if(strstr(str,dt_type[j]) != NULL) {
					if(strstr(str,"(") != NULL && strstr(str,")") != NULL) {
						func.push_back(str);
					}
				}
			}
		}
	}
	
	public: void show() {
		for(int i=0;i<func.size();i++) {
			cout<<func[i]<<endl;
		}
	}
};	

int main(void) {
	char filename[10] = "1_test.txt";
	FHandling f1(filename);
	f1.search();
	f1.show();
	return 0;
}
```


----------



## RBX (Mar 11, 2012)

Ok, so the problem lies in dt_type array and not in getline.

When I use it in search() method, it turns out to array of empty strings, while it works in constructor.
Probably because I'm assigning values to it from a local array, and it gets lost after constructor's scope is over. I tried strcpy, but when using it my exe stops responding.

EDIT:
Resolved that, now when I look at vector :: push_back, I think it too is not working. Used them for the first time so I've no idea what might be wrong.


```
#include<fstream>
#include<iostream>
#include<cstring>
#include<vector>

using namespace std;

class FHandling {
	ifstream a_file;
	vector<char*> func;
	char *dt_type[7];
	int flag;
	
	public: FHandling(char* str) {
		//char arr[5][7] = {"void","int","float","double","char"};
		a_file.open(str,ifstream::in);
		/*
		for(int i=0;i<5;i++) {
			//strcpy(dt_type[i],arr[i]); //fail
			dt_type[i] =  arr[i];
		} 
		*/
		
		dt_type[0] = "void";
		dt_type[1] = "int";
		dt_type[2] = "float";
		dt_type[3] = "double";
		dt_type[4] = "char";
		
		//for(int i=0;i<5;i++) 	cout<<dt_type[i]<<endl; //Test //Working
		
		flag = 0;
	}

	public: void search() {
		//char str[sizeof(a_file)]; //test *
		char str[100];
		while(a_file) {
			char *dt=NULL, *lb=NULL, *rb=NULL;
			//a_file.getline(str,sizeof(a_file),'\n'); //test *
			a_file.getline(str,100,'\n');
			//a_file>>str; //fail
			cout<<str<<endl; //Test Line
			
			for(int j=0;j<5;j++) {
				if((dt = strstr(str,dt_type[j])) != NULL) {
					flag = 1;
					cout<<"found "<<dt_type[j]<<endl; //Test Line
				}
			}	
			if(flag == 1) {
				if((lb = strstr(str,"(")) != NULL && (rb = strstr(str,")")) != NULL) {
					cout<<endl<<"***ADDING***"<<endl; //Test Line
					func.push_back(str);
				}
			}
			flag = 0;
		}
	}
	
	public: void show() {
		for(unsigned int i=0;i<func.size();i++) {
			cout<<func[i]<<endl;
		}
	}
};	

int main(void) {
	char* filename = "1_test.txt";
	FHandling f1(filename);
	f1.search();
	f1.show();
	return 0;
}
```


----------



## RBX (Mar 12, 2012)

It now works, and needs a bit of finishing.


```
#include<fstream>
#include<iostream>
#include<cstring>
#include<vector>

using namespace std;

class FHandling {

	struct S {
		char x[100];
	};
	
	ifstream a_file;
	vector<S> func;
	char *dt_type[7];
	int flag;
	char str[100];
	
	public: FHandling(char* str2) {
		//char arr[5][7] = {"void","int","float","double","char"};
		a_file.open(str2,ifstream::in);
		/*
		for(int i=0;i<5;i++) {
			//strcpy(dt_type[i],arr[i]); //fail
			dt_type[i] =  arr[i];
		} 
		*/
		
		dt_type[0] = "void";
		dt_type[1] = "int";
		dt_type[2] = "float";
		dt_type[3] = "double";
		dt_type[4] = "char";
		
		//for(int i=0;i<5;i++) 	cout<<dt_type[i]<<endl; //Test //Working
		
		flag = 0;
	}

	public: void search() {
		//char str[sizeof(a_file)]; //test *
		struct S str_in;
		while(a_file) {
			char *dt=NULL, *lb=NULL, *rb=NULL;
			//a_file.getline(str,sizeof(a_file),'\n'); //test *
			a_file.getline(str_in.x,100,'\n');
			//a_file>>str; //fail
			cout<<str_in.x<<endl; //Test Line
			
			for(int j=0;j<5;j++) {
				if((dt = strstr(str_in.x,dt_type[j])) != NULL) {
					flag = 1;
					cout<<"found "<<dt_type[j]<<endl; //Test Line
				}
			}	
			if(flag == 1) {
				if((lb = strstr(str_in.x,"(")) != NULL && (rb = strstr(str_in.x,")")) != NULL) {
					cout<<endl<<"***ADDING***"<<endl; //Test Line
					func.push_back(str_in);
				}
			}
			flag = 0;
		}
	}
	
	public: void show() {
		for(unsigned int i=0;i<func.size();i++) {
			cout<<func[i].x<<endl;
		}
	}
};	

int main(void) {
	char* filename = "1_test.txt";
	FHandling f1(filename);
	f1.search();
	f1.show();
	return 0;
}
```


----------

