How would someone save a file such as /etc/vpnc/test.conf locally into a temp file, so it can be queried? So for example if I used rsync to copy this file locally, how would I add that to a temp_file variable and discard it using unlink?
I truncate the code to get to the point as you can tell I am very new to C and wanted to get a better understanding of it. How would I initialize tmp_file so I can query it locally and then discard the file using unlink? I looked at this but is rather confusing for something so simple. Howto: C Programming with Temporary Files in Linux
Except, what do you mean by "queried"? And how will dumping it wholesale into memory help you? I suspect it won't. Usually you'd operate on lines one-by-one and seek around and things.
My suggestion would be "don't do that" -- open it, then discard it using unlink, but keep the file pointer around until you're done with it. It will disappear from the directory, but your file pointer will still be good until you close it.
1) Download tmp_file
2) Open tmp_file in program
3) Unlink tmp_file (yes, while still open)
4) Search for line
5) Print results
6) Close file (which will now disappear for good)
You seem to have #1 handled, so:
#include <stdio.h>
int main() {
char buf[4096];
char *n=tmpnam(NULL); // Use this as the file name, not tmp_file.
// get the file
...
FILE *fp=fopen(n, "r");
unlink(n);
// Read a line into 'buf' until we run out of lines
while(fgets(buf, 4096, fp))
{
// Check the contents of 'buf' for what you want
}
}
Trust me, I would have done this using python, perl and or bash but the code is written in C and am appending what is already in place. This is my latest adventure
Corona is correct - most command line utilities are tuned and designed for a particular purpose, and really are very close to as good as it gets. Unless you are doing this to learn coding, or have some fun messing around with C, I would second the idea: use command line utilities.
A very good reason for this is the 'next guy'. Somebody may need to use what you have. Or modify it because the manager is having a bad day. If it is C code and does not quite cut it, that person will have to find the source and change it. Simplicity and transparency (like a shell script) has a lot to recommend it.
Dedicated well-designed C code is often provably faster than a script. But how fast do you have to go? That file is less than 1MB. So, C may save you 1 or 2ms on modern systems.
#include <stdio.h>
int main() {
char buf[4096];
char *n=tmpnam(NULL); // Use this as the file name, not tmp_file.
// get the file
...
FILE *fp=fopen(n, "r");
unlink(n);
// Read a line into 'buf' until we run out of lines
while(fgets(buf, 4096, fp))
{
// Check the contents of 'buf' for what you want
}
}
How do I get the results of:
rsync rsync://192.168.3.1/vpnc/test.conf
into the "n" temp file? Forgive me for my stupidity. Also when I attempt to compile my code, its says to use mkstemp
I'm missing something here. If you're copying the contents of a file to another temp file in order to query it, why not just examine the contents of the original file?
Why do this:
read data -> write data -> read data -> process data