EOF checking the below


 
Thread Tools Search this Thread
Top Forums Programming EOF checking the below
# 1  
Old 03-06-2008
Error 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  
Old 03-06-2008
Quote:
Originally Posted by ramkrix

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  
Old 03-07-2008
Power

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  
Old 03-07-2008
Quote:
Originally Posted by ramkrix
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  
Old 03-10-2008
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?Smilie
# 6  
Old 03-10-2008
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.
# 7  
Old 03-10-2008
Power

Thanks fmurphy for your reply.

if I have a structure defined at the beginning in another prgm instaed of an char array name[20],can I use like this?

struct bio {
char name[20];
int age;
float salary;
}

main()
{
struct bio mydata;
--------
--------
--------

read(fd, &mydata, (size_t) ))

}
How can we use the size_t with structure?

Thanks in Advance..

Ramkrix
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. SCO

Stop boot system at "Checking protected password and checking subsystem databases"

Hi, (i'm sorry for my english) I'm a problem on boot sco unix 5.0.5 open server. this stop at "Checking protected password and checking subsystem databases" (See this image ) I'm try this: 1) http://www.digipedia.pl/usenet/thread/50/37093/#post37094 2) SCO: SCO Unix - Server hangs... (9 Replies)
Discussion started by: buji
9 Replies

2. Shell Programming and Scripting

[Solved] <<EOF error checking

I am trying to check the return status of a command thats encapsulated in an <<EOF #!/bin/ksh set -x xxx() { set -x ls -lt /tmp/jdlkewjdlkewjdlkewjdlewjdlew<<EOT ret=$? EOT echo "ret=$ret" } >>$LOG 2>&1 LOG=/tmp/t2.out rm -rf $LOG echo "Starting process `date... (2 Replies)
Discussion started by: BeefStu
2 Replies

3. Shell Programming and Scripting

confused with << EOF EOF

Hi friends , I am confused with << EOF EOF Most of the cases I found sqlplus $db_conn_str << EOF some sql staments EOF another exapmle is #!/bin/sh echo -n 'what is the value? ' read value sed 's/XXX/'$value'/' <<EOF The value is XXX EOF (1 Reply)
Discussion started by: imipsita.rath
1 Replies

4. Shell Programming and Scripting

Until eof do????

Hey! Can I write a routine that allows me to in a txt file check line by line until the end of file? something like until do ---some code--- done maybe it is a stupid question but I never learned shell scripts and I need this :p thanks in advance (1 Reply)
Discussion started by: ruben.rodrigues
1 Replies

5. Programming

Eof

How can I write EOF into a file? I tryed to do as follows: size=sizeof(EOF); end_of_file=EOF; write(fdMutexRichieste, &end_of_file, size); But it does non work correctly, becouse in the next cicle (using lseek(..., SEEK_END) of my code it seems to ignore my EOF and use the LAST... (5 Replies)
Discussion started by: DNAx86
5 Replies

6. Shell Programming and Scripting

What is << EOF

I'm trying to connect to oracle with the following code in the script: checksqlerror{ if echo $1 exit fi } $SQLPLUS username/password@schemaname checksqlerror "failed to connect to oracle" If I'm passing wrong schema name,instead of executing checksqlerror it stops and expects user... (2 Replies)
Discussion started by: BhawanaAggarwal
2 Replies

7. UNIX for Dummies Questions & Answers

\n after EOF

Hello all, I have unix file that ends with the following EOF '9999999999' I want to remove the '\n' character after EOF. What is the command that should be included in the script, before sending the file? will this work: $ echo "<99999999999>\c" >> <filename> thanks in advance. (3 Replies)
Discussion started by: priya12
3 Replies

8. Shell Programming and Scripting

Eof

hi, in a shell script i came accross the following bit of code 1.shift $(($OPTIND - 1)) 2.if ; then 3. cat << EOF >&2 4.Usage: $0 lockfilename 5.EOF 6. exit 1 7.fi I am not able to understand the meaning of lines(1,3,5). Can any one of u tell me the purpose of above said lines.... (1 Reply)
Discussion started by: ravi raj kumar
1 Replies

9. UNIX for Dummies Questions & Answers

Eof

I have written this code: code echo 'Now choose the file or files you want to merge your input with: \c' read filenames filelist="" for file in $filenames; do filelist="$filelist $file" done echo "Now that you've chosen these files ($filelist), please start typing: " until ; do paste -... (2 Replies)
Discussion started by: fiol73
2 Replies

10. Shell Programming and Scripting

Please help with EOF

Hello, you are an awesome crowd! You answered my last questions, thank you sooo much! I am trying to write a korn shell script that will record the memory my application is using (HP-UX B.11.11) and I have this: if (( $APP > $THRESHOLD )) then echo "Warning message will display" cat... (2 Replies)
Discussion started by: satraver
2 Replies
Login or Register to Ask a Question