IO buffering
807578Jul 4 2001 — edited Aug 1 2001HPUX11
I need to write a program in C. My program is to read in an input file and write into several output files. There are different set of input file and output files. That means if I run:
If I run:
MyProg File2
It will read File2 and extract the fields to write to output files File21 File22 File23.
I have questions:
1. I need to maintain the data integrity, if I write to File11, and I fail to write to File12, I should rollback the writing to File11(just like database). How can I do that in C? Is there any method I can use?
2. Is there any problems if I open too many file at one time?
Main(){
Open output files File11, File12, File13, File14, File15;
Open input file File1;
Read a line;
While not eof {
Extract Fields();
Write to File11(field3, field4);
Write to File12(field1, field2, field3);
Write to File13(field1, field4, field5);
Write to File14(field6, field7, field8);
Write to File15(field8, field9, field10);
}
}
3. The input file contains a few millions of lines. Is this an efficient way to do io processing. Is there any buffering mechanism I can adopt for example attached the file with a buffer. If the buffer is filled up, then flush to the file and so on. How can do it? Is this OS-dependent?
Thanks.