ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have managed interchange data correclty using fopen(), fread() and fwrite() to a rfcomm0 device file between my Ubuntu pc and a bluetooth module.
but whenever i run
Code:
ft = ftell(fd);
printf("ftell %d\n", ft); //-1
ft = fseek(fd, 0,SEEK_END);
printf("SEEK_END %d\n", ft); //-1
if (ft = -1)
{
printf("ERROR: %s\n", strerror(errno)); //illegal seek.
}
the error output is illegal seek.
any hints plz?
Thanks
I have managed interchange data correclty using fopen(), fread() and fwrite() to a rfcomm0 device file between my Ubuntu pc and a bluetooth module.
but whenever i run
Code:
ft = ftell(fd);
printf("ftell %d\n", ft); //-1
ft = fseek(fd, 0,SEEK_END);
printf("SEEK_END %d\n", ft); //-1
if (ft = -1)
{
printf("ERROR: %s\n", strerror(errno)); //illegal seek.
}
the error output is illegal seek.
any hints plz?
Thanks
Please post full code.
Please compile the code with '-Wall -Wextra -Wformat=2' and make sure there are no warnings.
According to the man page for fseek the fd argument is a stream descriptor, and the lseek functions should be used for cases where the fd is a regular file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main()
{
int i;
char c;
FILE *fd;
int ft;
printf("Wait for BT to establish\n");
fd = fopen("/dev/rfcomm0", "rb+");
if(fd == NULL)
{
perror("Failed to open /dev/rfcomm0");
exit(128);
}
sleep(1);
i = 0;
while(i++ < 5)
{
sleep(1);
ft = feof(fd);
if (!ft)
{
printf("feof %d\n", ft);
if(!fread(&c, 1, 1, fd)){break;};
printf("read: %c\n", c);
ft = fseek(fd, 1,SEEK_CUR);
printf("SEEK_CUR %d\n", ft);
ft = ftell(fd);
printf("ftell %d\n", ft);
ft = fseek(fd, 0L, SEEK_SET);
printf("SEEK_SET %d\n", ft);
if (ft = -1)
{
printf("ERROR: %s\n", strerror(errno));
}
//fflush(fd);
clearerr (fd);
printf("------------------\n\n", ft);
}
else
{
printf("--------------\n\n", ft);
}
}
c = 'D';
count = fwrite(&c, 1, 1, fd);
fflush(fd);
printf("Wrote %c, %zu byte(s).\n", c, count);
sleep(1);
fclose(fd);
return 0;
}
and it fails compiling with gcc -Wall -Wextra -Wformat=2 btf btf.c
the reason i need to use fteel and fseek because if i call fread() while no data is sent from bluetooth module the system freezes, keeps running fread() until a data is available again..
Thanks
...
the reason i need to use fteel and fseek because if i call fread() while no data is sent from bluetooth module the system freezes, keeps running fread() until a data is available again..
Thanks
From the point of view of my common sense one can't use seek in a blue tooth device.
And your reason is wrong - use threads; one of them will be used to read from the blue tooth device and will be stalled as necessary, the other thread(s) will keep your system responsive.
From the point of view of my common sense one can't use seek in a blue tooth device.
And your reason is wrong - use threads; one of them will be used to read from the blue tooth device and will be stalled as necessary, the other thread(s) will keep your system responsive.
hello,
thanks for the reply,
why we cant use fseek or ftell with bluetooth, since it is a character device?
any other methods or functions to use with bluetooth?
hello,
thanks for the reply,
why we cant use fseek or ftell with bluetooth, since it is a character device?
any other methods or functions to use with bluetooth?
Thanks
How do you physically imagine seeking in, say, keyboard ?
Seeking means physically reposition the read device proper (e.g. magnetic head in digital tape recorder or disk) WRT the medium. I.e. one can position magnetic head in a disk drive, or rewind or move forward magnetic tape (or even punched tape).
But how would you seek in keyboard ?
Is bluetooth conceptually different from keyboard ?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.