-1

I'm working with the open-source thermal simulator HotSpot 6.0, I seek to extract the dimensional parameters -x and -y onto a text file to use for a new thermal modeling tool.

I've tried terminal printing x and y locations:

fprintf(stdout, "Location in x: %u\n", i1);
fprintf(stdout, "Location in y: %u\n", j1);

The above code prints lines of i1 and j1 numbers, but I would like to have them saved to a text file. The code below is my attempt at printing to a text file.

FILE *fptr;
fptr = fopen("Dimensions.txt","w");
fprintf(fptr,"%u\t %u\t",i1,j1);
fclose(fptr);

The output of the code above prints only one line of code. I would like to know why this is to fix it.

1 Answers1

0

Here is an improvement suggestion:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  unsigned i1 = 101;
  unsigned j1 = 202;

  FILE *fptr = fopen("Dimensions.txt", "w");
  if (fptr == NULL) {
    fprintf(stderr, "Can not open/create file\n");
    exit(EXIT_FAILURE);
  }
  fprintf(fptr, "%u %u\n", i1, j1);
  fclose(fptr);
  return EXIT_SUCCESS;
}
Bo R
  • 2,334
  • 1
  • 9
  • 17
  • What does it improve? – Eugene Sh. Jul 16 '19 at 16:19
  • 1
    Er, why be cute with the comma operator in that error check and not a more usual `if (fptr == NULL) { fprintf(stderr, "Can not open/create file\n"); exit(EXIT_FAILURE); }`? – Shawn Jul 16 '19 at 16:38
  • @EugeneSh. The question was different when I answered it (it used two `printf`). Now I'm a bit confused as to what the question asker wants. – Bo R Jul 16 '19 at 20:38
  • @Shawn It was a bit hasty to my local formatter to not use so many lines. I'll adjust. – Bo R Jul 16 '19 at 20:39