My program opens a read-write file with the append flag to read from any position specified with lseek. But I'm getting some compilation warnings and I'd like you to help me remove them. What is the problem?
#include "lab.h"
#include <fcntl.h>
#define BUFFERSIZE 1024
int main(int argc, const char *argv[])
{
int fd;
char buffer[BUFFERSIZE];
char dx[] = "C is awesome";
if ((fd = open("test", O_APPEND)) < 0)
{
err_sys("open error");
}
if (write(fd, dx, sizeof(dx)) != sizeof(dx)) {
err_sys("write error");
}
if(lseek(fd, 0, SEEK_END) == -1)
{
err_sys("lseek error1");
}
if (read(fd, buffer, BUFFERSIZE) > 0)
{
puts(buffer);
}
if(write(fd, dx, sizeof(dx)) != sizeof(dx))
{
err_sys("write error1");
}
if (lseek(fd, 0, SEEK_CUR) == -1) /* EOF */
{
err_sys("lseek error2");
}
if (read(fd, buffer, BUFFERSIZE) > 0)
{
puts(buffer);
}
if (write(fd, dx, sizeof(dx)) != sizeof(dx))
{
err_sys("write error2");
}
if(lseek(fd, 0, SEEK_SET) == -1) /* the beginning of file */
{
err_sys("lseek error3");
}
if (read(fd, buffer, BUFFERSIZE) > 0)
{
puts(buffer);
}
if ((write(fd, dx, sizeof(dx))) != sizeof(dx))
{
err_sys("write error3");
}
close(fd);
return 0;
}
header:
#ifndef __LAB_H__
#define __LAB_H__
#include <sys/types.h> /* required for some of our prototypes */
#include <stdio.h> /* for convenience */
#include <stdlib.h> /* for convenience */
#include <string.h> /* for convenience */
#include <unistd.h> /* for convenience */
#define MAXLINE 4096 /* max line length */
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
/* default file access permissions for new files */
#define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
/* default permissions for new directories */
/* prototypes for our own functions */
char *path_alloc(int *); /* {Prog pathalloc} */
int open_max(void); /* {Prog openmax} */
void clr_fl(int, int); /* {Prog setfl} */
void set_fl(int, int); /* {Prog setfl} */
void err_dump(const char *, ...); /* {App misc_source} */
void err_msg(const char *, ...);
void err_quit(const char *, ...);
void err_ret(const char *, ...);
void err_sys(const char *, ...);
#endif /* __LAB_H__ */
warnings receive:
gcc -Wall -Wextra -O -g -D_FORTIFY_SOURCE=2 -I../exemple/ append.c ../exemple/liblab.a -o append
append.c: In function ‘main’:
append.c:6:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
6 | int main(int argc, const char *argv[])
| ~~~~^~~~
append.c:6:32: warning: unused parameter ‘argv’ [-Wunused-parameter]
6 | int main(int argc, const char *argv[])
| ~~~~~~~~~~~~^~~~~~
You aren't using the
argcandargvvariables and when you compile the solution, the compiler displays a warning for each of these.Change the signature of
int main(int argc, const char *argv[])to plainint main()and it should compile without warnings.