Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX_LINE_LENGTH 100
- #define ARRAY_A_SIZE 100
- #define ARRAY_B_SIZE 100
- int main(void){
- FILE *fp = fopen("test.txt", "r");
- char lineBuf[MAX_LINE_LENGTH], *tokenPtr = NULL;
- int i = 0, a = 0, b = 0;
- double currentNumber = -1;
- double arrayA[ARRAY_A_SIZE], arrayB[ARRAY_B_SIZE];
- if (!fp){
- puts("Error opening file.");
- return EXIT_FAILURE;
- }
- while (!feof(fp)){
- printf("---------\nLINE %d\n---------\n", ++i);
- fgets(lineBuf, MAX_LINE_LENGTH, fp);
- tokenPtr = (char*) strtok(lineBuf, " \n");
- while (tokenPtr){
- printf("Read number %f.\n", atof(tokenPtr));
- currentNumber = atof(tokenPtr);
- if (1){
- puts("Stored number in array A!");
- arrayA[a++] = currentNumber;
- } else {
- puts("Stored number in array B!");
- arrayB[b++] = currentNumber;
- }
- tokenPtr = (char*) strtok(NULL, " \n");
- }
- }
- fclose(fp);
- printf("Printing elements stored in array A (%d)\n", a);
- for (i = 0; i < a; i++){
- printf("%f\n", arrayA[i]);
- }
- printf("\nPrinting elements stored in array B (%d)\n", b);
- for (i = 0; i < b; i++){
- printf("%f\n", arrayB[i]);
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement