Storing a Temporary File Using C

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?

   #include <stdio.h>
    #include "error.h"
    #include "string.h"
    #define BUFLEN 256
    char buffer[BUFLEN];
    int main() {
    FILE *fp;
    char *rsync;
    char *target
    char tmp_file[BUFLEN];
                command=malloc(2*strlen(rsync)+strlen(target)+24+strlen(tmp_file));
                sprintf(command, "%s %s://%s/csync/etc/passwd %s", rsync, rsync, target, tmp_file);1
            }
            if ... 
         }
     fclose(fp);
     unlink(tmp_file);
   }

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

fread(tmp_file, buflen, 1, filepointer);

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 Like

Essentially in a nutshell this is what I am trying to achieve:

1 - Temporary download /etc/vpnc/test.conf to tmp_file
2 - Search for a line in tmp_file
3 - print out the results of my findings
4 - Delete tmp_file

As always, many thanks for your help

What I am suggesting is

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
        }
}
1 Like

simply awesome and thank you for your help. Let me peck away and see what I am come up.

Also I am wondering, why do this in C? Shell would be just as good for this.

1 Like

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 :slight_smile:

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.

1 Like

Code which calls system() a lot, like this one, are often exceptions to the rule. It uses C and shell instead of just shell :slight_smile:

1 Like

Corona688,

One more stupid question. From your example:

#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

Honestly I wouldn't use rsync, I'd use scp.

scp username@host:/etc/passwd /tmp/filename

...and mkstemp will not do what I want because C is not creating the file here.

Unfortunately I have to use the above method (rsync) for obtaining the file. Let me see what I can come up with. Thanks for the help

There's more discussion on this here >>
Storing a Temporary File Using C
and here >>
http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0CCcQFjAB&url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F27929794%2Fstoring-a-remote-file-locally-using-c&ei=AM21VOGgEoGfyAT25IGICg&usg=AFQjCNEwrnGKVfQRmMZk7B2SUXvplz5kAA&bvm=bv.83640239,d.aWw

And what protocol is rsync using? Often it's just sftp or scp.

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

when you can just do this:

read data -> process data

It's not a local file, it's a local copy of a file on a remote system.