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 4626
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 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...
|
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
|
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>
|
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.
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
| |
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
|
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.
|
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) {
|
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");
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |