nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

dynamic creations of files

This is a discussion on dynamic creations of files within the Coding in General forums, part of the Development/Scripting category; Hi friends, I have a small question. I have a data file. Depending on the application i need to pass ...


Go Back   nixCraft Linux Forum > Development/Scripting > Coding in General

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 10-15-2005, 12:25 PM
Junior Member
User
 
Join Date: Sep 2005
Posts: 24
Rep Power: 0
kavi
Default dynamic creations of files

Hi friends,
I have a small question. I have a data file. Depending on the application i need to pass an argument to my program(C program), which is n (2,4,8,16,etc). Now based on this input argument n, i need to perform some operations on the data file and write the output to n different files. How do i create new files dynamically ( based on user input n)?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-15-2005, 03:14 PM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 581
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
Default

You mean you need uniq file name each time and then write output to that file?
__________________
Rocky Jr.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
  #3 (permalink)  
Old 10-15-2005, 03:35 PM
Junior Member
User
 
Join Date: Sep 2005
Posts: 25
Rep Power: 0
charvi
Default

If you wanna make a unique file you can use mkstemp()

Code:
int main()
{
        char sfn[] = "myapplication.XXXXXX";
        int fd;
        fd=mkstemp(sfn);
}
Just run ls -l command in current dir to see unique file and use fd to open file in code. But i'm not clear what kavi is looking for, may we need bit more info...
Reply With Quote
  #4 (permalink)  
Old 10-16-2005, 07:05 AM
Junior Member
User
 
Join Date: Sep 2005
Posts: 24
Rep Power: 0
kavi
Default

Quote:
Originally Posted by rockdalinux
You mean you need uniq file name each time and then write output to that file?
yes i will need unique file names. for example, the input arg is 8 then i have to perform different operations and write outputs to 8 different new output files.
Reply With Quote
  #5 (permalink)  
Old 10-16-2005, 07:12 AM
Junior Member
User
 
Join Date: Sep 2005
Posts: 24
Rep Power: 0
kavi
Default

Quote:
Originally Posted by charvi
If you wanna make a unique file you can use mkstemp()

Code:
int main()
{
        char sfn[] = "myapplication.XXXXXX";
        int fd;
        fd=mkstemp(sfn);
}
Just run ls -l command in current dir to see unique file and use fd to open file in code. But i'm not clear what kavi is looking for, may we need bit more info...
Hi charvi,
Thank for the clues. I will try to elaborate with an example. Lets say i write a program divide.c which takes input argument n(2,4,8,16,etc). First thing i do is open an input data file. Then depending on n, i will perform some math on every M bytes of data file and get n different outputs. Now i would like to write each of these n outputs in n different files.
Note: i am performing n operations on every M bytes and there will be n output for every M bytes of the data file. I will be appending data to n files every time
Reply With Quote
  #6 (permalink)  
Old 10-16-2005, 05:22 PM
tom tom is offline
Contributors
User
 
Join Date: Jun 2005
Location: London, UK
Posts: 213
Rep Power: 4
tom is on a distinguished road
Default

hope foillowing will code help you
Code:
#include <errno.h>
int main(int a, char *v[])
{
        char sfn[] = "/tmp/myapplication.XXXXXX";
        FILE *f;
        int fd=-1;
        int i=0,m=0;
        // if not sufficent args
        if (a < 2 )
        {
                fprintf(stderr, "%s %s %s\n", "Error: Argument missing\nSyntax:\n",
                                v[0],"{number}");
                exit(1);
        }

        // put loop to create files
        m=atoi(v[1]);
        for(;i<m;i++){
                sprintf(sfn,"%s%d.XXXXXX","/tmp/file",i);
                fd = mkstemp(sfn);
                if (( f = fdopen(fd, "w+")) == NULL ){
                        fprintf(stderr, "%s\n", "Failed to open file");
                        perror("Error : ");
                        close(fd);
                        exit(2);
                }
                printf("Using file %s\n",sfn);
                fprintf(f,"This is test data %d\n",i);
                fflush(f);
                close(fd);
                // before removing file you need to copy the tmp file to data dir
                // else you will lost the data stored in /tmp/file* file
                unlink(sfn);
        }
}
it need to run as
Code:
cc test.c
./a.out 5
and it will create five files in /tmp directory, make sure before unlink you copy back data file using API as we cannnot open too many files aka FDs.
Reply With Quote
  #7 (permalink)  
Old 10-16-2005, 05:34 PM
Junior Member
User
 
Join Date: Sep 2005
Posts: 25
Rep Power: 0
charvi
Default

wow tom, u extended my 4 line code to big one now why the hell you used ulink() at the end
as far as i know it will remove file ... may be you need to through some light coz i'm not able to get your idea, rest of program is fine
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Shell script for automatic conversion of files in tar files kasimani Shell scripting 2 02-08-2007 04:45 PM
Wget from dynamic url? karabaja Linux software 5 11-18-2006 08:09 PM
Dynamic DNS , should get updates automatically from DHCP sonaikar Linux software 1 03-28-2005 01:14 PM


All times are GMT +5.5. The time now is 02:35 AM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36