468,513 Members | 1,751 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

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 3952
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

Post your reply

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

Similar topics

2 posts views Thread by Keith Doyle | last post: by
4 posts views Thread by Ramprasad A Padmanabhan | last post: by
2 posts views Thread by j0mbolar | last post: by
18 posts views Thread by JG | last post: by
9 posts views Thread by kernelxu | last post: by
10 posts views Thread by santosh | last post: by
cat
22 posts views Thread by Jag | last post: by
2 posts views Thread by vippstar | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.