1

I'm basically trying to take a file loaded with 1's and 0's like:

10101000 00000000 01010100
10000000 00000000 00000000
01101000 11111111 00000000

and take those exact boolean numbers into an array in that exact order. I don't have much experience with fscanf and general file I/O with C so its a bit rough. This is what I have so far.

#include <stdio.h>

bool memory[1024];
char file;
char bit;
int i;

int main () {
    file = fopen ("memory.txt", "r");
    while (fscanf (file, "%d", bit) != EOF) {
        i = 0;
        memory[i] = bit;
        ++i;
    }
}

Upon my attempts at compiling this I get:

./stackexample.c:3:1: error: unknown type name ‘bool’    
bool memory[1024];
 ^
./stackexample.c: In function ‘main’:
./stackexample.c:9:10: warning: assignment makes integer from pointer without a cast     [enabled by default]
 file = fopen ("memory.txt", "r");
      ^
./stackexample.c:10:5: warning: passing argument 1 of ‘fscanf’ makes pointer from integer without a cast [enabled by default]
 while (fscanf (file, "%d", bit) != EOF) {
 ^
In file included from /usr/include/features.h:374:0,
             from /usr/include/stdio.h:27,
             from ./stackexample.c:1:
/usr/include/stdio.h:443:12: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘char’
extern int __REDIRECT (fscanf, (FILE *__restrict __stream,
        ^
./stackexample.c:10:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat=]
 while (fscanf (file, "%d", bit) != EOF) {
 ^

I'm not quite sure why its saying unknown type bool and I don't quite understand the warnings about making an integer from a pointer either.

firephyz96
  • 263
  • 2
  • 6
  • 1
    fscanf is overkill. fgets and an loop with `if` for 0, 1 and whitespace is enough. And you´re just using fscanf wrong (missing &) – deviantfan Oct 28 '14 at 20:47
  • 1
    Type `bool` requires `` header (direct name is `_Bool`) and compiler must be C99/C11 compliant. – Grzegorz Szpetkowski Oct 28 '14 at 20:47
  • `bool` is not a type in older C dialects. Feel free to use `unsigned char` if it's not available. `fopen()` returns a `FILE *`; you're assigning it to a `char` for some reason. – Paul Roub Oct 28 '14 at 20:47
  • C programmimng langualge doesn't have bool type: http://stackoverflow.com/questions/1921539/using-boolean-values-in-c – Rotem Varon Oct 28 '14 at 20:48
  • Don´t forget fclose. – deviantfan Oct 28 '14 at 20:48
  • Do you actually want to read single symbols as boolean true or false values? Or do you want to capture the *value* of the 8-bit *binary* numbers textually represented by '1' and '0'? Ie, should your values be 0xa8, 0x00, 0x54, 0x80, 0x00, etc? If so, read one character at a time, check if it is a '1' or '0' and build bytes by shifting your accumulated value to the left and or'ing in the new result. – Chris Stratton Oct 28 '14 at 21:06

3 Answers3

2

C doesn't have bool type - it's C++. If you use C99 standard you can include stdbool.h header and you'll get bool as typedef. This will solve your first problem.

You shouldn't read the file with %d in fscanf(), because you'd get the WHOLE number, for example 10101000. You should either specify width or read data as characters (with %c) - personally I'd go for the second option. Of course you should "scan" into a temporary variable of right type and then copy into your array - that will solve the warning.

Freddie Chopin
  • 8,440
  • 2
  • 28
  • 58
0

With as little modifications as possible:

#include <stdio.h>
#include <stdbool.h>
#include <assert.h>

bool memory[1024];
char file;
char bit;
int i;

int main () {
    FILE * file = fopen ("memory.txt", "r");
    assert(file);
    while (fscanf (file, "%c", &bit) != EOF) {
        printf("%c",bit);
        assert( i < 1024 );
        if ( bit == '1' )
            memory[i++] = true;
        if ( bit == '0' )
            memory[i++] = false;
    }
    fclose(file);
}
igon
  • 3,016
  • 1
  • 22
  • 37
0

You are not defining your file correctly, also instead of using bool, you can use int and just check if it is a 1 or 0 with an if else statement

Also don't forget your fclose statment to close the file.

#include <stdio.h>
#define FILENAME "/your/file/path"
int main () 
{
    int memory[1024];
    int bit;
    int i;
    FILE *myfile; //Define file

    myfile = fopen (FILENAME, "r"); //Open file
    while (fscanf(myfile,"%i",&bit) != EOF) // Read file
    {
        i = 0;
        memory[i] = bit;
       ++i;
    }

    fclose(myfile);
    return 0;
}
nsij22
  • 869
  • 10
  • 20