Data not written to txt file

I am using an Uno r3 with the Spark Fun Canbus Shield https://www.sparkfun.com/products/10039.

The card is a uSD (1gb pny) formatted FAT using the SD group’s tool.

The text file gets created but it is empty.

The shield uses SPI to interface with a CAN controller. I am beginning to think that the SPI commands to the can controller may be causing problems.

Here is some of my code:

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

// Define pins on mcp2515
#define CANSELECT 10
#define CANINT_D 2

// Define Joystick connection
#define UP     A1
#define RIGHT  A2
#define DOWN   A3
#define CLICK  A4
#define LEFT   A5

#define SD_CS 9
File dataFile;	// File for logging data

void setup() {

	pinMode (SCK,OUTPUT);
	pinMode (MISO,INPUT);
	pinMode (MOSI, OUTPUT);
	pinMode (CANSELECT, OUTPUT);
	pinMode (CANINT_D, INPUT);
	pinMode (SD_CS, OUTPUT);
	// Pin mode for joystick
	pinMode(UP,INPUT);
	pinMode(DOWN,INPUT);
	pinMode(LEFT,INPUT);
	pinMode(RIGHT,INPUT);
	pinMode(CLICK,INPUT);
	// Enable internal pull-ups for joystick
	digitalWrite(UP, HIGH);
	digitalWrite(DOWN, HIGH);
	digitalWrite(LEFT, HIGH);
	digitalWrite(RIGHT, HIGH);
	digitalWrite(CLICK, HIGH);
	
	Serial.begin(9600);  //connection with serial monitor
	if(!SD.begin(SD_CS)) {
		Serial.println("SD initialization failed.");
		//return;
	}
	else {Serial.println("SD initialization successful.");}
	delay(50);
	dataFile = SD.open("DATALOG.TXT", FILE_WRITE);
	if(dataFile){
		dataFile.println("**********Log Start**********");
		Serial.println("Logging...");
	}
	
	byte ret;
	
	Serial.println("Beginning SPI Loopback testing...");
	SPI.begin();
	SPI.setDataMode(SPI_MODE0);
	SPI.setClockDivider(SPI_CLOCK_DIV2);	
	SPI.setBitOrder(MSBFIRST);

        // Code specific to setting up the CAN controller 
        // That I am omitting to save some space
}

void loop() {
	
        // More omitted code specific to the CAN controller

        if(digitalRead(CANINT_D) == 0) {// Check for interrupt from mcp2515
		//Serial.println("Interrupt!");
		readBus();
	}
	// Check for joystick click to close log file
	if (digitalRead(CLICK) == 0){
		if(dataFile){
			dataFile.println("***********Log End***********");
			dataFile.close();
			Serial.println("Done");
		}
	}
}

void readBus() {
	byte rx_status, len, i;
	unsigned short id_h, id_l, id_full;
	byte data[8];

        // Omitted code specific to receiving data from the CAN controller
        // and formatting it for output to the serial monitor and text file

	Serial.print(id_h);
	Serial.print(", ");
	Serial.print(id_l);
	Serial.print(", ");
	Serial.print(id_full);
	Serial.print(", ");
	Serial.print(len);
	for(i = 0;i<len;i++){
		Serial.print(", ");
		Serial.print(data[i]);
	}
	Serial.println();
	// Print to data log file
	if(dataFile){
		dataFile.print(id_h);
		dataFile.print(", ");
		dataFile.print(id_l);
		dataFile.print(", ");
		dataFile.print(id_full);
		dataFile.print(", ");
		dataFile.print(len);
		for(i = 0;i<len;i++){
			dataFile.print(", ");
			dataFile.print(data[i]);
		}
		dataFile.println();
	}
	else{
		Serial.println("Unable to access file");
	}
}

The serial monitor is displaying this: (The program is just sending the same data packet over and over)

Any help you guys can provide would be much appreciated!

After some more testing I am pretty sure it is the SPI commands to the CAN controller that are causing the issue. If I open the file, write to it, and close it before initializing the CAN controller everything works. I am still testing a few things but I will update when I have more info.