Advertisement
theos0o

thelabbbbb1

Apr 18th, 2012
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX_LINE_LENGTH 100
  5. #define ARRAY_A_SIZE 100
  6. #define ARRAY_B_SIZE 100
  7.  
  8. int main(void){
  9.         FILE *fp = fopen("test.txt", "r");
  10.         char lineBuf[MAX_LINE_LENGTH], *tokenPtr = NULL;
  11.         int i = 0, a = 0, b = 0;
  12.         double currentNumber = -1;
  13.         double arrayA[ARRAY_A_SIZE], arrayB[ARRAY_B_SIZE];
  14.         if (!fp){
  15.                 puts("Error opening file.");
  16.                 return EXIT_FAILURE;
  17.         }
  18.         while (!feof(fp)){
  19.                 printf("---------\nLINE %d\n---------\n", ++i);
  20.                 fgets(lineBuf, MAX_LINE_LENGTH, fp);
  21.                 tokenPtr = (char*) strtok(lineBuf, " \n");
  22.                 while (tokenPtr){
  23.                         printf("Read number %f.\n", atof(tokenPtr));
  24.                         currentNumber = atof(tokenPtr);
  25.                         if (1){
  26.                                 puts("Stored number in array A!");
  27.                                 arrayA[a++] = currentNumber;
  28.                         } else {
  29.                                 puts("Stored number in array B!");
  30.                                 arrayB[b++] = currentNumber;
  31.                         }
  32.                         tokenPtr = (char*) strtok(NULL, " \n");
  33.                 }
  34.         }
  35.         fclose(fp);
  36.  
  37.         printf("Printing elements stored in array A (%d)\n", a);
  38.         for (i = 0; i < a; i++){
  39.                 printf("%f\n", arrayA[i]);
  40.         }
  41.  
  42.         printf("\nPrinting elements stored in array B (%d)\n", b);
  43.         for (i = 0; i < b; i++){
  44.                 printf("%f\n", arrayB[i]);
  45.         }
  46.  
  47.         return EXIT_SUCCESS;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement