Thread: Recursively opening new files

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    1

    Recursively opening new files

    Hello, I am trying to write a program but am stuck on one point.

    My program reads in three lines from my main data file, then, it calls a function which once again opens that same datafile with a new name (let's call it data_file2) and reads from that. Under certain conditions, I would like my function to call itself again and open a new version of the same data file (I do not want to lose my position in datafile2) and search through it. When it is done in there, I'd like it to return one level up and continue reading through data_file2 where it left off. I believe that each time the file will be opened with some local name datafile2. However, I get a segmentation fault when I try this. The function itself is attached below, I'd appreciate any help. I'm still mostly a begginer in the world of programming.

    Code:
    void find(int numbertofind)
    
    {	int number1, type, number2, type2, number3, type3, length, length2, length3;
    
    	int cont2, cont3, cont;
    
    	double x, y, z, x2, x3, y2, y3, z2, z3;
    
    	FILE *data_file2;
    
    	
    
    	data_file2 = std::fopen("positions_hook23_e5_polymers", "r");
    
    	
    
    	while(fscanf(data_file2, "%d %d %lf %lf %lf %d %d\n", &number1, &type, &x, &y, &z, &length, &cont)!= EOF)
    
       {fscanf(data_file2, "%d %d %lf %lf %lf %d %d\n", &number2, &type2, &x2, &y2, &z2, &length2, &cont2);
    
    	fscanf(data_file2, "%d %d %lf %lf %lf %d %d\n", &number3, &type3, &x3, &y3, &z3, &length3, &cont3);
    
    	if (used[number3]==0){
    
    	   if (number1 == numbertofind){
    
    		fprintf(output_file2,"%d %d %lf %lf %lf %d %d\n", number1, type, x, y, z, length, cont);
    
    		fprintf(output_file2,"%d %d %lf %lf %lf %d %d\n", number2, type2, x2, y2, z2, length2, cont2);
    
        	fprintf(output_file2,"%d %d %lf %lf %lf %d %d\n", number3, type3, x3, y3, z3, length3, cont3);   
    
    		used[number3]==1;
    
    		if(cont2>1){find(number2);}
    
    									}
    
    	if (number2 == numbertofind){
    
    		fprintf(output_file2,"%d %d %lf %lf %lf %d %d\n", number1, type, x, y, z, length, cont);
    
    		fprintf(output_file2,"%d %d %lf %lf %lf %d %d\n", number2, type2, x2, y2, z2, length2, cont2);
    
        	fprintf(output_file2,"%d %d %lf %lf %lf %d %d\n", number3, type3, x3, y3, z3, length3, cont3);   
    
    		used[number3]==1;
    
    		if(cont>1){find(number1);}
    
    									}
    
    }
    
    }
    
    fclose(data_file2);

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    First of all, you're confusing C and C++ code together. Pick one or the other. std is a namespace, which is all C++.

    Secondly, I think you're totally confusing your program up for youself. It's not indented properly, and hard to follow. Also, it doesn't sound like your algorithm is the simplest it can be. Think about what it is you're really trying to accomplish by the end of the project.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If it is very recursive, then you're going to run out of file handles pretty quickly.

    If you can read the whole file into memory, then search it, it will be much more efficient.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opening ASCII files in C?
    By Mavix in forum C Programming
    Replies: 6
    Last Post: 04-25-2007, 02:23 PM
  2. Need help opening a series of files
    By ramparts in forum C Programming
    Replies: 9
    Last Post: 11-14-2006, 05:49 PM
  3. Opening files with UNICODE file names
    By decohk in forum Linux Programming
    Replies: 2
    Last Post: 11-09-2006, 05:25 AM
  4. opening files
    By angelic79 in forum C Programming
    Replies: 3
    Last Post: 10-19-2004, 06:52 AM
  5. Opening files - Giving options?
    By wwwGazUKcom in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2001, 07:06 AM