Solved

transferring arrays into files

Posted on 2001-06-15
6
172 Views
Last Modified: 2010-04-15
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++;
     }
0
Comment
Question by:yabelson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 3

Expert Comment

by:JackThornton
ID: 6196600
(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).

0
 
LVL 1

Author Comment

by:yabelson
ID: 6196643
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.
0
 
LVL 3

Expert Comment

by:JackThornton
ID: 6196662
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:yabelson
ID: 6198647
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?
0
 
LVL 3

Accepted Solution

by:
JackThornton earned 37 total points
ID: 6198712
The standard 'C' library has several useful functions for creating formatted strings from bits and pieces of data. The base function is called "printf", which creates a string as specified and sends it to the standard output device (for console applications, this is typically the screen, but might be another file or device if "piping" is used - but that's another topic). The printf function is one of a small class of functions that take an indeterminate number of parameters - an interesting quirk of 'C'/'C++'. The first parameter is always a format string - this tells the function what to print and how to interpret any following parameters. Examples:

int i = 5;
char *s = "Yet another string.";
printf("Hello World.");    // prints "Hello World"
printf("Hello world - the value of i is %d", i);  // prints "Hello world - the value of i is 5"

// prints "The variable "i" is 5, and I'm printing Yet another string"
printf("The variable \"i\" is %d, and I'm printing %s\n", i, s);  

As you can see, places where substitutions are made for parameters are preceded with a '%', followed by a character which tells the printf function how to interpret the next parameter (d for integer, s for string in the above examples). Each replacement sequence (called a format specification) from left to right consumes the "extra" parameters fed to printf from left to right. Look up the documentation for printf to learn about all the format specifications and modifiers, including integer, unsigned, floating point, strings and hex output. It is a very powerful function for formatting strings.

There are two "sister" functions to printf: fprintf (for printing to "buffered" file output) and sprintf (for creating a composite string). In these functions, the format string is the second parameter; the first parameter for fprintf is the pointer to the FILE to print to; the first parameter for sprintf is the address of the destination string. So, lets look at two of those lines again:

char filename[13];
sprintf(filename, "FILE%04u.dat", i);

The call to sprintf essentially creates a filename for you and places that name into the string variable "filename" (which you see is the first parameter). When 'i' is zero, like the first time through the loop, sprintf will write "FILE0000.dat" into the variable filename. The second time through the loop, when i is one, sprintf will write "FILE0001.dat" into filename. And so on. The format specification "%04u" means to interpret the next parameter as an unsigned integer, print four digits, and pad to the left with 0's as required to make the number four digits wide. I could have just as easily used "%u", in which case it would have created "FILE0.dat", "FILE1.dat", ... "FILE10.dat", etc. - however, it is easier sorting the filenames if they are padded. I'll leave this as an "exercise for the reader" to figure out why.

Hope this helps
0
 
LVL 1

Author Comment

by:yabelson
ID: 6198831
thanks alot.you've helped me very much.i think i can take it on from now.
thanks again.
yair.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question