![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 11:40 PM |
| Checking cp progress | MarGur | UNIX for Dummies Questions & Answers | 0 | 05-15-2007 05:13 PM |
| Checking for PXE | maestro@altiris | SUN Solaris | 5 | 05-25-2004 01:06 AM |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|