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 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: -
-
char* filename;
-
-
filename=strcpy(filename,"data");
-
strcat(filename,itoa(i[0]));
-
strcat(filename,".dat");
-
-
FILE *fp=fopen(filename,"w");
-
.
-
.
-
.
-
Regards,
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: -
-
char* filename;
-
-
filename=strcpy(filename,"data");
-
strcat(filename,itoa(i[0]));
-
strcat(filename,".dat");
-
-
FILE *fp=fopen(filename,"w");
-
.
-
.
-
.
-
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
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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')
>>>...
|
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...
|
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...
|
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...
|
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"...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
| |