1
votes

I am trying to test writing in a file in Contiki. here is the code I used :

#include "contiki.h"
#include <stdio.h>
#define LEN 256
 PROCESS(test_process, "Coffee test process");
 AUTOSTART_PROCESSES(&test_process);
 PROCESS_THREAD(test_process, ev, data)
 /**/
 {
  PROCESS_BEGIN();
FILE * fp;
   int i;
   /* open the file for writing*/
   fp = fopen ("/home/user/contiki/examples/mySim/1.txt","w");

   /* write 10 lines of text into the file stream*/
   for(i = 0; i < 10;i++){
       fprintf (fp, "This is line %d\n",i + 1);
   }

   /* close the file*/  
   fclose (fp);
  PROCESS_END();
 }

I get this error message after compiling in Cooja simulator:

test.c: In function ‘process_thread_test_process’: test.c:12:1: error: unknown type name ‘FILE’ test.c:15:4: warning: implicit declaration of function ‘fopen’ [-Wimplicit-function-declaration] test.c:15:7: warning: assignment makes pointer from integer without a cast [enabled by default] test.c:19:8: warning: implicit declaration of function ‘fprintf’ [-Wimplicit-function-declaration] test.c:19:8: warning: incompatible implicit declaration of built-in function ‘fprintf’ [enabled by default] test.c:23:4: warning: implicit declaration of function ‘fclose’ [-Wimplicit-function-declaration] make: *** [test.co] Error 1 Process returned error code 2

does anyone has any idea about the problem?

1
No idea ... but, just for fun, swap the includes: first the ones with <> followed by the ones with "". - pmg
It is declared in stdio.h, but you may have a "cut-down" implementation for embedded systems. You might simply open the stdio file you have and see if it is conditionally declared or not declared at all. You probably need to configure the system to include a filesystem. It looks like you are trying to access a file on the development host rather then the target in any case - that won't work! - Clifford
You didn't specify what compiler you use but in gcc for example you can use -E to generate a preprocessed code and search for FILE. You might need to add some #define statements or maybe it's not defined in your <stdio.h> at all. - Arkadiusz Drabczyk
thank you all for the responses. I changed the line <> and " ", it did not work. I think you were right about cut down stdio.h in contiki. I searched in src folder of e_stdio folder in Contiki, there are some files like fvwrite.h and local.h and others which used 'FILE' but I could not found the source where 'FILE' had been defined. and yes I use gcc compiler. I tried gcc -E but could not get what that meant, I'm new in C. - mahshid

1 Answers

3
votes

Contiki does not provide/support the POSIX file API, the same way it does not have many other things (POSIX sockets API, POSIX process creation and control API). Instead, it provides its own filesystem API ("protosockets" API, "protothreads" API etc.).

There are two filesystem implementations: CFS (Contiki File System) and Coffee. You can use the functions described in the Wiki page; they are analogues to low-level POSIX file API (e.g. cfs_open is similar to POSIX open, cfs_close to POSIX close and so on). There are no analogues for buffered I/O functionality (fopen, fclose) and the FILE structure does not exist.