Read_write Problem on a SD card

Hello,
I have a Mega2560 and a CC3000 Adafruit wifi shield, and I first want to use only the SD card and not the Wifi.
Therefore, i've disabled the wifi CS ( D10 set on OUTPUT and HIGH), and put the hardware SS on OUTPUT (although it seems to be optional if you use SD.begin(sdCSpin) )

I won't bother you with the fact that the card doesn't initialize half the time when i try to connect it on my PC and back on the shield, i think i'm just gonna give up and never remove the SD card from the shield.
But i then have to be able to read datas on my SD card and the read() method doesn't work : the error message is the following "error opening donnees.csv" ( it's the same with a .txt file)

Do you know how to fix that?

I've run the following sketch by fat16lib on Ethernet shield SD card initialization failed - Storage - Arduino Forum topic , and there is no problem.

#include <SD.h>
#include <SPI.h>
Sd2Card card;
SdVolume vol;
SdFile root;

// Chip select pin for SD card.
const int sdCsPin = 8;

// Replace the -1 with the CS pin number of any shared SPI device.
const int sharedCsPin = -1;

void setup() {
  Serial.begin(9600);
  
  // Disable any other SPI devices
  if (sharedCsPin >= 0) {
    pinMode(sharedCsPin, OUTPUT);
    digitalWrite(sharedCsPin, HIGH);
  }
  
  // Try to initialize the SD card
  if (card.init(SPI_HALF_SPEED, sdCsPin)) {
    Serial.println("card init OK");
  } else {
    Serial.print("errorCode: ");
    Serial.println(card.errorCode(), HEX);
    Serial.print("errorData: ");
    Serial.println(card.errorData(), HEX);
    return;
  }
  
  // Try to initialize the FAT volume.
  if (vol.init(&card)) {
    Serial.println("vol init OK");
  } else {
    Serial.println("vol init failed");
    return;
  }
  
  // Try to open root.
  if (root.openRoot(&vol)) {
    Serial.println("open root OK");
  } else {
    Serial.println("open root failed");
  }
}
void loop() {}

EDIT. It seems i cannot open and write a file now too...

The SD card slave select is normally D4, not D8, but I could be wrong about that shield.

edit: It is D4. This is from the pdf on the adafruit site:

On the CC3000 shield, we use the following pin connections
SCK - #13
MISO #12
MOSI #11
CS for CC3000 #10
VBAT_EN #5
CS for SD Card #4
IRQ #3
On the breakout, b

Don't use these objects for routine access to an SD card:

Sd2Card card;
SdVolume vol;
SdFile root;

They are useful to diagnose problems but just use the SD object like this example:

/* SD card read/write example. Derived from the official example by Bill Greiman */

#include <SD.h>

// Change to SD chip select.
const int8_t sdChipSelect = 4;

// Chip select for any other SPI device or -1 if no other SPI device.
const int8_t otherChipSelect = 10;

File myFile;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    // wait for serial port to connect. Needed for Leonardo only
  }
  // Disable any other SPI device.
  if (otherChipSelect >= 0) {
    pinMode(otherChipSelect, OUTPUT);
    digitalWrite(otherChipSelect, HIGH);
  }
  Serial.print("Initializing SD card...");

  if (!SD.begin(sdChipSelect)) {
    Serial.println("initialization failed!");
    while(1);
  }
  Serial.println("initialization done.");
  
  // open the file.
  myFile = SD.open("test.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
	// close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
    
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    	Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
  	// if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
	// nothing happens after setup
}

Here is typical output from a Mega:

Initializing SD card...initialization done.
Writing to test.txt...done.
test.txt:
testing 1, 2, 3.
testing 1, 2, 3.

I just copy-pasted the script from fat16lib and ddn't think to change the chips when i posted but i did it on arduino.

It seems to work, but oddly the first time i try any script using SD card, it first says that initialisation failed, and i have to push the reset button. The the card is initialized and i'm able to write and read thanks to your script =)

Do you know what the problem with the first initialisation could come from and how to solve it? thx

Try a 1 second delay after disabling the cc3000 slave select. The SD may need a second to power up.

  // Disable any other SPI device.
  if (otherChipSelect >= 0) {
    pinMode(otherChipSelect, OUTPUT);
    digitalWrite(otherChipSelect, HIGH);
  }

   pinMode(sdChipSelect, OUTPUT);
   digitalWrite(sdChipSelect, HIGH);

  delay(1000);

  Serial.print("Initializing SD card...");

  if (!SD.begin(sdChipSelect)) {
    Serial.println("initialization failed!");
    while(1);
  }
  Serial.println("initialization done.");

edit: I also added setting the SD slave select to OUTPUT and HIGH before the delay.

It doesn't seem to be the problem ; i've tried with a delay of 1,5,10 and 60 seconds and it doesn't work

Try this code instead.

  while(!SD.begin(sdChipSelect)) {
    Serial.println("initialization failed!");
    delay(2000);
    Serial.print("Initializing SD card again...");
  }
  Serial.println("initialization done.");

Well i haven't changed anything and now it works... I'm relieved but i definitely don't understand how that device works ...

Thanks anyway, if the problem reappears i'll be sure to try your second solution!

the problem persists with the while loop too..