Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Dynamic file

Posted on 2005-05-01
6
Medium Priority
?
217 Views
Last Modified: 2010-04-15
Hello All,
I have a segment of program which creates file dynamically.. i.e. when if the number of files to be opened is to be decided at run time.. here is my code :::

for(i = 0;i<=N;i++) // N decide at runtime
{
sprintf(str,"%d",i);
strcat(str1,str);
printf("path = %s\n",str1);
fp1 = fopen(str1,"w");
...
...
fclose(fp1);

}

This creates a file with no xtension.. and also the file name will be 0, 01,012 etc etc..
I have no problem with the file name but the problem is with its filetype.. How can i make those files as a .txt file inside the program itself..


-hssuma
0
Comment
Question by:hssuma
  • 2
  • 2
  • 2
6 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 13907196
Change the following:
   sprintf(str,"%d",i);
   strcat(str1,str);

to

   sprintf(str1,"%s%d.txt",str, i);


0
 

Author Comment

by:hssuma
ID: 13907302
Hey,
  I'm not getting files line 0.txt ,  01.txt, 012.txt.......... but i'm getting something like 0.txt1,0.txt2.........


char str[20],str1[20] = "d:\\su_mat\\";

Wat is the mistake now
0
 
LVL 3

Expert Comment

by:joju
ID: 13907486
for(i = 0;i<=N;i++) // N decide at runtime
{
sprintf(str,"%d.txt",i);
strcat(str1,str);
printf("path = %s\n",str1);
fp1 = fopen(str1,"w");
...
...
fclose(fp1);

}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:hssuma
ID: 13907538
No  joju... i get files like 0.txt, 0.txt1.txt.. ... This doesn't work...
Hope u have got what i'm trying to convey...

Lemme clear it again...

  I have to decide upon the number of files to be opened at runtime..  suppose i want 4 files to opened and then data to be entered in those fiiles... ^Th e number 4 is known only at runtime..
The logic which i pasted in my first post works perfectly well other than it doesn't give an extension of .txt.... If i enter it with strcat .txt gets carried........... Plzz try the code i have given and the one u r suggesting ..
0
 
LVL 3

Accepted Solution

by:
joju earned 60 total points
ID: 13907622
sorry str1 has to be reset in the loop
str1[0] = '\0';



  for(i = 0;i<=N;i++) // N decide at runtime
        {
                str1[0] = '\0';
                sprintf(str,"%d.txt",i);
                strcat(str1,str);
                printf("path = %s\n",str1);

                fp1 = fopen(str1,"w");
                ...
                ...
                fclose(fp1);

        }
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 13910354
> sorry str1 has to be reset in the loop
> str1[0] = '\0';

Note that using sprintf appropriately as in my earlier solution not only
avoids the extraneous call to strcat, but in doing so avoids the bug.

The moral of the tale:  Any reduction in complexity will often remove
the potential for bugs.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question