sd.open returns '0' ?

Hey,

im currently messing arround with the SD library.

The micro SD card (2 GB) is connected to the Arduino Ethernet Shield. It initializes successfully but i cant read the content of my file:

 else if (request.substring (0, 6) == "/test/")
            {
              File fh = SD.open ("/test.txt");
              client.print (fh);
              client.print (fh.available());
              while (fh.available())
                client.print (fh.read());
                
              fh.close();
              client.print ("END");
            }

Output:

00END

The file exists and contains 'HELLO WORLD !". I also tried it with filepath "test.txt"

SD.open() will also fail if you run out of SRAM. I see you are using some network device that uses a client.print() call. You should post your entire sketch.

Well, the whole sketch is kind of big, jsut a collection of multiple things

I didn't count every byte, but if that is an Uno, I suspect you are running out of SRAM. You might be able to eliminate some SRAM usage by using the F() function on those static strings.

This function works for me to check SRAM remaining. This is how to use it. If you run out of SRAM, it does not show zero. It shows a negative number or a very large number.

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);  
}

void setup() {
  Serial.begin(9600);
  // all your setup stuff, then

  Serial.print(F("SRAM = "));
  Serial.println(freeRam());
}

Okay, ill try it later. Thank you.

What does the SRAM actually do (when does it get full?)

Also, how can i prevent it from getting full?

SRAM is data memory. The memory reported after a compile is program memory only. They are separate. An Uno has 2K SRAM. A Mega has 8K SRAM.

You can save SRAM (data memory) by keeping static strings in program memory instead of copying to SRAM. That is what the F() function does. You can see how to use it in the freeRam example above.

Works fine. thank you.