473,915 Members | 4,409 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 4631
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
3072
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
8360
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
4729
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
2039
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
5720
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
13329
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
6956
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
2424
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
4273
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
10039
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
9881
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11354
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9732
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
8100
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
5943
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...
1
4778
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
3
3368
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.