I am reading a text file titled 'binary' on the C:\ root. In it are several hundred bytes (By that I mean many, many lines of 8-bit binary increments in normal text) that will be read.
It is format is to have each 8 bits typed on it's own line: i.e.
01010100
01101000
01101001
etc...
Here is the code.
CODE
#include <stdio.h>
/*
Read a text file for binary vals. Returns ASCII codes as of now.
*/
int readbi(char bytebi[])
{
int i, res, val;
val = 128;
printf("val is %d\n","val");
for (i=0; i<=8; i++)
{
if (bytebi[i])
{
res += val;
printf("res is now %d\n","res");
}
val /= 2;
printf("val halved to %d\n","val");
}
printf("readbi returning %d\n","res");
return res;
}
int main()
{
FILE *f;
int ret;
char s[10];
f = fopen("C:\\binary.txt","r");
if (!f)
{
printf("Error opening file.\n");
return 1;
}
while (fgets(s,9,f)!= NULL)
{
ret = readbi(fgets(s,9,f));
printf("ret is %d\n","ret");
printf("%d\n",ret);
}
fclose(f);
return 0;
}
/*
Read a text file for binary vals. Returns ASCII codes as of now.
*/
int readbi(char bytebi[])
{
int i, res, val;
val = 128;
printf("val is %d\n","val");
for (i=0; i<=8; i++)
{
if (bytebi[i])
{
res += val;
printf("res is now %d\n","res");
}
val /= 2;
printf("val halved to %d\n","val");
}
printf("readbi returning %d\n","res");
return res;
}
int main()
{
FILE *f;
int ret;
char s[10];
f = fopen("C:\\binary.txt","r");
if (!f)
{
printf("Error opening file.\n");
return 1;
}
while (fgets(s,9,f)!= NULL)
{
ret = readbi(fgets(s,9,f));
printf("ret is %d\n","ret");
printf("%d\n",ret);
}
fclose(f);
return 0;
}
Please note that the several printf's are just a primitive debugging. I'm still learning.
My problem is noted in this screenie.

Noting different to see whenever I scroll about.
Right off the bat, I see that I need to move a line down in the text after each read (do not know how with text functions and \n newline specifically) and find out why readbi's val is being read incorrectly constantly. I figure a pointer comes in somewhere.
I do not know how to figure out ANY of that. That's where my issue stands.
Help is appreciated, as well as advice!

