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. =]