C++: 2d char arrays | File handling

RBX

In the zone
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.

Code:
/**
 *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.
Code:
#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;
}
 
Last edited:

Liverpool_fan

Sami Hyypiä, LFC legend
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.
 
Last edited:
OP
RBX

RBX

In the zone
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.
Code:
#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

Sami Hyypiä, LFC legend
a_file(str);
a_file.open(str);


char func[][80];
Size?

dt_type = arr;

Use strcpy
while(!a_file.eof()) {
use while(a_file)

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

int size = sizeof(func)/sizeof(int);
This will create major problems, seg phaults etc.
Create a class member to store the size perhaps

Tried the strings as well, but the cstring library doesn't seem to work with C++ strings.
Which cstring functionality you want to use?
 
OP
RBX

RBX

In the zone
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.

Code:
#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;
}
 
Last edited:
OP
RBX

RBX

In the zone
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.

Code:
#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;
}
 
Last edited:
OP
RBX

RBX

In the zone
It now works, and needs a bit of finishing.

Code:
#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;
}
 
Top Bottom