Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert txt file to binary

Status
Not open for further replies.

murdCa

Technical User
Jun 4, 2003
7
US
Can someone please help me with converting txt file to binary. I have code that nicely opens the txt file reads it and displays it on the screen but I have problems trying to convert that txt file to binary and then appending new lines to it. Can someone please look at it and let me know how to do that. Code and explanation would really be appreciated.

Thanks in advance....


***********************************************************

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

typedef struct human HUMAN;

struct human
{
char name[50];
char gender[10];
int age;
};

int main(void)
{
char c[50],tmpage[8];
FILE *file;
HUMAN *humans;
int num_humans = 0;
int i, j;
char *textFile = &quot;flintstones.DAT&quot;;

file = fopen(&quot;flintstones.DAT&quot;, &quot;r&quot;);


if(file==NULL)
{
printf(&quot;Error: can't open file.\n&quot;);
return 1;
}
else
{
while(fgets(c, 50, file)!=NULL)
{
if(num_humans==0)
{
humans = calloc(1, sizeof(HUMAN));
}
else
{
humans = realloc(humans, (num_humans+1)*sizeof(HUMAN));
}
for(i=0, j=0 ; c!='\t' ; i++, j++)
{
humans[num_humans].name[j] = c;
}
humans[num_humans].name[j] = '\0';

while (c=='\t' || c==' ') i++;
for(j=0 ; c!='\t' && c!=' ' ; i++, j++)
{
tmpage[j]=c;
}
tmpage[j]='\0';
humans[num_humans].age = atoi(tmpage);

while (c=='\t' || c==' ') i++;
for(j=0 ; c!='\0' ; i++, j++)
{
humans[num_humans].gender[j] = c;
}
humans[num_humans].gender[j] = '\0';

num_humans++;
}
fclose(file);

printf(&quot;*************** Current Age *****************\n&quot;);
for(i=0 ; i<num_humans ; i++)
{
printf(&quot;%s\t\t%d\t%s\t \n&quot;, humans.name, humans.age, humans.gender);
}
if(humans!=NULL)
{
free(humans);
}

return 0;
}
}
***********************************************************
 
Can you repost your code between
[code]
[/code]
tags, to prevent things like your [ i ] index from being interpreted as italics
 
Salem,
I tried and still looks the same. Probably I'm doing something wrong I'm not aware off... Please let me know what's the problem or you can just copy and paste the code and run it, looks just fine when I do that(not italics).

Thanks,

DM
 
Your code is broken &quot;as is&quot;, due to the problem I mention

I can see this in the code
Code:
for(i=0, j=0 ; c!='\t' ; i++, j++)
where I'm pretty sure you meant
Code:
for(i=0, j=0 ; c[i]!='\t' ; i++, j++)

That's just the obvious first instance of the board chopping out of your code what it thinks are formatting instructions.

So please post your code again and use the preview button to make sure it looks correct before you submit the post.
 
Salem,
I don't expect you to believe this but my code before submit does look like you pointed out:
for(i=0, j=0 ; c!='\t' ; i++, j++)

At preview it changes to
for(i=0, j=0 ; c!='\t' ; i++, j++)

I tried going around and playing with it and honestly I don't know what I'm doing wrong. The code that I posted works really fine to open txt file; if I had it that way it wouldn't work, I tested it.
That's as much as I can tell you, it's weird and I've never seen something like this before so I guess we'll have to work around it.

BTW: What I really need to do is
1.open txt file I have and convert it to binary.
2.open another txt file and convert that in binary too so now I'll have 2 binary files.
3.open first binary file for append (ab) and open second binary file for read and append lines from first to the second file so my first binary will have all data from first and second binary.
I probably just need help with converting one binary file and I think I can figure the rest out, if you don't mind helping me with that...

Thanks in advance,

DM
PS: I'm sorry about this post thing, I really hope it don't create problems.
 
Ok, I see that the problem stays there...
If you don't mind email me and I'll send you the source
code. dmuran7544@aol.com

Thanks in advance,

DM
 
dlk thanks a lot...
Here is the code that I have... Please help...

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

typedef struct human HUMAN;

struct human
{
char name[50];
char gender[10];
int age;
};

int main(void)
{
char c[50],tmpage[8];
FILE *file;
HUMAN *humans;
int num_humans = 0;
int i, j;
// char *textFile = &quot;textfile.DAT&quot;;


file = fopen(&quot;textfile.DAT&quot;, &quot;r&quot;);

if(file==NULL)
{
printf(&quot;Error: can't open file.\n&quot;);
return 1;
}
else
{

while(fgets(c, 50, file)!=NULL)
{
if(num_humans==0)
{
humans = calloc(1, sizeof(HUMAN));
}
else
{
humans = realloc(humans, (num_humans+1)*sizeof(HUMAN));
}

for(i=0, j=0 ; c!='\t' ; i++, j++)
{
humans[num_humans].name[j] = c;
}
humans[num_humans].name[j] = '\0';

while (c=='\t' || c==' ') i++;
for(j=0 ; c!='\t' && c!=' ' ; i++, j++)
{
tmpage[j]=c;
}
tmpage[j]='\0';
humans[num_humans].age = atoi(tmpage);

while (c=='\t' || c==' ') i++;
for(j=0 ; c!='\0' ; i++, j++)
{
humans[num_humans].gender[j] = c;
}
humans[num_humans].gender[j] = '\0';

num_humans++;
}

fclose(file);

printf(&quot;Text File Data: \n\n&quot;);
for(i=0 ; i<num_humans ; i++)
{
printf(&quot;%s\t\t%d\t%s\t \n&quot;, humans.name, humans.age, humans.gender);
}
if(humans!=NULL)
{
free(humans);
}

return 0;
}
}
 
Ok, now for binary files

Code:
// first, open a file for writing in binary, deleting old file first (if present)
FILE *fp = fopen( &quot;database.bin&quot;,&quot;wb&quot; );

// You can now either write one record at a time
for ( i = 0 ; i < num_humans ; i++) {
    fwrite( &humans[i], sizeof(HUMAN), 1, fp );
}

// or, since you have an array, the whole array in one step!
fwrite( humans, sizeof(HUMAN), num_humans, fp );

// all done
fclose( fp );

To read the binary file, you would do something like this
Code:
HUMAN person;
FILE *fp = fopen( &quot;database.bin&quot;,&quot;rb&quot; );
fread( &person, sizeof(HUMAN), 1, fp );

If you want to later on open the binary file for update (reading and writing), then use this call
Code:
FILE *fp = fopen( &quot;database.bin&quot;,&quot;r+b&quot; );

Finally, one of the nice things about binary files is that the records are all the same size. This makes it easy to implement random access - going to a record of your choice without having to read all the records before it.
Code:
HUMAN person;
int person_num = 10;
FILE *fp = fopen( &quot;database.bin&quot;,&quot;rb&quot; );
fseek( fp, sizeof(HUMAN)*person_num, SEEK_SET );
fread( &person, sizeof(HUMAN), 1, fp );
printf(&quot;%s\t\t%d\t%s\t \n&quot;, person.name, person.age, person.gender);


Some comments on your existing code
Code:
      if(num_humans==0)
      {
        humans = calloc(1, sizeof(HUMAN));
      }
      else
      {
        humans = realloc(humans, (num_humans+1)*sizeof(HUMAN));
      }
You can remove the need for the if/else by initialising your pointer before you start.
Code:
HUMAN *humans = NULL;
...
        humans = realloc(humans, (num_humans+1)*sizeof(HUMAN));
realloc knows that if humans == NULL is a special case, and it won't try and do anything with it.


The more serious problem is what happens if realloc fails. In your code, everything is lost since you overwrite your pointer.
It should go something like this
Code:
    while(fgets(c, 50, file)!=NULL)
    {  
      void *temp;
      temp = realloc( humans, (num_humans+1)*sizeof(HUMAN));
      if ( temp != NULL ) // success
      {
        humans = temp;    // update the proper pointer
      }
      else
      {
        // error - out of memory
        // but all the existing work is still pointed to by humans,
        // so do whatever seems appropriate
      }
 
Salem,
Thanks a lot. I'm gonna try to work this out and let you know how it goes.

Have a good day,

PS: I'll probably post the whole code before the end of the day...

DM
 
Salem,
I'm kinda off of what I want to do... In my previous post I probably should've written....
I have two files...File1 and File2, both txt files.
I want to convert both of them to binary .bin file1.bin and file2.bin.
I want to append file2 to file1. I understand the way that should go, please correct me if I'm wrong....
I should first convert both of the files to .bin (where I have the most problems). Display contents of file1 to the screen.
Then I should open file1 for append, and open file2 for read. Append lines from file2 to file1 and close both files and display contents of file1 after append.
I'm sorry about confusion, I'm really confusing myself at this time :)
I usually don't like bothering people for help but I really need to have this one done.
U probably answered my question in previous post I'm just too confused, please forgive me for that...

Thanks in advance,

DM
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top