ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hi,
i have a C structure, say, with the following contents:
struct abc
{
int x;
char ch[16];
int y;
} mnp[4];
from a program, i get the values of struct abc mnp[1]. i enter the values as:
x = 1;
ch = "abcdef";
y = 100;
i want the values in a file, so i use fprintf. but in the file, i see the output as:
1abcdef100
i there any way i can get the values exactly as the sizes of int and char, like
x should occupy 4 bytes(size of int) in the output file, char ch[] should occupy 16 bytes, and the y should occupy 4 bytes?
i want this because, say, i have a record of all 5 employees and i want to access the details of 4th employee. i must be able to access the details using lseek = (n - 1)*sizeof record.
kev, i saw a few junk characters in the output file like ^@^B etc, i tried opening the outfile with "a+" and by fwrite-ing one member at a time to the file, but the results were the same.
dorian, ur method worked. i didn't know this syntax, i learned something new.
by skywalker27182 i there any way i can get the values exactly as the sizes of int and char, like
x should occupy 4 bytes(size of int) in the output file, char ch[] should occupy 16 bytes, and the y should occupy 4 bytes?
thats what my method did, if you run hexdump on the file produced then you will see something like
the black numbers are offsets from start of file, blue is first integer, red is the 16 byte character array, and green is the second integer. if you want to read the data back into the program then simply declare enough memory and use fread() if you do want your data stored in ASCII form in the file then do as dorian33 said but its a big pain in the ass to read it back in. thats why binary files are so popular.
maybe i didnt get your question right, but ...
when u frprintf, cant you append a '\0' or a '\n' to your variables, and make it look like
Code:
1
abcdef
4
?
then you can read your file in a loop based on '\n' or '\0'.
also, since you have 2 ints there, cant u just put those two in a struct of their own, and then just have the char array as the other element?
i fail to remember, but are nested structs allowed in C (im thinking it is)?
by skywalker27182 so the junk characters won't make any difference?
there not junk characters, they are the ascii representation of your data in binary form.
im not sure why you wish to use open/read/write when you have C-standard streams available but you can use fopen, fread, fwrite, fseek, fclose or their unistd.h alternatives to work with and move through binary files, check out the man pages or any good book on C. if your still stuck with the idea of binary files i'll write you an example program when i get back home on wednesday.
h/w, i have a record of, say, a 100 employees of a company and the details are name, id, salary etc. it becomes a lot easier to open the record file and go to the nth employee using lseek and reading the rest of the line if all lines of the file are of same size. if it isn't i have to hunt for the right employee number, which might be same as the salary. i hope i made it clear
ok here ya go, the file format is quite simple, first it saves the number of records in the file(a 4-byte integer) then its just all the records one after another
code to create the file
Code:
#include <stdio.h>
typedef struct {
int a;
char b[16];
int c;
} D;
int main()
{
FILE *f=NULL;
D *data=NULL;
int n;
int numEmployees=0;
f=fopen("filename","w");
printf("How many records?\n");
scanf("%i", &numEmployees);
fwrite(&numEmployees, sizeof(int), 1, f);
for(n=0;n<numEmployees;n++) {
D temp;
printf("first integer?\n");
scanf("%i", &temp.a);
printf("character string?\n");
scanf("%s", temp.b);
printf("second integer?\n");
scanf("%i", &temp.c);
fwrite(&temp, sizeof(D), 1, f);
}
fclose(f);
return 0;
}
~
code to read the file back
Code:
#include <stdio.h>
typedef struct {
int a;
char b[16];
int c;
} D;
int main()
{
FILE *f=NULL;
int numRecs=0;
int rec=0;
D data;
f=fopen("filename","r");
fread(&numRecs, sizeof(numRecs), 1, f);
while(1) {
printf("file has %i records(0-%i) which one to look at?", numRecs, numRecs-1);
scanf("%i", &rec);
if(rec<0) break;
fseek(f, sizeof(int)+rec*sizeof(D), SEEK_SET);
fread(&data, sizeof(D), 1, f);
printf("first integer %i, character string %s, second integer %i\n", data.a, data.b, data.c);
}
fclose(f);
return 0;
}
~
i havnt programmed in C for a while so ive probably done something wrong but it seems to work. it needs A LOT of error checking code added if you wanted to use it for anything other than demonstration purposes.
instead of using streams you could quite as easily use open, close, read,write,lseek i dont think it makes any difference to the program.
you might consider using mmap() to access your records all the seeking would be done automatically for you and it would make adding/deleting records much easier.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.