Operating System - HP-UX
1751977 Members
4559 Online
108784 Solutions
New Discussion юеВ

Re: Files of type data in C

 
SOLVED
Go to solution
Joseph A Benaiah_1
Regular Advisor

Files of type data in C

I need some help, I'm looking write some code in C that will be able to write a file that has 3 or 4 fields to a binary data file. For the example of this, assume that the last field in an integer, the rest are strings.

If anyone has an example of this, I would be grateful if you could attach the example with your.

Thanks in advance.

Joseph.
5 REPLIES 5
Elmar P. Kolkman
Honored Contributor

Re: Files of type data in C

I'm not sure I understand the question... If you mean you want to get data from an ascii file and want to write it in binary version, but it contains strings for 3 of the 4 columns (which would be written as ascii again), I think: WHY?

It would become something like:
int fd;

fd = open("outputfile",O_RDONLY|O_WRONLY|O_CREAT,452);
write(fd,&intcolumn,sizeof(int));

write(fd,asccol[0],strlen(asccol[0]);
write(fd,asccol[1],strlen(asccol[1]);
write(fd,asccol[2],strlen(asccol[2]);

close(fd);

Of course, add error checking and stuff, but this would be about it...
Every problem has at least one solution. Only some solutions are harder to find.
Mark Grant
Honored Contributor

Re: Files of type data in C

Personally, I would consider writing the integers together with pointers to the strings first and then stuffing all the string at the end of the file.
Never preceed any demonstration with anything more predictive than "watch this"
Joseph A Benaiah_1
Regular Advisor

Re: Files of type data in C

Elmar,

To answer your question, the process that I am trying to do is write a small application that creates a binary file, read from it and be able to write to it as well just in the way UNIX uses the wtmp file for example.

The whole purpose is that only the program that I run can read and write to the file.

Cheers,

Joseph.
R. Allan Hicks
Trusted Contributor
Solution

Re: Files of type data in C

If you are looking for quick and dirty. consider using fixed length fields

#define MAX_LENGTH 30
char field1[MAX_LENGTH];
char field2[MAX_LENGTH];
int field3;

fwrite(field1,size_of(field1),1,fh);
fwrite(field2,size_of(field2),1,fh);
fwrite(field3,size_of(int),1,fh);

This wastes space because it will write the data beyond the trailing null and read data beyond the trailing null.

However, reading it back in will allow you to read for length and not have to do a lot of parsing, and the data beyond the trailing null is ignored anyway.

fread(field1,size_of(field1),1,fh);
fread(field2,size_of(field2),1,fh);
fread(field3,size_of(int),1,fh);

If you go the variable length record route, your reading program gets very messy. The idea of the snippet below is.

1.) To read a byte stream and poke the results based on a pointer to the correct field. When you encouter a null, consider it an end of field.

2.)Reset the byte pointer to the beginning of the next field and repeat.

3.)Keep track of the field number and when you encounter the null from the last char field, do a read for length on the integer field.

It takes the form of the following mess...

char *fieldptr;
int field; /* Field Number */
char myByte;

char field1[MAX_LENGTH];
char field2[MAX_LENGTH];
char myByte;
int field3;

fieldptr = field1;
field=1;

while (!feof(fh)){

myByte=fgetc(fh);

*fieldptr=myByte;

fieldptr++;
/* End of char field */
if( myByte==NULL){
switch (field){
case 1:{
fieldptr=field2; /*next */
field=2;
break;
}
case 2:{
fielddptr=(char*)field3;
fread(fieldptr,sizeof(int),1,fh);
field=1;
fieldptr=field1; /* back to 1st*/
break;
}
default:{
fprintf(stderr,"Invalid field\n");
}
}
Anyway you get the general idea. Fixed length is easy and good, but the trade off is space. Space is hopefully, cheap.


-Good Luck
"Only he who attempts the absurd is capable of achieving the impossible
Joseph A Benaiah_1
Regular Advisor

Re: Files of type data in C

Thanks to all of you for you replies. You have given me plenty to work with.

Cheers,

Joseph.