nixCraft Linux Forum

nixCraft

Linux / UNIX 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

Linux answers from nixCraft.


Coding in General Discussion on PHP/Perl/Python/Ruby/GNU C or C++. MySQL, PgSQL and (X)HTML or any other programming languages you want.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 15-10-2005, 11:25 AM
Junior Member
User
 
Join Date: Sep 2005
OS: suse/redhat
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
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
  #2 (permalink)  
Old 15-10-2005, 02:14 PM
rockdalinux's Avatar
Is that all you got?
User
 
Join Date: May 2005
Location: Planet Vegeta
OS: Redhat
Posts: 705
Thanks: 15
Thanked 19 Times in 18 Posts
Rep Power: 10
rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light
Default

You mean you need uniq file name each time and then write output to that file?
__________________
Rocky Jr.
What's wrong? I hope I am not making you uncomfortable...

Never send a boy to do a mans job.
Reply With Quote
  #3 (permalink)  
Old 15-10-2005, 02:35 PM
Member
User
 
Join Date: Sep 2005
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
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 16-10-2005, 06:05 AM
Junior Member
User
 
Join Date: Sep 2005
OS: suse/redhat
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
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 16-10-2005, 06:12 AM
Junior Member
User
 
Join Date: Sep 2005
OS: suse/redhat
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
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 16-10-2005, 04:22 PM
tom tom is offline
Contributors
User
 
Join Date: Jun 2005
Location: London, UK
Posts: 213
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 5
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 16-10-2005, 04:34 PM
Member
User
 
Join Date: Sep 2005
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
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


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 Off


Similar Threads

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


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


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

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 37 38