473,791 Members | 3,216 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C: Use of setvbuf

140 New Member
Hi,

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;
  7.  
  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;
  14.  
  15.     if ((fp1 = fopen(infile, "r")) == NULL)
  16.     {
  17.         printf("Error opening file %s for input.\n",infile);
  18.         return FAIL;
  19.     }
  20.  
  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.     }
  27.  
  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.     }
  34.  
  35.     if (setvbuf(fp2, NULL, _IOLBF, 0) < 0)
  36.     {
  37.                 printf("setvbuf error in %s\n",outfile);
  38.         return FAIL;
  39.     }
  40.  
  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.     }
  50.  
  51.     fwrite(block, sizeof(DATA), num_read, fp2);
  52.     fclose(fp1);
  53.     fclose(fp2);
  54.  
  55.     if (ferror(fp1))
  56.     {
  57.         printf("Error reading file %s\n",infile);
  58.         return FAIL;
  59.     }
  60.     return SUCCESS;
  61. }
  62.  
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.

Thanks,
Sitko.
Dec 10 '07 #1
2 4626
sitko
140 New Member
Hi,

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.     }
  6.  
  7.     if (setvbuf(fp2, NULL, _IOLBF, 0) < 0)
  8.     {
  9.         printf("setvbuf error in %s\n",outfile);
  10.         return FAIL;
  11.     }
  12.  
Thanks,
Sitko.
Dec 10 '07 #2
sitko
140 New Member
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);"

Thanks,
Sitko.
Dec 10 '07 #3

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

Similar topics

2
3070
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 gcc 2.95.2 19990314 than it does on SCO 5.0.4 cc or Linux gcc egcs-1.1.2 19991024. This is a bit curious as here's a gcc example that works one way and one that works the other, and I would think the relevant code is in libc/glibc. This is what...
4
8355
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 it Thanks Ram
6
4726
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 almost shakes itself to pieces. I tried using setvbuf, with large buffers, e.g. (example only, not checked): #include <stdio.h>
2
2037
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?
18
5706
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. And no, I know I can use direct low level I/O or non-buffered to do reads, but for my app, I need the buffering. I can implement myself, but this is not optimal. Example, I open a read only file using fopen(). I periodically know
9
13314
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 work like buffered? the program is below
10
6948
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 setvbuf() on the predefined streams, immediatly upon start of main()? How often is this function used in typical C programs? Thanks.
22
2417
cat
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 there an alternative algorithm? void catfile(FILE *in, FILE *out) { register int num_char; /*Get characters*/ while ((num_char = getc(in)) != EOF) {
2
4272
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) { perror("setvbuf");
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10156
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.