CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2003
    Posts
    11

    writing to many files

    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
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    628

    Re: writing to many files

    from a quick glance, it seems there are many things wrong with the code..
    - you declare your array in the loop- the array is declared:
    iteration 1, len 0 - illegal
    iteration 2, len 1
    and so on...

    this is wrong. you should declare it outside the loop of whatever size you want.

    -you can't do this: funcf[ct].open (funcfile+ct);
    first of all, you have to have funcfile in quotes, and you can't just say '+'. you should use strcat. you also have to tell it the full name of the file. ie the extension.

    that's all i have time for right now, but there are a zillino other problems that i see..

  3. #3
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    628

    Re: writing to many files

    i just tried to look for a tutorial on how to deal with strings and char * in c++ .. found this: http://cplus.about.com/od/beginnerct.../aa051202a.htm

    havne't really read it.. but you could start there, and if you don't like it just search on google... there are about a billion such tutorials.. it seems you are new to c++... you really should understand how pointers adn stuff work.. it is impossible to write code if you don't.

  4. #4
    Join Date
    Dec 2003
    Posts
    11

    Re: writing to many files

    ok so i fied the array declaration. now about your second statement about the opening of the funcfile+ct part. it will create files that subtract a character each time from the filename. so i named it 'test' and it ouput files named 'test' 'est' 'st' and 't'. so what is the syntax i need to use to make it output the filenames properly? and yes i do need to add the extension to the end of the filename. so when i input a filename such as 'foo.f', i want it to create files named foo1.f, foo2.f and so on. im not sure how to use strcat along with the declaration of the open function for the array. would it be like funcf[ct].open (strcat("funcfile"+ct) or something? thanks for any help.

  5. #5
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    628

    Re: writing to many files

    for strcat:
    http://msdn.microsoft.com/library/de...c_._mbscat.asp

    char * a= new char 10;
    a[0]='hi';
    char * b= " sup";
    strcat (a,b);

    a shoudl now have "hi sup"

    it will create files that subtract a character each time from the filename. so i named it 'test' and it ouput files named 'test' 'est' 'st' and 't'.
    no...

    i suggest your forget about 16 files at the moment. just do what you want for a single file. then add teh rest of them... get back with code and an explanation of what you want to do in each of the files.

    seriously, read something about how strings work in c++... it will really help you...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured