Cannot read a file with read(fd, buffer, buffersize) function


 
Thread Tools Search this Thread
Top Forums Programming Cannot read a file with read(fd, buffer, buffersize) function
# 1  
Old 06-11-2009
Cannot read a file with read(fd, buffer, buffersize) function

Code:
# include <stdio.h>
# include <fcntl.h>
# include <stdlib.h>
# include <sys/stat.h>

int main(int argc, char *argv[])
{
     int fRead, fPadded, padVal;
     int btRead;
     int BUFFSIZE = 512;
     char buff[BUFFSIZE];

     if (argc != 4)
     {
          printf ("Please provide all of the following: names of original and padded files, and a pad value.\n");
          return 0;
     }

     if (fRead =open (argv[1], O_RDONLY) ==-1)
     {
          printf ("An error has occurred opening the file.\nPlease make sure file names are correct.\n");
          return 0;
     }
     else
     {
          close (fRead);
          if (fPadded =creat (argv[2], S_IRWXU) ==-1 )
          {
               printf ("An error has occurred writing the file.\n");
               return 0;
          }
          else
          {
               close (fPadded);

               if (padVal =atol(argv[3]) ==-1)
               {
                    printf ("An error has occurred with pad value.\nPlease make sure the number is correct.\n");
                    return 0;
               }
               else
               {
                    if (padVal<0 || padVal>(2^32-1))
                    {
                         printf ("The pad value is out of range, it should be greater than 0 and less than 4294967295.\n");
                    }
                    else
                    {
                         while ((btRead = read (fRead, buff, BUFFSIZE))>0)
          if (write (fPadded, buff, btRead) != btRead)
                                  printf ("An error has occurred while copying file\n");
                              if (btRead <0)
                                  printf ("An error has occurred while reading file\n");

                         return 1;
                    }
               }
          }
     }
}

I think every part of the program works fine except for the highlighted part. For some reason, c fails to read a file and btRead is assigned a -1 value. Of course, write function does not work either, because buff is probably empty. What could be the problem? Thanks in advance.

Last edited by vidyadhar85; 06-11-2009 at 11:04 PM..
# 2  
Old 06-11-2009
You can't read from or write to a file descriptor after you close it. You need to remove the close() statements:

Code:
           .
           .
           .
          close (fRead);  <--- 
          if (fPadded =creat (argv[2], S_IRWXU) ==-1 )
          {
                    printf ("An error has occurred writing the file.\n");
                    return 0;
          }
          else
          {
             close (fPadded);   <---
               .
               .
               .

# 3  
Old 06-12-2009
Before the close() happens, his biggest problem is his syntax:

Code:
if (fRead =open (argv[1], O_RDONLY) ==-1)

is completely wrong, and he's done it in several places.

== has higher precedence than =, so this is parsed as:

Code:
if (fRead = (open (argv[1], O_RDONLY) ==-1))

try
Code:
if ((fRead = open (argv[1], O_RDONLY)) ==-1)

and use this method throughout your code.

Also, don't use the ato* functions, but strto* instead, because they set errno.
# 4  
Old 06-12-2009
Quote:
Originally Posted by mgessner
Before the close() happens, his biggest problem is his syntax:

...
Doh! I totally missed that.

Just another example of why you should never, ever perform assignments within a conditional clause - it's all too easy to be misleading. If the code had been written as follows the bug would never could have been introduced:

Code:
fRead = open( argv[ 1 ], O_RDONLY );
if ( fRead == -1 )
{
    printf( "...." );
    return 0;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Ubuntu

Barcode scanner buffer read manipulation

Hello, I'm facing some problems with a barcode scanner from Cygnal Inc, model Sweda SL-20. Info from lsusb -v: Bus 003 Device 003: ID 10c4:ff11 Cygnal Integrated Products, Inc. Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 1.10 ... (4 Replies)
Discussion started by: minharojr
4 Replies

2. Shell Programming and Scripting

[Bash] Read History function & Read Arrowkeys

Hi. How can I create a history function? (By "read" command or so) & How can I configure a read command so that the arrow keys are not displayed so funny? (^[[A) Thanks in advance. (4 Replies)
Discussion started by: sinnlosername
4 Replies

3. UNIX Desktop Questions & Answers

AIX how to read the file in function again and again

dear friends I have a wrote a shell script which works like this. 1.) a command is executed and the log is moved in the file. 2.) this file is copied in to the other file. 3.) used a grep command to find a particular word. 4.) if a particular word is there then the script will go to next... (4 Replies)
Discussion started by: aboy212u
4 Replies

4. Homework & Coursework Questions

C++ Attempting to modify this function to read from a (;) semi-colon-separated file

After some thought. I am uncomfortable issuing my professors name where, there may be unintended side effects from any negative responses/feedback. Willing to re post if I can omit school / professor publicly, but can message moderator for validation? I am here for knowledge and understanding,... (1 Reply)
Discussion started by: briandanielz
1 Replies

5. UNIX for Advanced & Expert Users

Input buffer and READ

Hello everyone, Can someone please explain the input buffer behaviour for the read command in ksh93 on AIX? I have 'googled' for weeks now, and did not find a satisfactory answer or solution to my dilemma. I have the following code: STTY=$(stty -g) if ;then stty -echo -icanon time 0 min... (1 Reply)
Discussion started by: gio001
1 Replies

6. Programming

Maximum buffer size for read()

Hi friends, Hope everybody is fine. First have a look at my code, then we will talk about it. $ cat copy.c #include <stdio.h> #define PERMS 0644 /* RW for owner, R for group, others */ #define BUFSIZE 1 char *progname; int main(int argc,char * argv) { int f1, f2, n; ... (4 Replies)
Discussion started by: gabam
4 Replies

7. Shell Programming and Scripting

Read Table,View,Package,Function and Procedure Name in a File

Hi I am new to Unix shell scripting. But i need help to slove the below issue. Issue description: I want to read table, view names and package names in a file my plan to find the table name is : search "From" key word find the table or view To find the packge name : Search "Package... (5 Replies)
Discussion started by: sboss
5 Replies

8. Shell Programming and Scripting

Expect: How to read buffer for spawn

Hi All, I have to write one expect script to login to one system. I am using set timeout -1 match_max 100000 spawn ssh root@hostname Now when I do spawn ssh to that host it send some warning message and one challenge Challenge: 111-2345 I need to read this challenge value and has... (1 Reply)
Discussion started by: Lokesh Agrawal
1 Replies

9. Programming

read a file wich fscanf() in a function

I use fopen, fscanf, fclose to read a file. It can work well. since many files should be read, a function is created with the same code. But in the function, fscanf can not work well. for example, the first line of the the file is: > filename but the fscanf will give: 207/23/eee/34 it appears... (2 Replies)
Discussion started by: cdbug
2 Replies

10. Shell Programming and Scripting

Increase the buffer size to read lengthy lines

Hi All, I am trying to read output from a command. The output format is as follows: Thursday 13 Mar 2008 Information This is sample text Friday 14 Mar 2008 Warning This is one more sample text First line contains informtation (date etc) and the 2nd line contains some information. ... (3 Replies)
Discussion started by: ssunda6
3 Replies
Login or Register to Ask a Question