writing and reading into a file in C language
NickName:GalaxyVintage Ask DateTime:2015-03-09T14:08:45

writing and reading into a file in C language

Recentl I have started learning file I/O in C like fopen fwrite and stuff like that. I have a question regard writing/reading a struct that has pointers to a file. Let's say the struct looks like this

  struct {
  int len;
  int* data;
  }intarr;

len is the length of the array

data points to a integer array.

Assuming I know what the struct is(so I can read it back later on), I need to write len and data into the file in binary format. But if I only write the struct to the file, only the pointers are saved into the file instead of the content.

My current approach is that I copy the data into a new array,then I write the len and the new array into the file separately.

I am not too sure if my approach is correct or not. If it is correct, how are you supposed to read them back? If not ,what are we supposed to do when we want to write all the content of a struct with pointers into a file so we can read them back later?... I am still new to programming so if I have said anything wrong,please correct me.

Copyright Notice:Content Author:「GalaxyVintage」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28936092/writing-and-reading-into-a-file-in-c-language

More about “writing and reading into a file in C language” related questions

writing and reading into a file in C language

Recentl I have started learning file I/O in C like fopen fwrite and stuff like that. I have a question regard writing/reading a struct that has pointers to a file. Let's say the struct looks like t...

Show Detail

Reading and writing Graph Modeling Language (GML) files

Is there any java API available for reading and writing Graph Modeling Language (GML) files. In fact, I am looking for any popular graph file format --that is supported by some handy graph editor ...

Show Detail

How to use multiple threads for multiple file reading/writing operations in C?

I'm trying to analyse the optimum number of threads required to perform a large file reading/writing operation on multiple files. So how do I proceed with creating multiple threads and assigning each

Show Detail

Reading and writing structures in C

I know that structs in C may not be laid out memory as they are in the code. For example: struct a { short x; int y; }; assuming 2 byte shorts and 4 byte ints, may actually take 8 byte...

Show Detail

Reading and writing binary file in C

Firstly, I read here: reading-binary-files-in-c binary-file-reading-writing-in-c I am trying to read binary file in C. And wrote this code: struct emp { char name[20]; char surname[20]; ...

Show Detail

Binary Reading from and Writing to a File in C

I need some help writing a program in C to reading and writing binary values from and to a file. For this program I do not need the entire contents of the file (2048 bytes) read in, only the first 30

Show Detail

Writing and Reading to and from a binary file C language

I was given an assignment to manage a sort of music store. In the database (which is saved as a .dat file) we have an artists name, and the album. I'm having problems writing and reading the file....

Show Detail

Handling Reading/Writing Files in PHP?

It's been a while since I've touched PHP, and I've been working in C# for a while. I need to do some file reading/writing, but I'm not sure where to start. I've been spoiled by Visual Studio's code-

Show Detail

Binary file- menu writing and reading, in c issue

I have to write a program for writing and reading a binary file. There should be a menu for the choice. The file should have indentification number, an article with name, parameters, etc and a pric...

Show Detail

Reading and Writing from a binary file in C

This question has been asked many times before, but I've been trying this for hours with a variety of methods and I have no clue why this is wrong. I've been writing C structs to a binary file us...

Show Detail