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?
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.
Related
I have a C program that I am running on my MacOS terminal. All command line tools and GCC compiler have been installed. However for using functions like getpid() or execv() it gives the following error:
execv-test.c:7:35: error: implicit declaration of function 'getpid' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
printf("Pid before execv: %d\n", getpid());
^
execv-test.c:8:2: error: implicit declaration of function 'execv' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
execv("print",NULL);
^
2 errors generated.
The code:
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
printf("The game is never over, John. But there may be some new players now.\n");
printf("Pid before execv: %d\n", getpid());
execv("print",NULL);
printf("Returned from execv call.\n");
return 0;
}
The following Stack Overflow exchange suggested that I write helper functions for the ones that were taken as implicit declarations. However, I am not sure you could do the same with getpid() or execv(). What should I do to make sure this doesn't happen?
PLEASE NOTE: "print" is just another helper file that is supposed to be run once execv() is called.
Note that you are using system calls that are defined in the unistd.h header file. Therefore calling them without including the std library #include <unistd.h> amounts to "implicit declaration" = "calling a function without defining it first".
So I'm writing a bison (without lex) parser and now I want to read the input code from file and to write the output to another file.
Searching the stackoverflow for some time I found that this way should be good.
bison.y:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern FILE *yyin;
int yylex() { return getc(stdin); }
void yyerror(char *s) {
fprintf (stderr, "%s\n", s);
}
int counter = 1;
char filename2[10] = "dest.ll";
FILE *f2;
%}
%name parse
%%
//grammars
%%
int main(int argc, char *argv[]) {
yyin = fopen(argv[1], "r");
if (argc > 2)
f2 = fopen(argv[2], "w");
else
f2 = fopen(filename2, "w");
yyparse();
return 0;
}
Then i compile it this way:
bison bison.y
cc -ly bison.tab.c
And here the result of cc-compilation:
/tmp/ccNqiuhW.o: In function `main':
bison.tab.c:(.text+0x960): multiple definition of `main'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/liby.a(main.o):(.text.startup+0x0): first defined here
/tmp/ccNqiuhW.o: In function `main':
bison.tab.c:(.text+0x98c): undefined reference to `yyin'
collect2: error: ld returned 1 exit status
The output bison.tab.c file have only 1 main. Ofc int/void main doesn't matter. Can you teach me how to do it correctly?
P.S. By the way, I don't want to spam different posts, and have a little question here. How can I store the string (char *) in $$ in bison? For example, I want to generate a code string after I met the int grammar. I have this error and can't find the answer:
bison.y:94:8: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
INTNUM: NUMBER | DIGIT INTNUM {$$ = "string"};
bison.y: In function ‘yyparse’:
bison.y:28:15: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
PROGRAM: EXPRS { fprintf(f2, "%s: string here %d.\n", $$, counter++) };
will be extremely good if I find the help.
You are linking library liby (linker option -ly). The Bison manual has this to say about it:
The Yacc library contains default implementations of the yyerror and
main functions.
So that's why you have multiple definitions of main. You provide one, and there's one in liby.
Moreover, the docs go on to say that
These default implementations are normally not useful, but POSIX requires them.
(Emphasis added)
You do not need to link liby in order to build a program that includes a bison-generated parser, and normally you should not do so. Instead, provide your own main() and your own yyerror(), both of which you've already done.
Additionally, you are expected to provide a definition of yyin, not just a declaration, whether you link liby or not. To do so, remove the extern keyword from the declaration of yyin in your grammar file.
Your grammar is not complete (there are no rules at all) and the %name directive is not documented and is not recognized by my Bison, but if I add a dummy rule and comment out the %name, in conjunction with the other changes discussed, then bison generates a C source file for me that can be successfully compiled to an executable (without liby).
I recently started learning ZEROMQ and I am stuck somewhere. I tried to run the weather update examples(wuclient.c and wuserver.c) and I get the error below.
In file included from wuclient.c:5:0:
zhelpers.h: In function ‘s_sleep’:
zhelpers.h:133:5: warning: implicit declaration of function ‘nanosleep’ [-Wimplicit-function-declaration]
zhelpers.h: In function ‘s_console’:
zhelpers.h:158:5: warning: implicit declaration of function ‘time’ [-Wimplicit-function-declaration]
zhelpers.h:159:12: warning: implicit declaration of function ‘localtime’ [-Wimplicit-function-declaration]
zhelpers.h:159:26: warning: initialization makes pointer from integer without a cast [enabled by default]
zhelpers.h:161:5: warning: implicit declaration of function ‘strftime’ [-Wimplicit-function-declaration]
zhelpers.h:161:5: warning: incompatible implicit declaration of built-in function ‘strftime’ [enabled by default]
wuclient.c: At top level:
zhelpers.h:60:1: warning: ‘s_send’ defined but not used [-Wunused-function]
zhelpers.h:67:1: warning: ‘s_sendmore’ defined but not used [-Wunused-function]
zhelpers.h:75:1: warning: ‘s_dump’ defined but not used [-Wunused-function]
zhelpers.h:115:1: warning: ‘s_set_id’ defined but not used [-Wunused-function]
zhelpers.h:125:1: warning: ‘s_sleep’ defined but not used [-Wunused-function]
zhelpers.h:139:1: warning: ‘s_clock’ defined but not used [-Wunused-function]
zhelpers.h:156:1: warning: ‘s_console’ defined but not used [-Wunused-function]
The command I used to compile it is: gcc -Wall wuclient.c -o wuclient -L/usr/local/lib -lzmq
And this is the zhelpers.h code here https://github.com/imatix/zguide/blob/master/examples/C/zhelpers.h which is causing the error.
And it was included in this code below:
// Weather update client
// Connects SUB socket to tcp://localhost:5556
// Collects weather updates and finds avg temp in zipcode
#include "zhelpers.h"
int main (int argc, char *argv [])
{
// Socket to talk to server
printf ("Collecting updates from weather server...\n");
void *context = zmq_ctx_new ();
void *subscriber = zmq_socket (context, ZMQ_SUB);
int rc = zmq_connect (subscriber, "tcp://localhost:5556");
assert (rc == 0);
// Subscribe to zipcode, default is NYC, 10001
char *filter = (argc > 1)? argv [1]: "10001 ";
rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE,
filter, strlen (filter));
assert (rc == 0);
// Process 100 updates
int update_nbr;
long total_temp = 0;
for (update_nbr = 0; update_nbr < 100; update_nbr++) {
char *string = s_recv (subscriber);
int zipcode, temperature, relhumidity;
sscanf (string, "%d %d %d",
&zipcode, &temperature, &relhumidity);
total_temp += temperature;
free (string);
}
printf ("Average temperature for zipcode '%s' was %dF\n",
filter, (int) (total_temp / update_nbr));
zmq_close (subscriber);
zmq_ctx_destroy (context);
return 0;
}
I opened the "zhelpers.h" file and "time.h" was included. So I got confused on why this would happen. Am using Ubuntu 12.04 and please, I am neither an expert in C or ZEROMQ but this software looks like my realistic hope of scaling my thesis hurdle.
Thanks.
Note that these are just warnings and not errors. The compiler will still generate something but it might not work as intended.
The header file "zhelpers.h" includes <sys/time.h> and not <time.h> on Ubuntu. This is most probably incorrect. Remove the conditional in "zhelpers.h" and just include <time.h> on all platforms. This will remove half of the warnings.
The second half of the warnings are related to the fact that there are function definitions in "zhelpers.h". This is very poor coding style but the program should still work.
I am trying to read text in from a file and insert that text into a text box.
This is the code I am using.
FILE *infile;
GdkFont *fixed_font;
infile = fopen("text.txt", "r");
fixed_font = gdk_font_load ("-misc-fixed-medium-r-*-*-*-140-*-*-*-*-*-*");
if (infile) {
char buffer[1024];
int nchars;
while (1)
{
nchars = fread(buffer, 1, 1024, infile);
gtk_text_insert(view, fixed_font, NULL, NULL, buffer, nchars);
if (nchars < 1024)
break;
}
fclose (infile);
These are my includes
#include <gtk/gtk.h>
#include <gtk/gtktext.h>
When I compile I get this warning:
warning: implicit declaration of function ‘gtk_text_insert’
I have read on this forum and others that implicit declaration errors come from using functions before they are declared. However, gtk_text_insert() is included in the header file so how can this be implicit?
I am using the following software:
gtk version 2.20.1
Red Hat Enterprise Linux Server release 6.5
GtkText is deprecated and unsupported. It is known to be buggy. To use it, you must define the symbol GTK_ENABLE_BROKEN prior to including the GTK+ header files. Use GtkTextView instead.
From https://developer.gnome.org/gtk2/stable/GtkText.html
I want to get basic information from a hard-drive and print it out. The most important is that the physical sector size is correct.
For the past few hours I have been fighting with ioctl to get what I want but I can't figure it out.
I have never used ioctl before and I can't seem to find an easy explanation on what exactly you have to do.
Anyway my code looks something like this
int main () {
FILE *driveptr;
int sectorsize;
struct hd_driveid hd;
driveptr=fopen("/dev/sda","r");
if (ioctl(driveptr,HDIO_GET_IDENTITY, &hd)!=0) {
printf("Hard disk model: %s\n",hd.model);
printf("Serial number: %s\n",hd.serial_no);
printf("Sector size: %i\n",hd.sector_bytes);
sectorsize=hd.sector_bytes;
} else {
printf("Error fetching device data.\n");
}
}
In the compiler it throws these warnings, it compiles but the strings are empty when printed.
gcc -o test source.c
source.c: In function ‘main’:
source.c:67:9: warning: passing argument 1 of ‘ioctl’ makes integer from pointer without a cast [enabled by default]
/usr/include/x86_64-linux-gnu/sys/ioctl.h:42:12: note: expected ‘int’ but argument is of type ‘struct FILE *’
I hope somebody can explain to me what goes wrong!
Instead of
if (ioctl(driveptr,HDIO_GET_IDENTITY, &hd)!=0) {
you probably want
if (ioctl(fileno(driveptr),HDIO_GET_IDENTITY, &hd)!= -1) {
^^^^^^^ ^ ^^
Because ioctl's first argument need to be an integer file descriptor not a FILE *
fileno() will give you an integer fd from a FILE *.
Note also that ioctl returns -1 on an error and sets errno.
Reading the man pages of the functions you are using is probably quicker than posting to StackOverflow.
See man pages of ioctl, fileno.