- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Files of type data in C
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-08-2004 11:59 PM
тАО06-08-2004 11:59 PM
If anyone has an example of this, I would be grateful if you could attach the example with your.
Thanks in advance.
Joseph.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-09-2004 12:28 AM
тАО06-09-2004 12:28 AM
Re: Files of type data in C
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-09-2004 01:15 AM
тАО06-09-2004 01:15 AM
Re: Files of type data in C
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-09-2004 01:33 AM
тАО06-09-2004 01:33 AM
Re: Files of type data in C
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-10-2004 02:38 AM
тАО06-10-2004 02:38 AM
Solution#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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-10-2004 03:22 AM
тАО06-10-2004 03:22 AM
Re: Files of type data in C
Cheers,
Joseph.