1

i have been able to read an array of bits from a text file using this code:

#define _CRT_SECURE_NO_DEPRECATE
#define WM_size  13056
#include<stdio.h>
int main()
{
char a;
FILE *point;
int i, bits[WM_size];
point = fopen("WM-bits.txt", "r");
for(i = 0; i <WM_size; i++) {
        a = fgetc( point); 
    bits[i] = atoi(&a);              
}
fclose(point);
for(i = 0; i < WM_size; i++) 
printf("%d" , bits[i]);  
getchar();
return  bits;}

what should i do if i don't know the number of bits in the .txt file? i actually want to write a function that returns the bits and length of them as the output.

my textfile is something like this: 0101011110110 ...

zahraesb
  • 153
  • 1
  • 7
  • 3
    You could count them first, allocate memory, rewind the file and read them again. Or you could allocate memory for an array and `realloc()` it when full. – Weather Vane Feb 25 '17 at 18:27
  • 1
    Possible duplicate of [How do you determine the size of a file in C?](http://stackoverflow.com/questions/8236/how-do-you-determine-the-size-of-a-file-in-c) – AJNeufeld Feb 25 '17 at 18:27

0 Answers0