NXC: fprintf also for arrays of int?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: fprintf also for arrays of int?

Post by HaWe »

hi,
is it allowed to use fprintf also for arrays of int?
e.g.

Code: Select all

int values[256];
int fhandle;

fhandle=fopen("myfile.dat", "w"); // file open -write
fprintf(fhandle, "%d", values);   // writes complete array of int?
fclose(fhandle);  // file close
next question:
where do have I do specify the fsize?
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: NXC: fprintf also for arrays of int?

Post by m-goldberg »

In your signature you say: let NXC reach ACDC! -- but here you are trying to push it off the road. In the Standard C library, printf doesn't allow what you are asking for.
Regards, Morton
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: fprintf also for arrays of int?

Post by HaWe »

I don't understand quite right what you want to say:
fprintf is both ANSI C and NXC - my question now is related to NXC.
So is it possible to write arrays of int to a file? (It's not documented in the NXC help)
Or should I use fprint instead to your opinion?
At least using ANSI C with arrays of char it seems to work:

Code: Select all

#include <stdio.h>

main() {
  FILE 	*Ptr;
  char	 Line[256]; // array of char
  
  Ptr = fopen("/tmp/OutputFile", "w");  // Open a file for output  
  while(gets(Line))                     // Get data from stdin  
  {
    fprintf(Ptr, "%s\n", Line);         // Send char array to file   
  }
  fclose(Ptr);
}

afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: fprintf also for arrays of int?

Post by afanofosc »

The implementation of printf and fprintf in the NXC API both support only a single scalar value. They both use the fmtnum enhanced NBC/NXC firmware opcode to format the single value into a string. Neither supports multiple scalar values, strings, or arrays of any kind.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest