21

I have a 2 dimensional matrix:

char clientdata[12][128];

What is the best way to write the contents to a file? I need to constantly update this text file so on every write the previous data in the file is cleared.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
mugetsu
  • 4,228
  • 9
  • 50
  • 79
  • 3
    Is this supposed to be a human-readable text file? – Sergey Kalinichenko Sep 03 '13 at 17:01
  • Is the data numbers? Strings? What's going on here? Can you show an example of the data and what you want the file to look like? – Carl Norum Sep 03 '13 at 17:03
  • Duplicate of: http://stackoverflow.com/questions/4638568/write-2d-array-to-a-file-in-c – sara Sep 03 '13 at 17:04
  • it is strings. example: "4","error_msg","21.1". The file is needed so that I can pull out the data again if needed. So it doesn't have to be human readable in the text file, just when it is pulled out again – mugetsu Sep 03 '13 at 17:47
  • The converse of the problem in [How to read an array saved in binary mode to text file in C?](http://stackoverflow.com/questions/18599320/how-to-read-array-saved-in-binary-mode-to-text-file-in-c) – Jonathan Leffler Sep 03 '13 at 22:00

3 Answers3

40

Since the size of the data is fixed, one simple way of writing this entire array into a file is using the binary writing mode:

FILE *f = fopen("client.data", "wb");
fwrite(clientdata, sizeof(char), sizeof(clientdata), f);
fclose(f);

This writes out the whole 2D array at once, writing over the content of the file that has been there previously.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 4
    You use `FILE *ifp = fopen("client.data", "rb"); fread(clientdata, sizeof(char), sizeof(clientdata), ifp);` and add error checking. – Jonathan Leffler Sep 03 '13 at 17:52
  • 5
    This works across machine types because the data is character data. If it were `int`, say, then the data would not be portable between big-endian and little-endian machines. – Jonathan Leffler Sep 03 '13 at 17:53
  • I am amazed how difficult it was for me to find a good, concise answer to this. Thank you! Thank you, thank you! <3 – Sipty Jun 10 '15 at 14:08
1

I would rather add a test to make it robust ! The fclose() is done in either cases otherwise the file system will free the file descriptor

int written = 0;
FILE *f = fopen("client.data", "wb");
written = fwrite(clientdata, sizeof(char), sizeof(clientdata), f);
if (written == 0) {
    printf("Error during writing to file !");
}
fclose(f);
1

How incredibly simple this issue turned out to be... The example given above handle characters, this is how to handle an array of integers...

/* define array, counter, and file name, and open the file */
int unsigned n, prime[1000000];
FILE *fp;
fp=fopen("/Users/Robert/Prime/Data100","w");
prime[0] = 1;  /* fist prime is One, a given, so set it */
/* do Prime calculation here and store each new prime found in the array */
prime[pn] = n; 
/* when search for primes is complete write the entire array to file */
fwrite(prime,sizeof(prime),1,fp); /* Write to File */

/* To verify data has been properly written to file... */
fread(prime,sizeof(prime),1,fp); /* read the entire file into the array */
printf("Prime extracted from file Data100: %10d \n",prime[78485]); /* verify data written */
/* in this example, the 78,485th prime found, value 999,773. */

For anyone else looking for guidance on C programming, this site is excellent...

Refer: [https://overiq.com/c-programming/101/fwrite-function-in-c/

  • 3
    I know this isn't the most important comment in the world but 1 is **not** a prime number – RGS Jan 16 '19 at 20:53
  • RGS... Correct. By Definition, A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. I just figured I needed to include 1, as the first entry in my array, since it is pivotal to all primes? – user3359049 Jan 20 '19 at 04:46