Thread: Opening a file in another file

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    60

    Opening a file in another file

    Ok does anyone have any ideas on how to search through a file that contains subfiles and then search through each subfile to get its data. I can't seem to get it to work.
    C++ can hurt.

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Post your code

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    well actually my code is posted under String Streams but since no one is replying i thought i would just ask people directly the problem i have.
    C++ can hurt.

  4. #4
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    maybe findfirst & find next

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    OK - here's a link to the old question for anyone who wants to help.

    And scott, thread bumping's against the rules here.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    How would I allow the class_name to be opened in the for loop?
    it would be data.open(????)
    I can't get this to work.



    Code:
    void read_data(int ID,istream& in)
    {
    	int number = ID;
    	cout<<"Student ID:  "<<number<<endl;
    	fstream data;
    	char class_name[10];
    	int counter = 0;
    	int count = 0;
    	int ident;
    	char mark;
    	while(count<7)
    	{
    		for (int i=0;i<1;i++)
    		{
    			in >>class_name;
    			data.open("class_name.dat");
    			if(data.fail())
    			{
    				cout<<"There is a Bug in the System\n";
    				exit(1);
    			}
    			while (counter <5)
    			{
    				for (int j=0;j<2;j++)
    				{
    					if (j==0)
    					{
    						data>>ident;
    						
    					}
    					else
    					{
    						data>>mark;
    					}
    					if (ident == number)
    					{
    						cout<<class_name<<"	"<<mark<<endl;
    					}
    				}
    
    				counter ++;
    			}
    			
    			data.close();
    		}
    		count++;
    	}
    }
    C++ can hurt.

  7. #7
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    If I understand your question correctly you want to open and close text files inside of that function, the names of those functions will be read in from another file and that name is inputed into a character array, correct???

    If so ....then...

    if you are using a character array as a file name to be opened by an ifstream object then that character array MUST END with a NULL TERMINATED STRING CHARACTER with is: '\0'
    Otherwise that character array will NOT be recognized and considered as a file name!!!!

    what I recommend doing is reading IN the class_name array one by one (character by character) until your delimeter, which I assume is a space ??? or a new line character??? and when you read in your last character and find out that it is a delimeter then you will replace it wih \0 and exit the loop

    for example:

    Code:
    
    char class_name[10];
    
    for(int i =0; i < 10 && class_name[i] != ' '; i++)
    {
           in.get(class_name[i]);
    
           if(class_name[i] == ' ')         // depends what your delimeter is
               break;
    }
    
    class_name[i] = '\0';
    
    // and now you can open that file b/c it has a valid file name...
    // i.e.
    
    data.open(class_name); 
    
    // make sure to close it before you open another file...
    hope that answers your question....

    Regards,

    matheo917

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    thank for the help but i got it to finally work. Although there is still one quirk. It's not a major flaw but it would help to get it fixed. It's getting the + or - sign of the grade through the input. I can only seem to get the letter such as an A instead of an A-.

    Here is the entire code.
    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <sstream>
    #include <vector>
    
    using namespace std;
    
    void read_data(int ID,istream& in);
    int main()
    {
    	cout<<"Welcome User to the Imaginary College Transcript Search & Printout"<<endl;
    	cout<<endl;
    	bool more;
    	while(more)
    	{
    		int ID;
    		vector <int> v(5);
    		for(int i =0;i<v.size();i++)
    		{
    			if (i==0) 
    				v[i] = 11234;
    			else if (i==1) 
    				v[i] = 12547;
    			else if (i==2) 
    				v[i] = 16753;
    			else if (i==3) 
    				v[i] = 21886;
    			else 
    				v[i] = 48159;
    		}
    
    		string answer;
    		fstream text;
    		cout<<"Please Enter the ID number of the Student:"<<"\n";
    		cin>>ID;
    		if ((v[0]==ID)||(v[1]==ID)||(v[2]==ID)||(v[3]==ID)||(v[4]==ID))
    		{
    		}
    		else
    		{
    			cout<<"The Search has Failed to Find Student #"<<ID<< " in the Database."<<endl;
    			exit(1);
    		}
    		text.open("classes.dat");
    		if(text.fail())
    			{
    				cout<<"Houston, We Have a Problem.\n";
    				exit(1);
    			}
    
    		read_data(ID,text);
    		text.close();
    		cout<<"Do You Wish to Print Out Another Transcript ? (y/n)"<<endl;
    		cin>>answer;
    		if(answer == "y")
    		{
    			cout<<endl;
    			continue;
    		}
    		else if (answer == "n")
    		{
    			more = false;
    		}
    		else
    		{
    		             cout<<"You Obviously Don't Go to This College." <<endl;
    					 cout<<"Attempt Answering Properly Next Time."<<endl;
    		             exit(3);
    		}
    	}
    
    
    	
    	return 0;
    }
    
    
    void read_data(int ID,istream& in)
    {
    	int number = ID;
    	cout<<"Student ID:  "<<number<<endl;
    	string filename;
    	string addon = ".dat";
    	string line,line2;
    	string class_name;
    	int counter = 0;
    	int ident;
    	while (counter < 7)
    	{
    		getline(in,line);
    		istringstream instr(line);
    		instr>>class_name;
    		filename = class_name + addon;
    		fstream data;
    		int count = 0;
    		data.open(filename.c_str());
    		if(data.fail())
    		{
    			cout<<"There is a Bug in the System\n";
    			exit(1);
    		}
    		while (count < 5)
    		{
    			getline(data,line2);
    			istringstream instream(line2);
    			instream>>ident;
    			char mark = 0;
    			char ch;
    			instream.get(ch);			
    			if (isspace(ch))
    			{
    				instream>>mark;
    			}
    			
    			else
    			{
    				instream.unget();
    			}
    
    			if(ident == number)
    			{
    				cout<<class_name<<"	"<<mark<<endl;
    				break;		
    			}
    			else
    			{
    				continue;
    			}
    			
    			data.close();
    			count++;
    		}
    		
    		counter ++;
    	}
    	
    }
    Here are the files used:

    Code:
    classes.dat		CSC1.dat		
    CSC1			11234	A-		
    CSC2			12547	B		
    CSC46			16753	B+		
    CSC151			21886	C		
    MTH121			48159	C+		
    PHY50
    CHN1					
    	
    
    CSC151.dat		CHN1.dat			
    	
    11234	C		11234	A		
    12547	C+		12547	B+		
    16753	A-		16753	C+		
    21886	A		21886	B
    
    
    CSC2.dat                                  
    11234	C	
    12547	B
    16753	A+
    21886	B
    48159	B-
    
    
    CSC46.dat		PHY50.dat
    
    11234	D		11234	C-
    12547	C		12547	B
    16753	A+		16753	A-	
    21886	B-		21886	A
    48815	B+		48815	A+
    
    
    MTH121.dat	
    11234	C+
    12547	D
    16753	D-
    21886	B+
    48159	A-
    C++ can hurt.

  9. #9
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    Because you have declared (mark) to a character, ---> one single (and only one) character...

    so it will read it either A or + not both....

    since u're using string ....create a character array of 2 items or another string..........wichever makes you more comfortable..........and then you will be able to read in both...


    ...........
    matheo917

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    thanks a lot my program now runs flawlessly
    Last edited by scottmanc; 02-20-2003 at 11:48 PM.
    C++ can hurt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM