Sponsored Content
Top Forums Programming Maximum buffer size for read() Post 302601538 by gabam on Friday 24th of February 2012 12:45:36 AM
Old 02-24-2012
Maximum buffer size for read()

Hi friends,
Hope everybody is fine. First have a look at my code, then we will talk about it.


Code:
 
$ 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;
        char buf[BUFSIZE];
        progname = argv[0];
        if(argc != 3)
        printf("Usage %s from to\n", progname);
        f1 = open(argv[1], 0);
        if(f1 == -1)
        {
        printf("Can't open %s\n", argv[1]);
        }
        f2 = creat(argv[2], 777);
        if(f2 == -1)
        {
        printf("Can't create %s\n", argv[2]);
        }
        while((n = read(f1, buf, BUFSIZE)) > 0)
        {
                printf("%c\n", buf[0]);
                if(write(f2, buf, n) != n)
                {
                printf("Write error\n");
                }
        }
        exit(0);
}

As you can see I've defined a macro bufsize 1. The value 1 is used for my buffer. My question is, what is the unit of 1, bytes, kilobytes, megabytes? Because, the file that I am reading is very lage, containing thousands of lines, how come it copies that large file into another file. Initially, I had set bufsize as 512, then i set it to 1, still the program works fine, copying the large file into another file.
Could you please help me here?

Looking forward to your wonderful replies.

Thanks in advance!
 

10 More Discussions You Might Find Interesting

1. Programming

Using fread if the buffer size is not known

Hi... I am trying to read a binary data that have different types of messages of different lengths. I am using fread() but this functions needs the size and count to read the buffer from the file. I think this may cause that the buffer overlaps other messages. Is there an alternative to read... (1 Reply)
Discussion started by: jlrodz
1 Replies

2. Programming

Maximum File Size

Hi, When i checked for the maximum file size on solaris 5.9 the max file size obtained was only 2147483647 and all the further writes to the file which had reached that max size is not added to that file. even i had registered the signal SIGXFSZ, but the signal was not delivered to the... (5 Replies)
Discussion started by: matrixmadhan
5 Replies

3. AIX

Pipe Buffer Size

Hi:- One of our users is getting an error: "There is no process to read data written to a pipe.” I am trying to find out what the pipe buffer size is currently set to. How do I go about this? Thanks, (0 Replies)
Discussion started by: janet
0 Replies

4. Red Hat

buffer cache size

hi everyone, can any one help change the buffer cache size in redhat and suse?? this error i got when i installed oracle 10g and it went well and when i try to mount the database using startup cmd it says too many buffer cache parameters (error code : ora-1034) thnq in advance (0 Replies)
Discussion started by: gsr_kashyap
0 Replies

5. UNIX for Dummies Questions & Answers

Maximum size of a file in unix

What's the maximum file size supported by unix. (3 Replies)
Discussion started by: nagalenoj
3 Replies

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

7. UNIX for Dummies Questions & Answers

Maximum file size ????

Hi All, - block size of 512KB & every address requires 4 bits - The inode structure contains 10 direct pointers, 1 single indirect, 1 double indirect & 1 triple indirect pointer What could be the possible maximum file size for this system Any guess? I am unable to understand the question... (0 Replies)
Discussion started by: preethgideon
0 Replies

8. Programming

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

# 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; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

9. UNIX for Dummies Questions & Answers

Decrease buffer size

Hi, I am using the below command to get the output in a file called "Logs.txt" tail -f filename | egrep -i "cpu | hung " >> Logs.txt The problem is the Logs.txt file gets updated only after the buffer is 8Kb, but i want to update the file immediately and not wait for the buffer to get 8kb. Is... (8 Replies)
Discussion started by: @bhi
8 Replies

10. Programming

[c] How to calculate size of the file from size of the buffer?

Hi, Can I find size of the file from size of the buffer written? nbECRITS = fwrite(strstr(data->buffer, ";") + 1, sizeof(char), (data->buffsize) - LEN_NOM_FIC, fic_sortie); Thank You :) (1 Reply)
Discussion started by: ezee
1 Replies
GETSERVENT_R(3) 					     Linux Programmer's Manual						   GETSERVENT_R(3)

