Hello all,
I am new and I was hoping someone could please help me out.
My problem is, I am trying to design a C program to read in a text file containing values, and output these values in binary form.

the text file "values.txt" looks like this:

139 <tab> 0 <tab> 0 <tab> 1 <tab> 0 <tab> 0 <\n>
139 <tab> 0 <tab> 0 <tab> 1 <tab> 0 <tab> 0 <\n>
139 <tab> 0 <tab> 0 <tab> 1 <tab> 0 <tab> 0 <\n>

it is built from 6 values spaced by tabs, and I need to write these to a binary file and in binary values (139 = 00110001).

Here is what I have done so far:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *file, *output; //Declare a FILE pointer
    file = fopen("Data/Values.txt", "r");//Open file for access
    output = fopen("Data/output", "wb"); //Create a binary file for writing


    unsigned int a,b,c,d,e,f;// values for each of the sections


    if (file!=NULL)
    {
        printf("File opened successfully..\n\nProcessing..\n\n");

        while (!feof(file))
        {
                fscanf(file,"%d %d %d %d %d %d",&a,&b,&c,&d,&e,&f);

                fprintf (output, "%u%u%u%u%u%u",a,b,c,d,e,f);
        }
        //shutdown streams and sys
        fclose (file);
        fclose (output);
        return 0;
    }
    else//Catch for null pointer
    {
        printf("Error: can't open/find file! \n");
        return 0;//exit
    }
}

I never posted a problem in my life so I prob encased this code wrong too.

my two questions are,

1) How can I make a new line within my "output" file after six values are written?

2) What would be the best way to convert these values to binary before I writ to bin output?

Any help would be very much appreciated! and thank you in advance. =]

I need to write these to a binary file and in binary values (139 = 00110001).

139 in binary is 10001011. Are you using a different way of encoding the value as binary? I can show you how to get the bits of a value, but that won't help if you're limited to 6 bits and the binary string is different from the actual bits of the value.

Hi Tom,
Sorry that was my mistake, your value is correct =]

thanks

There is still a problem. Your format only allows 5 bits, but the value has 8.

how do I ammend to provide 8 bits?

I never posted a problem in my life so I prob encased this code wrong too.

you did much better than 95% of the new posters that can't be bothered to understand the forum before posting. :)

1) How can I make a new line within my "output" file after six values are written?

Simply output a newline after the six values - \n

2) What would be the best way to convert these values to binary before I writ to bin output?

Not sure what you mean -- based on the input you gave. If all you want is to break the 139 into binary, use the the && operator to test the last bit of the number, and >> to shift the second to last bit into the last bit position. Repeat until done.

Question for you:
What are the digits and tabs after the number in the input for? What do they mean?

Also, read this about using feof(). You're not using it properly.

I would just add more bits to the format if that's possible:

139 <tab> 1 <tab> 0 <tab> 0 <tab> 0 <tab> 1 <tab> 0 <tab> 1 <tab> 1 <\n>

You can extract the bits of a value like this:

#include <stdio.h>

void ExtractBits(unsigned long value, 
                 unsigned char bits[],
                 int sz)
{
    int x, y = 0;

    for (x = sz - 1; x >= 0; --x)
    {
        bits[y++] = ((value >> x) & 1) ? 1 : 0;
    }
}

int main()
{
    unsigned char bits[8];
    int x;

    ExtractBits(139, bits, 8);
    for (x = 0; x < 8; ++x) printf("%c", bits[x] + '0');
    puts("");

    return 0;
}

TopGun and WaltP I really appreciate your work!

Thank you so much, I now have the desired output xD

also just to note, I wrote "<tab>" just to symbolise the tabbed spacing within the text file I was accessing.

I noticed I still got alot of learning when using c, especially the feof() as you mentioned.

Again thank you for all your help, I much appreciate it ! =D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.