Link to home
Start Free TrialLog in
Avatar of yabelson
yabelson

asked on

transferring arrays into files

i have a code in which i get two files and perform some function on them.afterward i put them into arrays by several parameters,and then i need to get them into files.then i need to print the files,according to users request.
hte problemis that i dont know how many arrays im going to have(i build them whle running the code) and thus i dont know how many files i will need.therefore,i need to create the files during the running of the code,but i need to know their names so that i can pirnt them upon the users request.
here is what i have done so far.i hope i can get it in one shot.
     if((*fb)==(*(fb+1)))
     {
          file[0]=(int*)calloc(1,sizeof(int));
          file[0][0]=*fb;
     }
     else
     {
          file[0]=(int*)calloc(2,sizeof(int));
          file[0][0]=*fb;
          file[0][1]=*(fb+1);
     }

     flag=0;
     i=0;
     while(i<lengthofb)
     {
          for(k=0;k<=flag;k++)                          
          {
               temp=0;
               j=sizeof(file[k])/2;
               while(file[k][temp]<j)                                  
               {
                    if((*(fb+i))==(file[k][temp]))              
                    {                                                      
                         temp1=0;
                         while((file[k][temp1])<j)                    
                         {                                        
                              if((*(fb+i+1))==(file[k][temp1]))    
                              {                                    
                                   temp=-1;                              
                                   break;                                        
                              }      //if                                  
                              temp1++;                                  
                         } //while
                         if(temp==-1)
                              break;

                         if(file[k][temp1]>=j)                          
                         {                                        
                              file[k]=(int*)realloc(file[k],j+1);
                              file[k][j-1]=*(fb+i+1);
                         }   //if
                    } //if
                    temp++;                                      
               }//while
               if(temp==-1)
                    break;

               else
               {
                    temp1=0;
                    while(file[k][temp1]<j)    
                    {                              
                         if((*(fb+i+1))==(file[k][temp1]))          
                         {                                            
                              file[k]=(int*)realloc(file[k],j+1);
                              file[k][j-1]=*(fb+i);
                              temp=-1;
                              break;
                         }   //if
                         temp1++;
                    } //while
                    if(temp==-1)
                         break;

               }//else
          }//for of K ( the files running)
          if(temp!=-1)
          {                                          
               flag++;                                            
               file[k]=(int**)calloc(2,sizeof(int));    
               file[k][0]=*(fb+i);                              
               file[k][1]=*(fb+i+1);                        
          }                                                      
          i+=2;                                                  
     }//while of i.

     k=0;
 
     while(k<flag)
     {
          fopen(k,"a");
          for(j=0;j<(sizeof(file[k]));j++)
               fprintf(k,"%d ",file[k][j]);

          fclose(k);

          k++;
     }
Avatar of JackThornton
JackThornton

(1) Rather than reallocating arrays, you might consider making a linked list of files to be generated.

(2) If you're confident that there won't be name collisions, then you can generate names from the indices, like so:

int i;
char filename[13];
for (i = 0; i < numFiles; ++i) {
    sprintf(filename, "FILE%04u.dat", i);
    FILE *fd = fopen(filename, "wb");
    ...
}
(2a) I don't think your fopen & fprintf as written will work, unless you have a very non-standard C library. The first parameter should not be an integer.

(3) If you are not confident about name collisions, check your OS-specific library to see if it will give you a series of non-colliding filenames (e.g. in MS Windows you can use GetTempFileName).

Avatar of yabelson

ASKER

giving the file names is the excat problem.i need to CREATE these names,so i can show them as a cahrt,and then the user will be avle to pick one of them and the code will print it.
i know i cant put an int as a first parameter,but i didnt know what i CAN do.this IS the reason i sent the question.
p.s.-we are not aloud to use links.
Then look at (2) again - there is code to generate a sequence of filenames from integers. You didn't say if name collisions (e.g. avoiding names of files already on the hard disk) is an issue.

Just so we're clear, there is a limit to how much or how specific help I can give for problems that appear to be homework, which is why I'm framing the answers as things to ponder and/or *short* examples rather than writing any large amount of specific code to solve the problem.
to JackThornton:
a:thanks for your help.these homework are overedue anyway,but i wanted to finish the code anyway to know the way to do it right.the whole code is about 450 lines,and this part is the lightest one.i'm just stuck with this stupid thing.
b:there is not a problem of name-collisions,since the code is supposed to run on a new library,nad i can access the files from wherever i choose.
c:what does the 3rd line(sprintf...)means?i dont know the arguments within the parenthesis("").can you explain how it works?
ASKER CERTIFIED SOLUTION
Avatar of JackThornton
JackThornton

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks alot.you've helped me very much.i think i can take it on from now.
thanks again.
yair.