473,225 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

C: Use of setvbuf

140 100+

I'm creating a quick utility which will copy one file into another, but I need it to have the ability to select out certain lines not to copy.

I figured I'd use fread/fwrite but set the buffering size to a line with "setvbuf", here is the code I was going to use as a skeleton for this:
Expand|Select|Wrap|Line Numbers
  1. #include <stddef.h>
  2. #include <stdio.h>
  3. #define FAIL 0
  4. #define SUCCESS 1
  5. #define BLOCKSIZE 512
  6. typedef char DATA;
  8. int copyfile ( infile, outfile) //I'd add arguments for which strings to exclude
  9.     char *infile, *outfile;
  10. {
  11.     FILE *fp1, *fp2;
  12.     DATA block[BLOCKSIZE];
  13.     int num_read;
  15.     if ((fp1 = fopen(infile, "r")) == NULL)
  16.     {
  17.         printf("Error opening file %s for input.\n",infile);
  18.         return FAIL;
  19.     }
  21.     if ((fp2 = fopen(outfile, "w")) == NULL)
  22.     {
  23.         printf("Error opening file %s for output.\n",outfile);
  24.         fclose (fp1);
  25.         return FAIL;
  26.     }
  28. //code added here from post #2 below
  29.         if (setvbuf(fp1, NULL, _IOLBF, 0) < 0)
  30.     {
  31.                 printf("setvbuf error in %s\n",infile);
  32.         return FAIL;
  33.     }
  35.     if (setvbuf(fp2, NULL, _IOLBF, 0) < 0)
  36.     {
  37.                 printf("setvbuf error in %s\n",outfile);
  38.         return FAIL;
  39.     }
  41.     while ((num_read - fread(block, sizeof(DATA), BLOCKSIZE, fp1)) == BLOCKSIZE)
  42.     {
  43.         //Add check for strings to exclude.
  44.         //If (strcmp(line,string)==NULL}
  45.         //{ skip over that line}
  46.         //else {
  47.         fwrite(block, sizeof(DATA), num_read, fp2);
  48.         //}
  49.     }
  51.     fwrite(block, sizeof(DATA), num_read, fp2);
  52.     fclose(fp1);
  53.     fclose(fp2);
  55.     if (ferror(fp1))
  56.     {
  57.         printf("Error reading file %s\n",infile);
  58.         return FAIL;
  59.     }
  60.     return SUCCESS;
  61. }
I want to use setvbuf ( to change the buffering to line buffering, instead of block buffering, but all references I find only cover how to use setvbuf to set the buffering size to NULL..
Here is the syntax for setvbuf:
int setvbuf ( FILE * stream, char * buffer, int mode, size_t size );
mode = (_IOFBF - block buffering, _IOLBF - line buffering, _IONBF - no buffering)

So, what I'm looking for, is how to I use setvbuf to use the _IOLBF, and what changes do I need to make to this code to read LINES instead of BLOCKS.

Dec 10 '07 #1
2 4571
140 100+

I've since found this code, to set up the setvbuf. Now I just need help converting the BUFFERING to appropriate settings for line buffering...

Expand|Select|Wrap|Line Numbers
  1. if (setvbuf(fp1, NULL, _IOLBF, 0) < 0)
  2.     {
  3.         printf("setvbuf error in %s\n",infile);
  4.         return FAIL;
  5.     }
  7.     if (setvbuf(fp2, NULL, _IOLBF, 0) < 0)
  8.     {
  9.         printf("setvbuf error in %s\n",outfile);
  10.         return FAIL;
  11.     }
Dec 10 '07 #2
140 100+
Bummer, I found a code snipet that reads lines, instead of blocks. Which uses fgets/fputs.

My boss told me to use fread/fwrite so I got into that train of thought...

The code is virtually the same except for a "#define LINESIZE 100"

and the usage of fgets/fputs:
"while (fgets(line, LINESIZE-1, fp1) != NULL) fputs(line, fp2);"

Dec 10 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Keith Doyle | last post by:
I'm curious if setvbuf behavior is known to be undefined with regards to effects it may have on the file pointer. I've found that the following code works different on AIX 4.3.3 xlc and FreeBSD...
by: Ramprasad A Padmanabhan | last post by:
I am running a program within another and reading the output. My problem is that all outputs are delayed because of bufferring Can I tell printf to print all outputs immediately and not buffer...
by: grunes | last post by:
I wish to fread a few thousand bytes at a time in turn from several very large files from a DVD data disk, under Redhat Fedora Linux. When this is done the DVD drive wastes a lot of time and...
by: j0mbolar | last post by:
is using _IONBF with setvbf the general way of getting an unbuffered stream in a standardized way? or are system specific functions generally favored over c89's setvbuf?
by: JG | last post by:
Does anyone know a standard (or supported on Linux, Mac, Win32) way to clear a read stream buffer (standard ANSI C file stream)? I would even settle for a platform specific way of doing it. ...
by: kernelxu | last post by:
hi, everyone. now, I'am confused on such a problem: function setbuf(stdin, NULL) or setvbuf(stdin, NULL, _IONBF, 0) can set the stadard input stream unbuffered. however, why does my program...
by: santosh | last post by:
Which situations call for the use of setvbuf(), specifically when you supply the buffer yourself? What is the advantage of an user specified buffer over an automatically allocated one? Can we call...
by: Jag | last post by:
I've read parts of K&R's ANSI C v2 and this is what their cat looked like but when I compared the speed of this code to gnu cat, it seems very slow. How do I optimize this for greater speeds? is...
by: vippstar | last post by:
Is there a memory leak in this particular program: #include <stdio.h> #include <stdlib.h> int main(void) { printf("Hello, world\n"); if(setvbuf(stdout, NULL, _IONBF, 0) != 0) {...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.