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 ...
|
|||||||
| Register | FAQ | Members List | Calendar | Forgotten your password? | Mark Forums Read |
|
|||
|
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)? |
| Sponsored Links | ||
|
|
|
||||
|
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 |
|
|||
|
If you wanna make a unique file you can use mkstemp()
Code:
int main()
{
char sfn[] = "myapplication.XXXXXX";
int fd;
fd=mkstemp(sfn);
}
|
|
|||
|
Quote:
|
|
|||
|
Quote:
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 |
|
|||
|
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);
}
}
Code:
cc test.c ./a.out 5 |
|
|||
|
wow tom, u extended my 4 line code to big one
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 |
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |