I'm a noob in C-Programming so bear me and please correct me if I've the wrong approach.
I'm facing problem while reading file character by character in C.
As you can see below,
apparently, my file has 400 characters but when I try to read them character by character they take up to 419 indexes. I guess they're including end-of-line character too. I don't know how to ignore them. I tried using continue
as you can see but I'm unable to ignore them.
Here is my file data
00000000000000000000
00000000000000000000
00000000000000000000
00555555555555555500
00000000000000000300
00000000000000000300
00000000000000000300
00000000000000000300
00000000000000000300
00000000000000000300
00444444444444444300
00400000000000000000
00400000000000000000
00400000000000000000
00400000000000000000
00400000000000000000
00777777777777777700
00000000000000000000
00000000000000000000
00000000000000000000
Here is my code:
FILE *pToFile = fopen("toConvert.txt", "r");
char single;
i = end_color;
while ((single = fgetc(pToFile)) != EOF) {
if (single != '\0') {
continue;
} else {
if (single !='0') {
bitmap[i] = 0x66;
} else {
bitmap[i] = 0xff;
}
i++;
}
}
Note: I'm trying to add "gray (0x66)" Color in bitmap when a character is 0 and "white 0xff" when a character is anything other than zero. In other words, I've to differentiate between 0 and any other character and it's hard and disturbing with end of line character.
The newline character in your file is '\n', while '\0' refers to the null character of an array. So replace the latter with the former, and '!=' to '==' to skip the read-in of the newline character.
FILE *pToFile = fopen("toConvert.txt","r");
// single as int (thanks to Weather Vane comment)
int single;
int i = end_color;
while((single = fgetc(pToFile)) != EOF){
if(single == '\n' ){
continue;
}else{
if(single !='0'){
bitmap[i] = 0x66;
}
else{
bitmap[i] = 0xff;
}
i++;
}
}