473,385 Members | 1,693 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,385 software developers and data experts.

C: Use of setvbuf

140 100+
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 4591
sitko
140 100+
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 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);"

Thanks,
Sitko.
Dec 10 '07 #3

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

Similar topics

2
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...
4
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...
6
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...
2
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
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. ...
9
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...
10
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...
22
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...
2
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) {...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.