Take the 2-minute tour ×

I have a structure, that i want to write to a regular file as one record, with comma delimited fields. For example

    struct A {
      int a;
      int b;
   } test;

Given test.a=1 and test.b = 2, the regular file will have a corresponding 1,2 as one line record. Another requirement, is that i want to write the entire structure with one system call. So, i created a char buffer[10], stored the value of int a,b into buffer, with comma delimited and a new line character and used Linux system write() call to write the buffer.

The problem is that the record ends up as binary in the file. Which is intuitive as i took away the "typeness" from the variables, the momment i stored them in a char array. I can use standard io, but i would like to learn, how i can achieve the desired results with standard linux system calls with the outlined constraints. How can i preserve the "typness" of a variable. By typeness i mean that if the variable is of type int, the regular file should have human readable digits.

share|improve this question
Do you mean fprintf(file, "%d, %d\n", a, b);? –  user529758 Jan 14 '13 at 16:56
This doesn't make sense. You're saying that you create a human-readable string (presumably with sprintf or similar), but then when you write() it, the file contains only binary representation of your values? –  Oli Charlesworth Jan 14 '13 at 16:57
The typeness is applied by the human when reading the untyped human-readbale format. To have both (readabilty for humans and typeness for machines) you need to use human-readable format which incorporates the typeness as well. XML with a suitable scheme might help here. Further reading: w3.org/TR/xmlschema11-2 –  alk Jan 14 '13 at 17:02
Why the downvote? This question targets an essential problem, which was around for a long time ... - and hadn't been solved properly before mark-up came in use. –  alk Jan 14 '13 at 17:09
If you want human-readable with types you must explicitly include the types in the formatted output. –  Hot Licks Jan 14 '13 at 17:20
add comment

closed as not a real question by H2CO3, Mark, Soner Gönül, brenjt, Mario Jan 14 '13 at 21:08


3 Answers

up vote 5 down vote accepted

What you are looking to do is something like fprintf(fh, "%d,%d\n", test.a, test.b);, if you open the output file with fopen. If you really must use write() for some reason, you can sprintf(buffer, "%d,%d\n", test.a, test.b); write(fd, buffer, strlen(buffer));.

share|improve this answer
How do fprintf or sprintf achieve the correct result? At the lowest level, i am sure they are using write() also. Do they convert int to its ASCII or similar to make it human readable? –  Jimm Jan 14 '13 at 17:05
sprintf does not because it only writes to a buffer in memory, not to an output file descriptor. fprintf does end up using write() in some way. What matters here is that both are able to translate numbers in binary form to an array of human readable characters. –  whooot Jan 14 '13 at 17:10
It's not what the OP wants, as this way the typness is lost. –  alk Jan 14 '13 at 17:15
He wants ints to appear as human readable numbers in the output file and he sure as hell got it. How is the typeness lost when all you have are comma separated numbers? –  whooot Jan 14 '13 at 17:18
@alk I think you are reading more than there is in the question. –  whooot Jan 14 '13 at 18:40
show 2 more comments

fprintf(fh, "struct:A{int:%d,int:%d}\n", test.a, test.b);

In some environments you might be able to write:

fprintf(fh, "struct:%s{%s:%d,%s:%d}\n", typeid(A).name(), typeid(test.a).name(), test.a, typeid(test.b).name(), test.b);

share|improve this answer
I'm curious. Is this some kind of standard format? –  whooot Jan 14 '13 at 19:46
@whooot - Nope. Just a stab at something that sorta fits the OP's presumed "requirement". –  Hot Licks Jan 14 '13 at 20:18
ok, thank you for the answer. –  whooot Jan 14 '13 at 21:36
add comment

Use fprintf if you want your file to be human readable, writing the file using code like

void writeArrayToFile(struct A* array, int count)
    int i;
    FILE* fp;
    // open file

        fprintf(fp, "%d,%d\n", array[i].a, array[i].b);
share|improve this answer
isnt that standard io? –  Jimm Jan 14 '13 at 16:57
@Jimm It is. And now what? –  user529758 Jan 14 '13 at 16:59
... also it does not preserve the variables types. –  alk Jan 14 '13 at 17:08
@Jimm Can you update your question to show how you format your data before the write call, how your output file is currently structured and how you'd like it to appear please? It sounds like H2CO3, whooot and I may all have misunderstood what you're asking. If many users aren't understanding your question, re-wording it would be useful. –  simonc Jan 14 '13 at 17:08
@alk Depends on your interpretation of the question I guess. I thought the question was just about getting human readable output (as opposed to using write(fp, &myint, sizeof(myint))) –  simonc Jan 14 '13 at 17:11
show 5 more comments