NAME
getservent_r, getservbyname_r, getservbyport_r - get service entry (reentrant) SYNOPSIS
#include <netdb.h> int getservent_r(struct servent *result_buf, char *buf, size_t buflen, struct servent **result); int getservbyname_r(const char *name, const char *proto, struct servent *result_buf, char *buf, size_t buflen, struct servent **result); int getservbyport_r(int port, const char *proto, struct servent *result_buf, char *buf, size_t buflen, struct servent **result); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): getservent_r(), getservbyname_r(), getservbyport_r(): _BSD_SOURCE || _SVID_SOURCE DESCRIPTION
The getservent_r(), getservbyname_r(), and getservbyport_r() functions are the reentrant equivalents of, respectively, getservent(3), get- servbyname(3), and getservbyport(3). They differ in the way that the servent structure is returned, and in the function calling signature and return value. This manual page describes just the differences from the nonreentrant functions. Instead of returning a pointer to a statically allocated servent structure as the function result, these functions copy the structure into the location pointed to by result_buf. The buf array is used to store the string fields pointed to by the returned servent structure. (The nonreentrant functions allocate these strings in static storage.) The size of this array is specified in buflen. If buf is too small, the call fails with the error ERANGE, and the caller must try again with a larger buffer. (A buffer of length 1024 bytes should be sufficient for most applications.) If the function call successfully obtains a service record, then *result is set pointing to result_buf; otherwise, *result is set to NULL. RETURN VALUE
On success, these functions return 0. On error, a positive error number is returned. On error, record not found (getservbyname_r(), getservbyport_r()), or end of input (getservent_r()) result is set to NULL. ERRORS
ENOENT (getservent_r()) No more records in database. ERANGE buf is too small. Try again with a larger buffer (and increased buflen). CONFORMING TO
These functions are GNU extensions. Functions with similar names exist on some other systems, though typically with different calling sig- natures. EXAMPLE
The program below uses getservbyport_r() to retrieve the service record for the port and protocol named in its first command-line argument. If a third (integer) command-line argument is supplied, it is used as the initial value for buflen; if getservbyport_r() fails with the error ERANGE, the program retries with larger buffer sizes. The following shell session shows a couple of sample runs: $ ./a.out 7 tcp 1 ERANGE! Retrying with larger buffer getservbyport_r() returned: 0 (success) (buflen=87) s_name=echo; s_proto=tcp; s_port=7; aliases= $ ./a.out 77777 tcp getservbyport_r() returned: 0 (success) (buflen=1024) Call failed/record not found Program source #define _GNU_SOURCE #include <ctype.h> #include <netdb.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #define MAX_BUF 10000 int main(int argc, char *argv[]) { int buflen, erange_cnt, port, s; struct servent result_buf; struct servent *result; char buf[MAX_BUF]; char *protop; char **p; if (argc < 3) { printf("Usage: %s port-num proto-name [buflen] ", argv[0]); exit(EXIT_FAILURE); } port = htons(atoi(argv[1])); protop = (strcmp(argv[2], "null") == 0 || strcmp(argv[2], "NULL") == 0) ? NULL : argv[2]; buflen = 1024; if (argc > 3) buflen = atoi(argv[3]); if (buflen > MAX_BUF) { printf("Exceeded buffer limit (%d) ", MAX_BUF); exit(EXIT_FAILURE); } erange_cnt = 0; do { s = getservbyport_r(port, protop, &result_buf, buf, buflen, &result); if (s == ERANGE) { if (erange_cnt == 0) printf("ERANGE! Retrying with larger buffer "); erange_cnt++; /* Increment a byte at a time so we can see exactly what size buffer was required */ buflen++; if (buflen > MAX_BUF) { printf("Exceeded buffer limit (%d) ", MAX_BUF); exit(EXIT_FAILURE); } } } while (s == ERANGE); printf("getservbyport_r() returned: %s (buflen=%d) ", (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" : strerror(s), buflen); if (s != 0 || result == NULL) { printf("Call failed/record not found "); exit(EXIT_FAILURE); } printf("s_name=%s; s_proto=%s; s_port=%d; aliases=", result_buf.s_name, result_buf.s_proto, ntohs(result_buf.s_port)); for (p = result_buf.s_aliases; *p != NULL; p++) printf("%s ", *p); printf(" "); exit(EXIT_SUCCESS); } SEE ALSO
getservent(3), services(5) COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2008-08-19 GETSERVENT_R(3)
All times are GMT -4. The time now is 06:14 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy