Storing a Temporary File Using C


 
Thread Tools Search this Thread
Top Forums Programming Storing a Temporary File Using C
# 1  
Old 01-13-2015
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?
Code:
   #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

Last edited by metallica1973; 01-13-2015 at 12:03 PM..
# 2  
Old 01-13-2015
Quote:
Originally Posted by metallica1973
How would someone save a file such as /etc/vpnc/test.conf locally into a temp file, so it can be queried?
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.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-13-2015
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
# 4  
Old 01-13-2015
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:

Code:
#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
        }
}

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-13-2015
simply awesome and thank you for your help. Let me peck away and see what I am come up.
# 6  
Old 01-13-2015
Also I am wondering, why do this in C? Shell would be just as good for this.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 01-13-2015
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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Finding specific string in file and storing in another file

Text in input file is like this <title> <band height="21" isSplitAllowed="true" > <staticText> <reportElement x="1" y="1" width="313" height="20" key="staticText-1"/> <box></box> <textElement> <font fontName="Arial" pdfFontName="Helvetica-Bold"... (4 Replies)
Discussion started by: aankita30
4 Replies

2. UNIX for Dummies Questions & Answers

Mailx - temporary mail message file: No such file or directory

How would I go about resolving this error temporary mail message file: No such file or directory Can anybody tell me where the default location is for the temporary mail message file is for mailx? It appears that it doesn't exist. Thanks (1 Reply)
Discussion started by: joen
1 Replies

3. Shell Programming and Scripting

How to remove a temporary file inside gawk

Hi- How can I make the temporary file 0 byte , created inside gawk. I am using system("rm -f temp_orders"); It seems system command is deleting file permanently and I am not able to execute below statement. print ORD_HEAD_FULL >> cFILE; (cFile is temp_orders) (2 Replies)
Discussion started by: ashish_kaithi
2 Replies

4. Shell Programming and Scripting

How to remove the first line of a file without using any temporary file

I want remove first line of the file without using any temporary file. Everything should be done in the original file itself since I don't any file creation access whereas I have modify access. It would be great if somebody help in this regard ASAP . Thanks in Advance. Kishore:mad: (1 Reply)
Discussion started by: tvbhkishore
1 Replies

5. UNIX for Dummies Questions & Answers

mailx command - Temporary mail file: permission denied

Hi , I am facing a problem with respect to mailx command in unix . Earlier it was working fine and I am facing this issue only from last week . I used mailx command and I am getting a error message as follows : temporary mail file: Permission denied If I run mailx command from... (2 Replies)
Discussion started by: deepav1985
2 Replies

6. Shell Programming and Scripting

temporary file to file then move to directory

Ok in my bash script i have 5 options to create a simple html script. I need to create a temporary file and whatever the user types will be stored in that file using html codes. And then I have another option in which that temporary file will be moved to the public_html directory in which the user... (19 Replies)
Discussion started by: gangsta
19 Replies

7. Shell Programming and Scripting

Perl : how to modify a file without generate a temporary file

Hi All, I have a file like below, how can i insert one line after line 1 without using a temporary file in perl? line 1 line 2 line 3 expected result line 1 new line <---insert here line 2 line 3 (2 Replies)
Discussion started by: summer_cherry
2 Replies

8. AIX

error : pg: 0652-122 Cannot write to the temporary file

Hi All, I'm getting this error when I use "pg". /tmp is not full and the permission is correct. root@axappk01::/home> hostname|pg pg: 0652-122 Cannot write to the temporary file. Please advise. (6 Replies)
Discussion started by: fara_aris
6 Replies

9. Shell Programming and Scripting

remove temporary file ?

Hi all, In the script I am creating a temporary file with process id as temp.txt.$$ I want to remove this tomporary file first from the current directory when i'll run the same script next time. Note: Every time when the script executes then it has unique process id and it'll create a... (5 Replies)
Discussion started by: varungupta
5 Replies

10. Ubuntu

Avoid creating temporary files on editing a file in Ubuntu

Hi, My ubuntu flavor always create temporary files having filename followed by ~ on editing. For eg: if I am editing a file called "sip.c", automatically a temporary (bkup) file is getting created with the name "sip.c~". How to avoid this file creation? (7 Replies)
Discussion started by: royalibrahim
7 Replies
Login or Register to Ask a Question