Advertisement
mateuspl

memory (bidimensional arrays).c

Jan 11th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. /* Reading a file */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int main() {
  7.     // FILE
  8.     FILE *mainfile;
  9.     mainfile = fopen("file.txt", "r");
  10.     if (!mainfile) { system("pause"); return(0); }
  11.     // end FILE
  12.    
  13.     // VARIABLES
  14.     int qnt /* line breaks */, brli = 10 /* to add to string with realloc */;
  15.     int f, f2; /* any use */
  16.     //end VARIABLES
  17.    
  18.     // MALLOC
  19.     unsigned char *string[brli];
  20.     string = (unsigned char *) malloc(10000 * sizeof(char)); /* Compiler: incompatible types in assignment */
  21.     if (!string) { system("pause"); return(0); }
  22.     // end MALLOC
  23.    
  24.     for (qnt = 0; qnt < 9999; qnt++) { // to each record
  25.         fgets(string[qnt], 9999, mainfile); // get the record
  26.  
  27.         // FREE
  28.         for (f = 0; f < 9999; f++) { // each byte
  29.             if (string[qnt][f] == '\0') { // if byte = line break:
  30.                 while (f < 9999) { // while byte isn't the end of the record
  31.                     f++; // next byte
  32.                     free(string[qnt][f]); /* (freeing byte) Compiler: [Warning] passing arg 1 of `free' makes pointer from integer without a cast */
  33.                 } // freeing all bytes until the end of the record
  34.             }
  35.         } // release all unused bytes
  36.         // end FREE
  37.        
  38.         // REALLOC
  39.         if (qnt == (brli - 1)) { // if have more line breaks than the last allocation or re-allocation:
  40.             brli += 10; // adding more 10 possibles line breaks
  41.             unsigned char *newstr[brli];
  42.             newstr = realloc(string, brli * sizeof(int)); /* Compiler: incompatible types in assignment */
  43.             if (!newstr) { system("pause"); return(0); }
  44.             string = newstr; /* Compiler: incompatible types in assignment */
  45.         }
  46.         // end REALLOC
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement