Replace function argument on call

how can i call this funtion and replace text file in any files i want to read? Making another funtion for a file to read seem to affect memory in arduino

void count5_read(){
      df = Sd.open("count5.txt" ,FILE_READ);
      while(df.available()){
      line =df.readStringUntil('\n');
      Siz5++;
      }
   df.close();
   }

First, welcome to the Forum and thank you for taking the time to see how to use code tags when posting your code! Second, if you place your cursor in the IDE's source code window and press Ctrl-T, it will reformat your code for you into a common C coding style. Third, look for some examples on using the Serial object's readBytesUntil() method. This function would allow you to type in a file name in the Serial monitor, store it in a char array, and then pass that array to your function. As a start:

void setup()
{
   Serial.begin(9600);
}

void loop()
{
   char fileToRead[20];
   int charsRead;

   if (Serial.available) {
      charsRead = Serial.readBytesUntil('\n', fileToRead, sizeof(fileToRead) - 1);
      fileToRead[charsRead] = '\0';
      if (strlen(fileToRead) > 0) {
         count5_read(fileToRead);
      }
   }
}

void count5_read(char fileName[])
{
     // Your code with minor changes...
}

I have not tested the code, but it should be close. The key is the readBytesUntil() method of the Serial object. The code says to read bytes until a newline character ('\n') is read, and place all of the characters read into a char array named fileToRead[]. However, don't read more that 19 characters (i.e., the array size minus room for the string termination character '\0'). The code then converts what you typed in to a null-terminated string array and passes it to your function.

Thanks for quick reply, i have 4 .txt files stored in my sd card , i want this single function to call and change only the former txt file to lessen my codes, but i cant still figure out, it just ok to create another 3 void function with specific txt files to read in it but i think it affects memory and performance, at this moment i have 68% usage memory thats why i have to lessen my code... thanks for your time reading my codes, im still a noob in arduino

No. Just call your function with the name of the file you wish to read:

count5_read("ReadThisFile.txt");

This passes the name of the file to your file read function, count5_read(). You may want to read some of the online C tutorials that are available. Search "Arduino C programming tutorial". I just did that and got 11.5 million hits! If you want a book, I can suggest one for you!

Thanks for the time spare, i have tested the code it says cannot convert hardware serial available,

Can i use " constant char " or String to replace this argument directly?

Void count5_read(){
df = SD.open(“count5.txt” , FILE_READ) //wish to replace count5.txt to any.txt

i have tested the code it says cannot convert hardware serial available,

//if (Serial.available)
if (Serial.available())

thanks to both of you, it compiles now , but it doesnt print any on serial monitor.
this is my code :

#include <SD.h>
int siz5=0;
String line;
File df;
void setup()
{
   Serial.begin(9600);
   SD.begin();
   char fileToRead[20];
   int charsRead;

   if (Serial.available()) {
      charsRead = Serial.readBytesUntil('\n', fileToRead, sizeof(fileToRead) - 1);
      fileToRead[charsRead] = '\0';
      if (strlen(fileToRead) > 0) {
         count5_read(fileToRead);
      }
   }
  count5_read("count5.txt"); 
}

void loop()
{
   
}

void count5_read(char filename[])
{
while(Serial.available()){
  df= SD.open("",FILE_READ);
      line =Serial.readStringUntil('\n');
      siz5++;
      Serial.print(siz5);
      Serial.print(line);
     // Your code with minor changes...
}
}
  df= SD.open("",FILE_READ);

You don't provide a filename. I think you were supposed to say:

  df= SD.open(filename,FILE_READ);

String filename ="sample.txt";

void setup(){
count5_read("sample.txt");    // "sample.txt"  should be my filename but i just cant figure it out
}
void loop(){

}

void count5_read(char filename[])
{
while(Serial.available()){
  df= SD.open("",FILE_READ);   // thats why i put this double qoutes (  "" )
  df= SD.open(filename,FILE_READ);   // adding String parameters doesnt work either.
  line =Serial.readStringUntil('\n');
  siz5++;
      Serial.print(siz5);
      Serial.print(line);
     // Your code with minor changes...
}
}

my problem is, i have this four txt files

count5.txt
count3.txt
data1.txt
data2.txt

i want this single void funtion to be called each time using the 4 txt files above :

#include <SD.h>
void count5_read(){
      df = Sd.open("count5.txt" ,FILE_READ);// replacing "count5.txt to any of the 4 txt files above
      while(df.available()){
      line =df.readStringUntil('\n'); //getting the nth line
      Siz5++;                            // counting the lines
      }
   df.close();
Serial.println(line);    // printing the words in line
Serial.println(siz5);   // printing the number of lines
   }

i want this single void function to be called each time using the 4 txt files above :

void count5_read(){
      df = Sd.open("count5.txt" ,FILE_READ);// replacing "count5.txt to any of the 4 txt files above
      while(df.available()){
      line =df.readStringUntil('\n'); //getting the nth line
      Siz5++;                            // counting the lines
      }
   df.close();
Serial.println(line);    // printing the words in line
Serial.println(siz5);   // printing the number of lines
   }

Does this function do what you want with the file "count5.txt"?

Is your issue that you need one of the specified files below to be selected by serial input from a user and the serial monitor?

count5.txt
count3.txt
data1.txt
data2.txt

I have already make 4 void functions but this is memory consuming to arduino

  1. void data(){
    df =SD.open(“count5.txt”,FILE_READ)
    // some codes
    }

  2. void data(){
    df =SD.open(“count3.txt”,FILE_READ)
    // some codes
    }

  3. void data(){
    df =SD.open(“data1.txt”,FILE_READ)
    // some codes
    }.

  4. void data(){
    df =SD.open(“data2.txt”,FILE_READ)
    // some codes
    }

as of now i used 71% of memory
I cant figure out on how to call that single void function using each of any 4 txt file at a time so that i can delete 3 funtions to lessen my code
This 4 functions does what i want with the file
But 71% of used memory is not good coz i still have parts to add into it

Your last post already gives you a hint.
expand on that a bit...

void getSDdata(char *filename) {
  df = SD.open(filename, FILE_READ);
  // Some code
}

char fileToOpen[11];
void loop() {
//some code
strcpy(fileToOpen, "count5.txt");
getSDdata(fileToOpen);
// Some other code
}

just make sure that fileToOpen is large enough to fit the longest filename + 1 character

is this a right approach?
still i cant see any data in my serial monitor

void getSDdata(char *filename) {
  df = SD.open(filename, FILE_READ);
  while(df.available()){
      line =df.readStringUntil('\n');
       siz5++;
  Serial.println(siz5);   
  Serial.println(line);
  
}
}
char fileToOpen[100];
void loop() {
strcpy(fileToOpen, "count5.txt");
//getSDdata(fileToOpen);   // un commenting any 
getSDdata("count5.txt");   // of this two
Serial.println(line);

That's the correct approach.

Why you're not getting anything on your teminal, I couldn't guess without seeing the entire sketch and the contents of the file that you're opening.

Does the file contain data?

A good idea is to also check to make sure that the file opened eg:

  ...
  df = SD.open(filename, FILE_READ);
  if (df != NULL) {                // Did the file open?
    // do stuff with file
  }
  else {
    // do some error handling, or write a message to the screen
  }
  ...

All files have data on it, i guess i have to stick on my 4 void functions though its using 71% memory, maybe someday i can figure it out... thanks to all

Max2p:
All files have data on it, i guess i have to stick on my 4 void functions though its using 71% memory, maybe someday i can figure it out… thanks to all

Don’t go off in a huff

Try this program

#include <SPI.h>
#include <SD.h>

File theFile;

void setup()
{
  Serial.begin(115200);
  Serial.print("Initializing SD card...");
  if (!SD.begin(4))
  {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  getSDdata("test_1.txt");
  getSDdata("test_2.txt");
  getSDdata("test_3.txt");
  getSDdata("test_4.txt");
}

void getSDdata(char *filename)
{
  Serial.print("Reading from ");
  Serial.println(filename);
  theFile = SD.open(filename, FILE_READ);
  while (theFile.available())
  {
    Serial.print((char)theFile.read());
  }
  Serial.println();
}

void loop()
{
}

Obviously you will need to change the pin used CS pin (pin 4 above) to suit your wiring, substitute your own filenames in the calls to the getSDdata() function and use whatever baud rate suits you, but try it and let us know how you get on.

If it does not work for you then please post your version of it here.

This is my best guess at what you want to do:

#include <SD.h>


void setup()
{
  Serial.begin(9600);


  SD.begin();


  file_read("count5.txt");
  file_read("count3.txt");
  file_read("data1.txt");
  file_read("data2.txt");
}


void loop()
{
}


void file_read(const char *filename)
{ 
  Serial.print("Opening ");
  Serial.println(filename);


  File df = SD.open(filename , FILE_READ);
  if (df)
  {
    String line;
    while (df.available())
    {
      line = df.readStringUntil('\n');
      Serial.println(line);
    }
    Serial.print("Closing ");
    Serial.println(filename);
    df.close();
  }
  else
  {
    Serial.print("Failed to open ");
    Serial.println(filename);
  }
}

Max2p:
All files have data on it, i guess i have to stick on my 4 void functions though its using 71% memory, maybe someday i can figure it out... thanks to all

That's not a solution, that's ducking the problem. Do some research, spend some time working with the SD examples until you figure it out.

thanks to the 3 of you, the code works perfectly as i want it to be, @ econjack , youre right at saying its not the right solution, im thanking you for that,
its that just i have no choice but to code at the best that i can do.
THANK YOU ALL, more power, SOLVED.