LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2010, 09:37 AM   #1
raedbenz
Member
 
Registered: Dec 2007
Posts: 93

Rep: Reputation: 15
illegal fseek/ftell


Hello,

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
 
Old 07-17-2010, 10:27 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by raedbenz View Post
Hello,

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.
 
Old 07-17-2010, 10:40 AM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.

Without seeing your code, I'm only guessing.
 
Old 07-17-2010, 02:25 PM   #4
raedbenz
Member
 
Registered: Dec 2007
Posts: 93

Original Poster
Rep: Reputation: 15
hello,
nthg complicated about the code:

Code:
char *gc;
rfcomm = fopen("/dev/rfcomm0", "rb+");
if(rfcomm == NULL)
{
 perror("Error:");           
}
fread(&gc, 1, 1, rfcomm); //works fine
*gc = 0x31;
fwrite( gc, 1, 1, rfcomm); //works fine

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.
}
hints plz?
 
Old 07-17-2010, 03:40 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by raedbenz View Post
hello,
nthg complicated about the code:

Code:
char *gc;
rfcomm = fopen("/dev/rfcomm0", "rb+");
if(rfcomm == NULL)
{
 perror("Error:");           
}
fread(&gc, 1, 1, rfcomm); //works fine
*gc = 0x31;
fwrite( gc, 1, 1, rfcomm); //works fine

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.
}
hints plz?
Sorry, but I can't believe it's full code. If it is, it probably produces a lot of warnings and probably won't even compile.

And I do not see where 'fd' is defined. So the code shouldn't even compile. Reread my previous post: http://www.linuxquestions.org/questi...1/#post4036373 .

Last edited by Sergei Steshenko; 07-17-2010 at 03:41 PM.
 
Old 07-17-2010, 03:58 PM   #6
raedbenz
Member
 
Registered: Dec 2007
Posts: 93

Original Poster
Rep: Reputation: 15
source code

Code:
#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

Last edited by raedbenz; 07-17-2010 at 04:06 PM.
 
Old 07-17-2010, 04:20 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by raedbenz View Post
...
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.
 
Old 07-17-2010, 05:15 PM   #8
raedbenz
Member
 
Registered: Dec 2007
Posts: 93

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
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?

Thanks
 
Old 07-17-2010, 05:36 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by raedbenz View Post
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 ?
 
Old 07-17-2010, 05:44 PM   #10
raedbenz
Member
 
Registered: Dec 2007
Posts: 93

Original Poster
Rep: Reputation: 15
ok then..
is there a way to tell that the device file is empty or not? because i mentioned if i read and no data available the systems hangs..
cheers
 
Old 07-17-2010, 05:50 PM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by raedbenz View Post
ok then..
is there a way to tell that the device file is empty or not? because i mentioned if i read and no data available the systems hangs..
cheers
Haven't I suggested to use threads ?

You can read

man 2 read
man 3 unlocked_stdio
man 3 read

, but conceptually you will be implementing threads anyway.
 
Old 07-17-2010, 06:03 PM   #12
raedbenz
Member
 
Registered: Dec 2007
Posts: 93

Original Poster
Rep: Reputation: 15
using a thread that will keep reading the device all time definitely is not the best method especially on embedded Linux implementation.
 
Old 07-17-2010, 06:11 PM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by raedbenz View Post
using a thread that will keep reading the device all time definitely is not the best method especially on embedded Linux implementation.
Why ?
 
Old 07-17-2010, 06:21 PM   #14
raedbenz
Member
 
Registered: Dec 2007
Posts: 93

Original Poster
Rep: Reputation: 15
polling eats up cpu , hence more power consumption.
 
Old 07-17-2010, 06:26 PM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by raedbenz View Post
polling eats up cpu , hence more power consumption.
If a thread is stuck waiting for system call completion, where do you see polling ?

Besides, threads may have different priorities.

Besides there exists 'man 2 sched_yield'.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] ftell always returns 0 10110111 Programming 3 12-31-2009 04:47 PM
Strange fseek issue ? C programming exvor Programming 3 10-05-2006 11:20 AM
fseek problem tokernizer Programming 0 04-27-2006 07:21 PM
fseek on a socket descriptor to discard socket buffer? Thinking Programming 1 12-06-2005 09:15 PM
fseek cross NFS is really slow chiuyaa Linux - Networking 0 06-23-2004 08:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration