In C how would i write an array to a file

In summary, To create, open, and write an array to a text file in C, you can use functions such as fopen(), fwrite(), and fread(). It is important to add a 'b' in the fopen() call when using Windows to specify binary. A good tutorial on C File IO can be found on Wikipedia, and a Google search yields many results. One approach is to create a memory block containing all the data and file header, then writing it to the hard drive.
  • #1
j-lee00
95
0
In C how would i create, open and write an array to a text file

The array is EeV,

does it make a difference if I am using ubuntu or windows
 
Technology news on Phys.org
  • #2
fopen() to open the file fwrite() to dump the data fread() to get it back.
The only differenc eon windows is you have to add a 'b' in the fopen() call to specify binary, ie fopen(filename,"wb") to open a file for writing.
 
  • #3
  • #4
I like to create a memory block containing all the data and the file header, and then just write the whole block to my hard drive.
 

Related to In C how would i write an array to a file

1. How do I declare an array in C?

In C, arrays are declared by specifying the data type of the elements in the array, followed by the name of the array and the number of elements in square brackets. For example, to declare an array of integers with 10 elements, you would write int myArray[10];

2. How do I write an array to a file in C?

To write an array to a file in C, you will need to use the fwrite() function. This function takes four arguments: a pointer to the array, the size of each element in bytes, the number of elements to be written, and a pointer to the file to write to. For example, to write the entire array to a file called output.txt, you could use the following code: fwrite(myArray, sizeof(int), 10, output.txt);

3. How do I read an array from a file in C?

To read an array from a file in C, you will need to use the fread() function. This function takes four arguments: a pointer to the array where the data will be stored, the size of each element in bytes, the number of elements to be read, and a pointer to the file to read from. For example, to read 10 integers from a file called input.txt into an array, you could use the following code: fread(myArray, sizeof(int), 10, input.txt);

4. How do I write a specific element of an array to a file in C?

To write a specific element of an array to a file in C, you will need to use the fwrite() function and specify the index of the element you want to write. For example, to write the third element of the array to a file, you could use the following code: fwrite(&myArray[2], sizeof(int), 1, output.txt); Note that arrays in C are zero-indexed, so the third element would be at index 2.

5. How do I read a specific element of an array from a file in C?

To read a specific element of an array from a file in C, you will need to use the fread() function and specify the index of the element you want to read. For example, to read the fifth element of an array from a file, you could use the following code: fread(&myArray[4], sizeof(int), 1, input.txt); Note that arrays in C are zero-indexed, so the fifth element would be at index 4.

Similar threads

  • Programming and Computer Science
Replies
1
Views
712
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
759
  • Programming and Computer Science
Replies
4
Views
785
  • Programming and Computer Science
Replies
2
Views
507
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
4
Views
537
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
Back
Top