Skip to content

Improvement of stdio.h functions #2715

Open
@shiftleftplusone

Description

@shiftleftplusone

hi,
I would appreciate if the number of stdio.h functions for file access could be improved.
For file access we basically just have (file).print, println, .write, and just .read (by single bytes).
But fprintf and fscanf would be appriciated, too.

E.g.,that's why I just faked fgets() to something which mimics the ANSI function.
It's been programmed quick and dirty, but IMO it's already an improvement.
You may polish it up for your libs and add it for use by SD if you wish.

char * fgets ( char * str, int32_t num, File * stream ) {
  int32_t i;
  i=0;
  strcpy(str, "");
  while ( stream->available() && (i < num)   ) {  
     str[i]=stream->read() ;
     if(str[i] >=  ' ')  ++i;
     else
     if(str[i] == '\0') goto ok; // end of string: \0
     else
     if(str[i] == '\n')   {      // end of string: CR
         //str[i] = '\0';          // optional: overwrite CR by '\0'
         str[++i] = '\0';      // insert additional 0-termination after CR (== ANSI C)
         goto ok;
     }  
  } 
  ok: return(str);
}

function call: 
char sdata[30];
File myFile;
fgets ( sdata, 20, &myFile ); 
or
char * sptr;
sptr = fgets ( sdata, 20, &myFile ); 

I just am trying to mimic more functions using multiple variable parameter lists ( ... ) provided by

#include <stdarg.h>
#include <stdio.h>

e.g.,

char * fprintf(File * stream,  char fmtstr[], ... )

but of course it would be better to access the "real" stdio.h functionality instead of a faked and limited homebrewed one.

ps,
the source code gets always corrupted after uploading it here !! :(

Metadata

Metadata

Assignees

Labels

Component: CoreRelated to the code for the standard Arduino APIfeature requestA request to make an enhancement (not a bug fix)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions