By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,604 Members | 1,414 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,604 IT Pros & Developers. It's quick & easy.

Can someone help me? (file names)

P: 7
I have a program which asks the user to enter a starting value. Then opens a file to send values to. Is there a way that i can make the file name depend on what the user starts with, or get them to enter a file name?

Here is the start of my program...



printf("Enter a starting digit or string: ");
scanf("%d",&i[0]);

printf("How many times do you want to run Conway's Sequence? ");
scanf("%d",&rept);

FILE *dat;
dat = fopen("data.dat","w");
FILE *dat2;
dat2 = fopen("data2.dat","w"); ......



So instead of using data.dat and data2.dat, have something depending on the program!

Thanks
Nov 13 '06 #1
Share this Question
Share on Google+
4 Replies


vpawizard
P: 66
I have a program which asks the user to enter a starting value. Then opens a file to send values to. Is there a way that i can make the file name depend on what the user starts with, or get them to enter a file name?

Here is the start of my program...



printf("Enter a starting digit or string: ");
scanf("%d",&i[0]);

printf("How many times do you want to run Conway's Sequence? ");
scanf("%d",&rept);

FILE *dat;
dat = fopen("data.dat","w");
FILE *dat2;
dat2 = fopen("data2.dat","w"); ......



So instead of using data.dat and data2.dat, have something depending on the program!

Thanks


Hello,
You can create the filename by appending the starting digit to the name of file e.g. if the starting digit is 5, the file will be called data5.dat or if the starting digit is 10, name would be data10.dat, etc. It can be done using following logic:

Expand|Select|Wrap|Line Numbers
  1.  
  2. char* filename;
  3.  
  4. filename=strcpy(filename,"data");
  5. strcat(filename,itoa(i[0]));
  6. strcat(filename,".dat");
  7.  
  8. FILE *fp=fopen(filename,"w");
  9. .
  10. .
  11. .
  12.  

Regards,
Nov 13 '06 #2

P: 7
Hello,
You can create the filename by appending the starting digit to the name of file e.g. if the starting digit is 5, the file will be called data5.dat or if the starting digit is 10, name would be data10.dat, etc. It can be done using following logic:

Expand|Select|Wrap|Line Numbers
  1.  
  2. char* filename;
  3.  
  4. filename=strcpy(filename,"data");
  5. strcat(filename,itoa(i[0]));
  6. strcat(filename,".dat");
  7.  
  8. FILE *fp=fopen(filename,"w");
  9. .
  10. .
  11. .
  12.  

Regards,

Thanks for your quick response. I have tried what you suggested and included the relavenat header files, however it has an error saying itoa requires 3 arguments! What are the other twoit depends on?

Thanks
Nov 13 '06 #3

P: 7
Thanks for your quick response. I have tried what you suggested and included the relavenat header files, however it has an error saying itoa requires 3 arguments! What are the other twoit depends on?

Thanks

Also i am working in C++, so it sure if this is the best form to use.

Thanks
Nov 13 '06 #4

vpawizard
P: 66
Also i am working in C++, so it sure if this is the best form to use.

Thanks

itoa() has three arguments.
1st- The integer to be converted
2nd- The buffer(string) in which to store the number converted to string.
3rd- Radix. Base of number. For decimal numbers-10.
Nov 14 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.