The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
checking for non-zero value philplasma UNIX for Dummies Questions & Answers 6 01-08-2008 04:51 PM
checking uid filthymonk Shell Programming and Scripting 7 07-19-2007 10:40 PM
Checking cp progress MarGur UNIX for Dummies Questions & Answers 0 05-15-2007 04:13 PM
Checking for PXE maestro@altiris SUN Solaris 5 05-25-2004 12:06 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-06-2008
ramkrix ramkrix is offline
Registered User
  
 

Join Date: Dec 2007
Location: TamilNadu,INDIA
Posts: 52
Exclamation EOF checking the below

Hi,

I am practicing exercise programs with System calls.

Exercise Question: write a pogram to accept a filename from the user. The program should write ecery fifth byte of the file to the standard output.

My Program :

# include <stdio.h>
# include <fcntl.h>
# include <error.h>

main()
{
int fd,first_offset=5,move_offset=0,x=1;
char a,name[20];
scanf("%s",name);

fd = open(name,O_RDONLY,0755);
printf("fd is %d",fd);

if(fd==-1)
{
printf("error");
exit(1);
}

while(x<5)
{
move_offset=move_offset+first_offset;
lseek(fd,move_offset-1,0);
read(fd,name,sizeof(name));
write(1,name,1);
x++;
}
}


Output:
[ramki@lindesk3 sysint_ex]$ cc ex1.c -o ex1
[ramki@lindesk3 sysint_ex]$ ./ex1
./test
FIVEfd is 3

The file "Test" content:
abcdFfghiIklmnVpqrsE



My Question Now:
1. in the program, I used a While loop with an varaible "X" and comparing it to random no of my choice 5 . Instead I want to check the EOF condition in the whilepart. How to check that.
If we are using file pointer and fopen fn, we can use while(feof(fp)==0). But here we used syatem calls and I don know how to check the condition here.

2. In te program output, I found "FIVE" before printing the filedescriptor number. But as per my program flow, fd should be printed first and then the output "FIVE".

3.Is there any othet way of writing the program more simple and precise, especially using piointer for getting the name of the file, instaed of using Array.

Please Help...

Thanks,
Ramkrix
  #2 (permalink)  
Old 03-06-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Quote:
Originally Posted by ramkrix View Post

My Question Now:
1. in the program, I used a While loop with an varaible "X" and comparing it to random no of my choice 5 . Instead I want to check the EOF condition in the whilepart. How to check that.
If we are using file pointer and fopen fn, we can use while(feof(fp)==0). But here we used syatem calls and I don know how to check the condition here.

2. In te program output, I found "FIVE" before printing the filedescriptor number. But as per my program flow, fd should be printed first and then the output "FIVE".

3.Is there any othet way of writing the program more simple and precise, especially using piointer for getting the name of the file, instaed of using Array.

Please Help...

Thanks,
Ramkrix

Code:
#include <stdio.h>

main(int argc, char *argv[])
{
    char *infile;
    FILE *stream;

    infile = argv[1];
    stream = fopen(infile, "r");
}
...now you can use the stream status functions like feof or ferror.
  #3 (permalink)  
Old 03-07-2008
ramkrix ramkrix is offline
Registered User
  
 

Join Date: Dec 2007
Location: TamilNadu,INDIA
Posts: 52
Red face

Thanks for your reply shamrock..

Is tis the way, I need to include the condition in while loop:

while(feof(stream)==0)

One more question to you: instead of using command line args and C library fns can we check this by having the program absolute system calls..

Thanks in advance,
Ramkrix
  #4 (permalink)  
Old 03-07-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Quote:
Originally Posted by ramkrix View Post
Thanks for your reply shamrock..

Is tis the way, I need to include the condition in while loop:

while(feof(stream)==0)

One more question to you: instead of using command line args and C library fns can we check this by having the program absolute system calls..

Thanks in advance,
Ramkrix
You can use "absolute system calls" instead of standard C library routines like fopen() and you can avoid passing the input filename as a command line argument at the expense of hardcoding the input filename in the open() system call.

The system call approach is better for reading 5 bytes at a time from the input file and printing the fifth byte to standard output. This method is preferred over incrementing a counter and repeatedly testing if x < 5 or checking for EOF using the feof() standard lib function.

Code:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

main(int argc, char *argv[])
{
    int fd;
    char name[5];

    fd = open("/path/to/input/file", O_RDONLY);

    while (read(fd, (void *) name, (size_t) 5) == 5)
        printf("the fifth byte is %c\n", name[4]);
}
  #5 (permalink)  
Old 03-10-2008
ramkrix ramkrix is offline
Registered User
  
 

Join Date: Dec 2007
Location: TamilNadu,INDIA
Posts: 52
Once Again thanks for the useful info Shamrock.

let me ask you the last ques from your reply:
while (read(fd, (void *) name, (size_t) 5) == 5)

The "(size_t)5", what does it mean and will it do?Bcoz i read from a book that we need to give the sizeof() operator at the end. Also you are comaparing it to value "==5"? I could not understand here.

The below one is what I coded in my program: read(fd,name,sizeof(name));

Was mine a right one?
  #6 (permalink)  
Old 03-10-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,924
Quote:
The below one is what I coded in my program: read(fd,name,sizeof(name));
Was mine a right one?
sizeof(name) is 20 since you defined name as an array of 20 characters i.e. char name[20]. Therefore your program tries to read 20 bytes at a time - with no error checking.

Quote:
while (read(fd, (void *) name, (size_t) 5) == 5)
Here the program tries to read 5 bytes and checks that it has actally read 5 bytes. size_t is defined by ISO C for use in representing size information and is very useful whien code portability across different architectures and programming models is desirable. It is required to be an unsigned integral type. Typically it is an int or long.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0