473,385 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can someone help me? (file names)

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
4 1450
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
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
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
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

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

Similar topics

2
by: Joe Gazda | last post by:
I'm a relative newbie to PHP, but have been able to put together some PHP code to generate a CSV/XLS file from a Query result. Now, I would like to include custom column names instead of the MySQL...
6
by: Kevin T. Ryan | last post by:
Hi All - I'm not sure, but I'm wondering if this is a bug, or maybe (more likely) I'm misunderstanding something...see below: >>> f = open('testfile', 'w') >>> f.write('kevin\n') >>>...
6
by: Helmut Giese | last post by:
Hello out there, I am a rather experienced C programmer. However, today I got a javascript assignment because someone left (something like: "You're a great programmer - you'll handle this.") and I...
3
by: Chris | last post by:
Hi, In C# I tried to save a file from a generated file name. Just before launching the dialog I check for a valid file name to be sure. There for I used the method ValidateNames from the save...
7
by: Petr Jakes | last post by:
I have got names of functions stored in the file. For the simplicity expect one row only with two function names: printFoo, printFOO In my code I would like to define functions and then to read...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
8
by: OziRus | last post by:
Hi, I've char* array that I defined like char *str. I want to read from a file, that contains names in each row, and assign them to my str char * array. m is a char array, f is a file...
4
by: Jay | last post by:
What's the recommended way to change file/folder names within a project? I want to change the solution name from (say) sol1.sln to sol2.sln, and also everything else named sol1.* (eg sol1.suo,...
5
by: Steven D'Aprano | last post by:
After reading an earlier thread about opening and closing lots of files, I thought I'd do a little experiment. Suppose you have a whole lot of files, and you need to open each one, append a...
1
by: mujunshan | last post by:
Yesterday, I installed PythonCE on my cellphone whose OS is Windows Mobile 5.I wanted to use numpy as calculation tool.But after I copy numpy module in my desktop computer into my phone,I find many...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.