I need help with writing to a FAT file and reading it.

Hello,

I have a buffer, which is an array of integers of size 1 x 100. I want to write this buffer to memory and read it using FAT. I’m faced with the following errors:

line 107 : WARNING BUFFINFAT.C : Wrong type for parameter 2.
line 107 : WARNING BUFFINFAT.C : Conversion to incompatible pointer type
line 131 : WARNING BUFFINFAT.C : Conversion to incompatible pointer type
line 131 : WARNING BUFFINFAT.C : Wrong type for parameter 2.

The errors refer to the following lines of code:

// Write to it…
rc = fat_Write(
&my_file, // File, as set by fat_Open()
&buffer, // line 107
sizeof(buffer) // Number of characters to write.
);

rc = fat_Read(&my_file, buffer, sizeof(buffer)); // line 131.

Please let me know if you have any suggestions. I’ve pasted the complete code below. Thank you.

/*
11/2/8

This program creates the following buffer:
{ time, 1,2,3,4,5,6,7,time,1,2,3,4,5,6,7,…}
and writes it to screen.

*/

#class auto

// This macro causes the FAT library to wait for everything to complete
// before returning to the caller. This makes the application MUCH simpler.
#define FAT_BLOCK

// Call in the FAT filesystem support code.
#use “fat.lib”

// When files are accessed, we need a FATfile structure.
FATfile my_file;

unsigned int buffer [ 100 ]; // A buffer to write to and then read from.

int main ()
{
auto int i; // counter reseting every 8 cycles
auto int j; // counter used for positions in array

unsigned int the_time; // time stamp

int rc; // Return code store. Always need to check return codes from
// FAT library functions.
long prealloc;
// Used if the file needs to be created.
fat_part *first_part;
// Use the first mounted FAT partition.

// Auto-mount the FAT file system, which populates the default mounted
// partition list array that is provided in FAT_CONFIG.LIB. This is the most
// important information since, when you open a file, you need only to
// specify the partition. Also, tell auto-mount to use the default device
// configuration flags at run time.
rc = fat_AutoMount(FDDF_USE_DEFAULT);

j = 0;
while ( 1 )
{
for( i = 0; i < 8; i++ )
{

     buffer [ j ] = i; // insert i at jth position


    	if ( i == 0 ) // when i is 0, insert time at jth position
     {
     	//the_time = ( unsigned int )MS_TIMER;
     	buffer [ j ] =  2000;
     }

     // print out the integer in the jth position to screen
     //printf( "buffer[%d] = %d

", j , buffer[ j ] );

     j = j + 1; // move to the next position in array.
  }

  if ( j == 80 )
break;

}

// Scan the populated mounted partitions list to find the first mounted
// partition.  The number of configured fat devices, as well as the mounted
// partition list, are provided for us in FAT_CONFIG.LIB.
first_part = NULL;
for (i = 0; i &lt; num_fat_devices * FAT_MAX_PARTITIONS; ++i)

{
if ((first_part = fat_part_mounted[i]) != NULL)
{
// found a mounted partition, so use it
break;
}
}

// OK, filesystem exists and is ready to access. Let’s create a file.

// Do not pre-allocate any more than the minimum necessary amount of
// storage.
prealloc = 0;

// Open (and maybe create) it…
rc = fat_Open(
first_part, // First partition pointer from fat_AutoMount()
“MyBuffer.txt”, // Name of file. Always an absolute path name.
FAT_FILE, // Type of object, i.e. a file.
FAT_CREATE, // Create the file if it does not exist.
&my_file, // Fill in this structure with file details
&prealloc // Number of bytes to allocate.
);

// Write to it…
rc = fat_Write(
&my_file, // File, as set by fat_Open()
&buffer,
sizeof(buffer) // Number of characters to write.
);

// Done writing; close it.
rc = fat_Close(&my_file);

// At this point, my_file cannot be used until it is opened again.

// Open the same file for reading.
rc = fat_Open(
first_part, // First partition pointer from fat_AutoMount()
“MyBuffer.txt”, // Name of file. Always an absolute path name.
FAT_FILE, // Type of object, i.e. a file.
0, // 0 means the file must exist.
&my_file, // Fill in this structure with file details
NULL // This will not be used, you can pass NULL.
);

// Read the first 200 bytes (sizeof buf) from the file. Of course, we

// No matter, the return code indicates this.
rc = fat_Read(&my_file, buffer, sizeof(buffer));

// Read OK.  Print out the buffer contents.
  printf("Read %d bytes:

“, rc);
printf(”%d", rc, rc, buffer); // print
printf("
"); // terminated.

// Since we are using blocking mode, it will not return until it has
// closed all files and unmounted the partition & device.
fat_UnmountDevice(first_part->dev);

// Many operating systems do not like “hard reset/reboot” when a filesystem
// is involved. The Rabbit FAT implementation is robust enough to
// gracefully recover from reset/power loss without losing data. It will
// automatically recover when fat_Init() is called at startup. However, it
// is still a good idea to shut down properly if you know you are exiting
// the program.
printf("All OK.
");

}

Hello,

I was able to get rid of the errors by converting the type using “char* buffer”. However, I’m still working on getting the buffer to write to screen from FAT.

This portion of the code is not doing what I think it should do (write the buffer to screen):

rc = fat_Read(&my_file, (char*)&buffer, sizeof(buffer));

// Read OK. Print out the buffer contents.
printf(“Read %d bytes:
“, rc);
printf(”%d”, rc, rc, buffer );

The above code outputs the following to screen:

Read 200 bytes:
200

Is there an obvious solution to this?

try to change the code into

// Read OK. Print out the buffer contents.
printf("Read %d bytes:
“, rc);
for (counter=0;counter<100;counter++)
printf(”%d “,((unsigned int *)buffer)[counter]); // print
printf(”
"); // terminated.

I have incorporated the above solution into my code and it works. (thank you santogiuseppe). However, I’m still getting the following warnings:

line 104 : WARNING BUFFINFAT.C : Wrong type for parameter 2.
line 104 : WARNING BUFFINFAT.C : Conversion to incompatible pointer type
line 128 : WARNING BUFFINFAT.C : Conversion to incompatible pointer type
line 128 : WARNING BUFFINFAT.C : Wrong type for parameter 2.

rc = fat_Write(
&my_file, // File, as set by fat_Open()
buffer, // This is line 104.
sizeof( buffer ) // Number of characters to write.
);

and again here:
rc = fat_Read(&my_file, buffer, sizeof( buffer )); // This is line 128.

Any idea how I can get rid of these?

Thanks,
Ershad.