title: C Program Structures Using File Input And Output
body: <p>First C programming class and beginning to learn programming. I am currently learning how to use file input/output in C and this is a learning task to help understand this process.</p>
  
  <p>The task calls for the program to use the an input file with random U.S. addresses and then output the addresses in order from least to greatest by zipcode. As the program stands right now it does not crash, but I get an empty output file. I may have an idea of what is wrong. I believe that I have to tell the program/function when to start reading the input file, then when to write, and lastly when to close the file. Possibly use a 'while' statement somewhere?</p>
  
  <p>What is needed and where to get this program to output the addresses into a file? Can anyone kindly recommend a function/statement that can be added in order to achieve this?</p>
  
  <p>Thank you so very much in advance for your help, time, and guidance in getting this program working!</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define BUFF_SIZE 32
  #define STRUCT_SIZE 512
  
  struct info {
      char name[BUFF_SIZE];
      char stAddress[BUFF_SIZE];
      char cityAndState[BUFF_SIZE];
      char zip[BUFF_SIZE];
  };
  
  void selectionSort(struct info *ptrStruct[], int size);
  
  int main(int argc, char *argv[]) {
  
      FILE *fpin, *fpout;
      int count, size;
      char buffer[512];
      struct info *ptrStruct[STRUCT_SIZE];
  
      if (argc != 3) {
  
          printf("Usage: program, inputfile, outputfile\n");
          exit(1);
      }
  
      if ((fpin = fopen(argv[1], "r")) == NULL) {
  
          printf("Can't open input file\n");
          exit(1);
  
      }
  
      if ((fpout = fopen(argv[2], "w")) == NULL) {
  
          printf("Can't open output file\n");
          exit(1);
      }
  
      for (count = 0; count &lt; STRUCT_SIZE; count++){
          ptrStruct[count] = (struct info*) malloc(sizeof(struct info));
          if (EOF == scanf("%599[^\n]%*c", buffer)){
              free(ptrStruct[count]);
              break;
          };
          strcpy(ptrStruct[count]-&gt;name, buffer);
          scanf("%511[^\n]%*c", buffer);
          strcpy(ptrStruct[count]-&gt;stAddress, buffer);
          scanf("%511[^\n]%*c", buffer);
          strcpy(ptrStruct[count]-&gt;cityAndState, buffer);
          scanf("%511[^\n]%*c", buffer);
          strcpy(ptrStruct[count]-&gt;zip, buffer);
      }
  
      size = count;
      selectionSort(ptrStruct, size);
  
      printf("\n\nLEAST TO GREATEST\n");
      for (count = 0; count &lt; size; count++)
      {
          printf("%s\n", ptrStruct[count]-&gt;name);
          printf("%s\n", ptrStruct[count]-&gt;stAddress);
          printf("%s\n", ptrStruct[count]-&gt;cityAndState);
          printf("%s\n", ptrStruct[count]-&gt;zip);
          free(ptrStruct[count]);
      }
  
  fclose(fpin);
  fclose(fpout);
  
  }
  
  void selectionSort(struct info *ptrStruct[], int size)
  {
      int count1, count2;
      int minIndex;
      struct info *ptrTemporary;
  
      for (count2 = 0; count2 &lt; size - 1; count2++)
      {
          minIndex = count2;
          for (count1 = count2 + 1; count1 &lt; size; count1++)
          {
              if (strcmp(ptrStruct[count1]-&gt;zip, ptrStruct[minIndex]-&gt;zip) &lt; 0)
                  minIndex = count1;
          }
          if (minIndex != count2){
              ptrTemporary = ptrStruct[count2];
              ptrStruct[count2] = ptrStruct[minIndex];
              ptrStruct[minIndex] = ptrTemporary;
          }
      }
  }
  </code></pre>
  
  <p>input used:</p>
  
  <pre><code>A1, A2
  20294 Lorenzana Dr
  Woodland Hills, CA
  91364
  B1, B2
  19831 Henshaw St
  Culver City, CA
  94023
  C1, C2
  5142 Dumont Pl
  Azusa, CA
  91112
  D1, D2
  20636 De Forest St
  Woodland Hills, CA
  91364
  A1, A2
  20294 Lorenzana Dr
  Woodland Hills, CA
  91364
  E1, E2
  4851 Poe Ave
  Woodland Hills, CA
  91364
  F1, F2
  20225 Lorenzana Dr
  Los Angeles, CA
  91111
  G1, G2
  20253 Lorenzana Dr
  Los Angeles, CA
  90005
  H1, H2
  5241 Del Moreno Dr
  Los Angeles, CA
  91110
  I1, I2
  5332 Felice Pl
  Stevenson Ranch, CA
  94135
  J1, J2
  5135 Quakertown Ave
  Thousand Oaks, CA
  91362
  K1, K2
  720 Eucalyptus Ave 105
  Inglewood, CA
  89030
  L1, L2
  5021 Dumont Pl
  Woodland Hills, CA
  91364
  M1, M2
  4819 Quedo Pl
  Westlake Village, CA
  91362
  I1, I2
  5332 Felice Pl
  Stevenson Ranch, CA
  94135
  I1, I2
  5332 Felice Pl
  Stevenson Ranch, CA
  94135
  N1, N2
  20044 Wells Dr
  Beverly Hills, CA
  90210
  O1, O2
  7659 Mckinley Ave
  Los Angeles, CA
  90001
  </code></pre>