You're already getting 30 megabytes per second translation rate, which comes to 60 megs/second transfer rate considering you're reading AND writing. Just how fast is your disk? Will a hardcoded solution actually be faster?
Many thanks for your c++ program.
I'm very appreciate it.
Based on your experience, is it I should edit your program by using "argv", "f_read", "f_write" in order to get the desired program running format:
It really doesn't matter whether the shell or the program does the file-opening for a simple program that always has one input file and one output file, but since you insist:
Many thanks, Corona688.
I just curious what does it really mean in "strncmp(buf, "@HWI", 4) == 0" and "fputs(buf, fout);" that you use in your source code?
I can't really get how to use string compare function in this case.
Many thanks for advice
Many thanks, Corona688.
After I reading the 'man strcmp' and 'man fputs'. I understanding the reason why you using 'strcmp' and 'fputs' while coding
I just wondering why that "if(fgets(buf, 4096, fin)== NULL ) continue;" will print out only the line next to "@HWI instead of other line?
"fgets" will "Get string from stream" and read through it from the input file.
Many thanks to explain the reason that "if(fgets(buf, 4096, fin)== NULL ) continue;" will only print the second line shown in above example instead of other line.
It reads a line every loop, but never prints anything unless it finds @HWI. When it does, it prints both that line (after subbing # for @) and the line after it (by reading and immediately printing another line).
So any lines that aren't @HWI or don't immediately follow @HWI get read but not printed.
while(fgets(buf, 4096, fin)) //keep reading line-by-line into buffer
{
if(strncmp(buf, "@HWI", 4) == 0) //if buffer starts with '@HWI'
{
buf[0]='#'; //set the first char to '#'
fputs(buf, fout); //and print the whole line
if(fgets(buf, 4096, fin) == NULL) continue; //read next line; check for failure or eof
fputs(buf, fout); //print the line
}
}
This piece of code calls fputs() twice -- once it prints the modified '@HWI' line and the second time it prints the next line after. Then, the loop gets in next iteration and keeps reading in lines, until while condition is satisfied, that is, until '@HWI' is encountered. The third and fourth line in your input get lost in the while iterations, not in the if test.
@Corona: Any particular reason why you chose to use buffer of size 4096?
Not really. 4096 is just a "nice round number" in computing terms, a power of two, and plenty long enough for most text lines. 'most' may not mean yours, adjust to taste. Anything under a couple of megs in size will work. If you need really large buffers you have to make them with malloc:
unsigned long size=(1024L*1024L*64L); // 64 megabytes
char *buf=malloc(size);
You use buf the same way as you would a stack var like char buf[64]; since an array and a pointer amount to the same thing in the end, as far as the processor's concerned -- a starting point in memory.