Sponsored Content
Top Forums Programming Cannot read a file with read(fd, buffer, buffersize) function Post 302324774 by naranja18she on Thursday 11th of June 2009 06:46:26 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
AUPARSE_FEED(3) 						  Linux Audit API						   AUPARSE_FEED(3)

NAME
auparse_feed - feed data into parser SYNOPSIS
#include <auparse.h> int auparse_feed(auparse_state_t *au, const char *data, size_t data_len); au The audit parse state data a buffer of data to feed into the parser, it is data_len bytes long. The data is copied in the parser, upon return the caller may free or reuse the data buffer. data_len number of bytes in data DESCRIPTION
auparse_feed supplies new data for the parser to consume. auparse_init() must have been called with a source type of AUSOURCE_FEED and a NULL pointer. The parser consumes as much data as it can invoking a user supplied callback specified with auparse_add_callback with a cb_event_type of AUPARSE_CB_EVENT_READY each time the parser recognizes a complete event in the data stream. Data not fully parsed will persist and be prepended to the next feed data. After all data has been feed to the parser auparse_flush_feed should be called to signal the end of input data and flush any pending parse data through the parsing system. EXAMPLE
void auparse_callback(auparse_state_t *au, auparse_cb_event_t cb_event_type, void *user_data) { int *event_cnt = (int *)user_data; if (cb_event_type == AUPARSE_CB_EVENT_READY) { if (auparse_first_record(au) <= 0) return; printf("event: %d ", *event_cnt); printf("records:%d ", auparse_get_num_records(au)); do { printf("fields:%d ", auparse_get_num_fields(au)); printf("type=%d ", auparse_get_type(au)); const au_event_t *e = auparse_get_timestamp(au); if (e == NULL) return; printf("event time: %u.%u:%lu ", (unsigned)e->sec, e->milli, e->serial); auparse_first_field(au); do { printf("%s=%s (%s) ", auparse_get_field_name(au), auparse_get_field_str(au), auparse_interpret_field(au)); } while (auparse_next_field(au) > 0); printf(" "); } while(auparse_next_record(au) > 0); (*event_cnt)++; } } main(int argc, char **argv) { char *filename = argv[1]; FILE *fp; char buf[256]; size_t len; int *event_cnt = malloc(sizeof(int)); au = auparse_init(AUSOURCE_FEED, 0); *event_cnt = 1; auparse_add_callback(au, auparse_callback, event_cnt, free); if ((fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "could not open '%s', %s0, filename, strerror(errno)); return 1; } while ((len = fread(buf, 1, sizeof(buf), fp))) { auparse_feed(au, buf, len); } auparse_flush_feed(au); } RETURN VALUE
Returns -1 if an error occurs; otherwise, 0 for success. SEE ALSO
auparse_add_callback(3), auparse_flush_feed(3) AUTHOR
John Dennis Red Hat May 2007 AUPARSE_FEED(3)
All times are GMT -4. The time now is 01:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy