1

i have the following struct

struct
{
    char order;
    int row;
    int column;
    int length;
    char printChar;
}record;

and my file look's like this

F
30
40
7
X

how can i use fread to store the file in the struct? does my file appear correctly or should all the components need to be in one-line?

2
  • The file is ok, but if you have an element on each line then it is better to use fgets. Commented Dec 19, 2012 at 18:03
  • 2
    "and my file look's like this"... It doesn't look like a binary file. Is it, in fact, binary? Commented Dec 19, 2012 at 18:04

3 Answers 3

8

If I understand correctly, you're asking if you can do

struct record r;
fread(file, &r, sizeof(r));

or are you forced to use

struct record r;
fread(file, &r.order, sizeof(r.order));

If this is your question, then the answer is: you have to read the fields one-by-one since there may be padding between struct members. Or, if you use a GNU-compatible compiler, you might instruct it not to include any padding by declaring your struct as "packed":

struct record {
    // ...
} __attribute__((packed));

But this is not advised unless absolutely necessary (it's not portable).

Also, is your file really a binary file? If not, you should pay attention to newline characters and converting the numbers from text to their actual numeric value.

Sign up to request clarification or add additional context in comments.

4 Comments

i intend to use a binary file , are there any ways to create a real binary file without fwrite?
@DanielHallel What do you mean by a "real" binary file? Why do you want to avoid using fwrite()?
from what i understand if i open an empty .bin file with notepad and add 30 it will be 2 char's one 3 and the other 0 , but isn't there a way to save 30 in a binary format? oh and i want to avoid using fwrite because my program handles already created binary files so i dont want to create one..
@DanielHallel Now go read its documentation, fwrite() doesn't (re)create the file if you tell it not to do so. By the way, if you're not concerned by endianness problems, just use int i = 30; fwrite(&i, sizeof(i), 1, stream);
1

It is not possible to read from a file in that format (essentially containing the character representations of the data) into the structure. One method for reading it would be to use fgets and read each line and assign the data into the structure (converting numeric values as necessary with functions such as strtol or perhaps atoi if error checking is not as important).

Comments

0

Your file seems to be a text file, so if that's exactly the format of the file, you can use fscanf:

fscanf(file, "%c%d%d%d%c", &(record.order), &(record.row), ...

You can check the return value if you're interested in basic error handling. If you need a better description of the error, just use fgets to read one line at a time and parse it with sscanf, atoi, strtol and similar functions.

If you want to directly save data in the structure, no, you can't (with that kind of file), in a text file 30 is a string of two characters, not an integer in binary form.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.