Thread: array of ofstreams

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    4

    array of ofstreams

    ok i need to make an array of outfile file streams that will make about 16 different files. the files need to be named like funcfile0, funcfile1, funcfile2, etc. all the way to funcfile15. the name of the 'funcfile' part will be a char array input from the user. so i set char funcfile[15]; and then cin >> funcfile.

    so my question is creating an array of ofstream that will open the desired files. once i have created all 16 of the files, i need to write different data to each of the files. i currently have a for loop set up to change the file that it writes to and it will increment the file name after writing each file.

    here is what my code looks like now and it will compile but when i run it, it will crash when its writing the first file.

    Code:
    for(int ct=0;ct<16;ct++){
        ofstream funcf[ct];
        funcf[ct].open (funcfile+ct);
        if(funcf[ct].is_open()){
        cout << "writing function file" << ct<< " ..."<<endl;
        int c1=0;
        funcf[ct] << setw(10)<<idim<<setw(10)<<jdim<<setw(10)<<kdim<<setw(10)<<"6"<<endl;  
        for(int e3=0;e3<idim;e3++){
                for(int e2=0;e2<jdim;e2++){
                        for(int e1=0;e1<kdim;e1++){
        if(c1 == 3){funcf[ct]<<endl;c1=0;}
        funcf[ct] << xmagE[e1][e2][e3]*sin(pi*xphsE[e1][e2][e3]/180.0+ct*pi/8.0)<<"       "; c1++;}}}
        c1=0;
        for(int e3=0;e3<idim;e3++){
                for(int e2=0;e2<jdim;e2++){
                        for(int e1=0;e1<kdim;e1++){
        if(c1 == 3){ funcf[ct]<<endl;c1=0;}
        funcf[ct] << ymagE[e1][e2][e3]*sin(pi*yphsE[e1][e2][e3]/180.0+ct*pi/8.0)<<"       "; c1++;}}}
        c1=0;
        for(int e3=0;e3<idim;e3++){
                for(int e2=0;e2<jdim;e2++){
                        for(int e1=0;e1<kdim;e1++){
        if(c1 == 3){ funcf[ct]<<endl;c1=0;}
        funcf[ct] << zmagE[e1][e2][e3]*sin(pi*zphsE[e1][e2][e3]/180.0+ct*pi/8.0)<<"       "; c1++;}}}    
        c1=0;
        for(int e3=0;e3<idim;e3++){
                for(int e2=0;e2<jdim;e2++){
                        for(int e1=0;e1<kdim;e1++){
        if(c1 == 3){ funcf[ct]<<endl;c1=0;}
        funcf[ct] << xmagH[e1][e2][e3]*sin(pi*xphsH[e1][e2][e3]/180.0+ct*pi/8.0)<<"       "; c1++;}}}
        c1=0;
        for(int e3=0;e3<idim;e3++){
                for(int e2=0;e2<jdim;e2++){
                        for(int e1=0;e1<kdim;e1++){
        if(c1 == 3){ funcf[ct]<<endl;c1=0;}
        funcf[ct] << ymagH[e1][e2][e3]*sin(pi*yphsH[e1][e2][e3]/180.0+ct*pi/8.0)<<"       "; c1++;}}}
        c1=0;
        for(int e3=0;e3<idim;e3++){
                for(int e2=0;e2<jdim;e2++){
                        for(int e1=0;e1<kdim;e1++){
        if(c1 == 3){ funcf[ct]<<endl;c1=0;}
        funcf[ct] << zmagH[e1][e2][e3]*sin(pi*zphsH[e1][e2][e3]/180.0+ct*pi/8.0)<<"       "; c1++;}}} 
        }   
        
        funcf[ct] << endl;
        funcf[ct].close();
    }

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Code:
    for(int ct=0;ct<16;ct++){
        ofstream funcf[ct];
        funcf[ct].open (funcfile+ct);
        if(funcf[ct].is_open()){
    I bet funcfile is a character pointer. If so, then you can't do + to append an integer on the end. That actually increments the character pointer.

    Also, your declaration of ofstreams should be outside your for loop. Look at what happens on your first pass through.
    Code:
    ct = 0;
       ofstream funcf[0]; //a zero-member array...sounds dangerous
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    ahh ok yeah you're right that seemed to get the ball rolling but it will crash when writing the third file. and looking at the files that it creates. it will just take the name of the file and subtract a character. so i input the output file to test and it would creat 'test', 'est', 'st', 't' and then crash. so you're saying that has to do with when i was creating the type for the funcfile name.
    i did char funcfile[15];
    how would i fix this so it creates the correct file names? use a string?

    here is my updated code:

    Code:
    ofstream funcf[15];
        for(int ct=0;ct<16;ct++){
        funcf[ct].open(funcfile+ct);
        if(funcf[ct].is_open()){
        cout << "writing function file" << ct<< " ..."<<endl;
        int c1=0;
        funcf[ct] << setw(10)<<idim<<setw(10)<<jdim<<setw(10)<<kdim<<setw(10)<<"6"<<endl;  
        for(int e3=0;e3<idim;e3++){
                for(int e2=0;e2<jdim;e2++){
                        for(int e1=0;e1<kdim;e1++){
        if(c1 == 3){funcf[ct]<<endl;c1=0;}
        funcf[ct] << xmagE[e1][e2][e3]*sin(pi*xphsE[e1][e2][e3]/180.0+ct*pi/8.0)<<"       "; c1++;}}}
        c1=0;
        for(int e3=0;e3<idim;e3++){
                for(int e2=0;e2<jdim;e2++){
                        for(int e1=0;e1<kdim;e1++){
        if(c1 == 3){ funcf[ct]<<endl;c1=0;}
        funcf[ct] << ymagE[e1][e2][e3]*sin(pi*yphsE[e1][e2][e3]/180.0+ct*pi/8.0)<<"       "; c1++;}}}
        c1=0;
        for(int e3=0;e3<idim;e3++){
                for(int e2=0;e2<jdim;e2++){
                        for(int e1=0;e1<kdim;e1++){
        if(c1 == 3){ funcf[ct]<<endl;c1=0;}
        funcf[ct] << zmagE[e1][e2][e3]*sin(pi*zphsE[e1][e2][e3]/180.0+ct*pi/8.0)<<"       "; c1++;}}}    
        c1=0;
        for(int e3=0;e3<idim;e3++){
                for(int e2=0;e2<jdim;e2++){
                        for(int e1=0;e1<kdim;e1++){
        if(c1 == 3){ funcf[ct]<<endl;c1=0;}
        funcf[ct] << xmagH[e1][e2][e3]*sin(pi*xphsH[e1][e2][e3]/180.0+ct*pi/8.0)<<"       "; c1++;}}}
        c1=0;
        for(int e3=0;e3<idim;e3++){
                for(int e2=0;e2<jdim;e2++){
                        for(int e1=0;e1<kdim;e1++){
        if(c1 == 3){ funcf[ct]<<endl;c1=0;}
        funcf[ct] << ymagH[e1][e2][e3]*sin(pi*yphsH[e1][e2][e3]/180.0+ct*pi/8.0)<<"       "; c1++;}}}
        c1=0;
        for(int e3=0;e3<idim;e3++){
                for(int e2=0;e2<jdim;e2++){
                        for(int e1=0;e1<kdim;e1++){
        if(c1 == 3){ funcf[ct]<<endl;c1=0;}
        funcf[ct] << zmagH[e1][e2][e3]*sin(pi*zphsH[e1][e2][e3]/180.0+ct*pi/8.0)<<"       "; c1++;}}} 
        }   
        
        funcf[ct] << endl;
        funcf[ct].close();
    }

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    ofstream funcf[15];

    I thought you wanted 16 files, not 15.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    yes i do want 16 files but i thought i could use element 0-15 to get 16 files. anyways the real trouble im having is the naming of the files.
    funcf[ct].open(funcfile+ct);
    that line is incorrect i think. the program names the files in an odd way that will subtract a character from the name each time it iterates through them. how do i write that statement so it will create the filename properly? funcfile is a char array. do i need to use some special function?

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by quick
    it will just take the name of the file and subtract a character. so i input the output file to test and it would creat 'test', 'est', 'st', 't' and then crash.
    Heh, you mean like I said here:
    Quote Originally Posted by pianorain
    I bet funcfile is a character pointer. If so, then you can't do + to append an integer on the end. That actually increments the character pointer.
    Yes, I would use strings and an ostringstream. Consider this:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using std::cout;
    using std::endl;
    using std::string;
    using std::ostringstream;
    
    int main()
    {
    	string a("awesome");
    	ostringstream ost;
    	ost << a << 10;
    	cout << ost.str() << endl;
    	return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    ok so how do i append the file number on the end of the string?
    so will create string funcfile;
    have the use input the name of the funcfile.
    so then when i go to open the files, do i just do something like:
    funcf[ct].open ("funcfile"+ct); or what? im still unsure how to append the filenumber.

    thanks

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    In the above code example, I'm appending 10 onto awesome, resulting in "awesome10". Try a similar approach.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Banned
    Join Date
    Jun 2005
    Posts
    594
    how bout this, i dont understand why you need an array?

    Code:
    string filename;
    		stringstream ss;
    		for(int i = 1; i != 10001; i++)
    		{
    			ss << "FILENAME" << i << ".txt" << flush;
    			ss >> filename;
    			file1.open(filename.c_str(), ios::out);
    			if(file1.is_open())
    			{
    				file1 << "I smoke crack!" << endl;
    				file1.close();
    			}
    			filename.clear();
    			ss.clear();
    		}
    Last edited by ILoveVectors; 08-12-2005 at 10:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM