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: -
#include <stddef.h>
-
#include <stdio.h>
-
#define FAIL 0
-
#define SUCCESS 1
-
#define BLOCKSIZE 512
-
typedef char DATA;
-
-
int copyfile ( infile, outfile) //I'd add arguments for which strings to exclude
-
char *infile, *outfile;
-
{
-
FILE *fp1, *fp2;
-
DATA block[BLOCKSIZE];
-
int num_read;
-
-
if ((fp1 = fopen(infile, "r")) == NULL)
-
{
-
printf("Error opening file %s for input.\n",infile);
-
return FAIL;
-
}
-
-
if ((fp2 = fopen(outfile, "w")) == NULL)
-
{
-
printf("Error opening file %s for output.\n",outfile);
-
fclose (fp1);
-
return FAIL;
-
}
-
-
//code added here from post #2 below
-
if (setvbuf(fp1, NULL, _IOLBF, 0) < 0)
-
{
-
printf("setvbuf error in %s\n",infile);
-
return FAIL;
-
}
-
-
if (setvbuf(fp2, NULL, _IOLBF, 0) < 0)
-
{
-
printf("setvbuf error in %s\n",outfile);
-
return FAIL;
-
}
-
-
while ((num_read - fread(block, sizeof(DATA), BLOCKSIZE, fp1)) == BLOCKSIZE)
-
{
-
//Add check for strings to exclude.
-
//If (strcmp(line,string)==NULL}
-
//{ skip over that line}
-
//else {
-
fwrite(block, sizeof(DATA), num_read, fp2);
-
//}
-
}
-
-
fwrite(block, sizeof(DATA), num_read, fp2);
-
fclose(fp1);
-
fclose(fp2);
-
-
if (ferror(fp1))
-
{
-
printf("Error reading file %s\n",infile);
-
return FAIL;
-
}
-
return SUCCESS;
-
}
-
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.
2 4591
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... -
if (setvbuf(fp1, NULL, _IOLBF, 0) < 0)
-
{
-
printf("setvbuf error in %s\n",infile);
-
return FAIL;
-
}
-
-
if (setvbuf(fp2, NULL, _IOLBF, 0) < 0)
-
{
-
printf("setvbuf error in %s\n",outfile);
-
return FAIL;
-
}
-
Thanks,
Sitko.
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.
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: 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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
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...
| |