I m using Borland 3.1. The code is pretty much similar as before just with a slight changes that you suggessted.

Repost anyway -- one misplaced semicolon will cause big problems.

Repost anyway -- one misplaced semicolon will cause big problems.

Hi the code is below please have a look. it saves the file with user specified name but doesnt include the variable values of a b and c as file name. Someone told me that the compiler is old so let me know if thats the case and sugesst some good and easy to use ones. I am using Borland 3.1.

Code:

#include <fstream.h>
#include <iostream.h>
#include <stdio.h>
#include<conio.h>
//using namespace std;

int main()
{
    char FirstName[30], LastName[30];
    int Age;
    char FileName[255];
    char filename[30];
     cout << "\nEnter the name of the file you want to create: ";
    cin >> FileName;
    int a=10,b=12,c=13;
    
    
    sprintf(filename, "%s,%i.,%i.,%i.txt", FileName, a,b,c);

    cout << "Enter First Name: ";
    cin >> FirstName;
    cout << "Enter Last Name:  ";
    cin >> LastName;
    cout << "Enter Age:        ";
    cin >> Age;

   
    ofstream Students(FileName, ios::out);
    Students << FirstName << "\n" << LastName << "\n" << Age;

    cout << "\n\n";
   
    
    return 0;
}

thanks

It has nothing to do with the age of your compiler. Line 28 is wrong, you are using the wrong variable as the first parameter. That is one good reason to avoid using two variables with the same name except for capitalization.

sorry I didnt get that? I even copy pasted exactly that you posted here but the result is still the same...

Look at line 28. Don't you see the difference between the variable name on line 28 and the variable name on line 18 ????:icon_eek: Variable names are case sensitive, meaning filename is not the same as FileName

Oh sorry I tried ofstream Students(filename, ios::out); in line 28 and with this there is no output file at all. can you please comment again if i am doing it wrong.

this worked ok for me

int main()
{
    char FirstName[30], LastName[30];
    int Age;
    char FileName[255];
    char filename[255];
     cout << "\nEnter the name of the file you want to create: ";
    cin >> FileName;
    int a=10,b=12,c=13;
    
    
    sprintf(filename, "%s,%i.,%i.,%i.txt", FileName, a,b,c);
cout << "\"" << filename << "\"\n";
    cout << "Enter First Name: ";
    cin >> FirstName;
    cout << "Enter Last Name:  ";
    cin >> LastName;
    cout << "Enter Age:        ";
    cin >> Age;

   
    ofstream Students(filename, ios::out);
    Students << FirstName << "\n" << LastName << "\n" << Age;

    cout << "\n\n";
   
    
    return 0;
}

I presume you are using MS-Windows. If not then possibly your operating system does not support commas and periods embedded in filenames.

I am using windows vista, and you are write about the commas and dots. But i can save the file manually with dots and commas as part of the filename and also when i removed the commas and dots from the program it created a file but not all 3 variable values were printed as filename.

I am also using Vista. Check the directory in which your program is running and I'll bet my boots that the file is there. If you remove all the commas and dots you will get this filename "testing101213txt"

it doesnt print the last digit i.e. 3 as part of the file name rest is all good.

i think there is some restriction on the output file name length. Any method to remove this restriction?

i think there is some restriction on the output file name length. Any method to remove this restriction?

The restriction is 260 characters, as defined in windows.h MAX_PATH. The problem is in your code.

hey thanks for your help, can you give me some ideas about how do i go in generating a number of files say 3 with the same name but the values of a,b and c been incremented by 1 e.g. test11.12.13 etc etc. Can you give me some ideas places.

You'll have to make changes to this line: sprintf(filename, "%s,%i.,%i.,%i.txt", FileName, a,b,c); I can say you: I know the solution, but please try it first, you'll learn a lot more from it.

Right I got that but there is a slight issue with incrementing a fraction number e.g. .01 now what I am trying to achieve is to incerement this .01 by .01 for a number of times and generate output files with the incremented number. For example a=.01 now when the loop runs for once it generates a file say called abc.01.txt next time i increments the value of a by .01 (i.e. now a=.02) and generate another file with name abc.02.txt and so on.

Please explain as i am struggling to achieve this.

Many thanks,

did you ever hear of the increment ++ operator a = a + 1; then call sprintf() again to reformat the string.

hi,
How do i go in writing the same file in visual c++ 2005. there are certain commands that dont work with VS C++. Can you please advise. The reason I am doing so because I had no success in printing all the variable values as part of the file name using Borland compiler.

Many thanks

you can use the same methods as above you just need to use the right include files and settings. first make a new win 32 console application and check the box for an empty project. add a new cpp file and include stdio.h like #include <stdio.h> . this will let you use the sprintf function.

Hi,

It doesnt seem to be working with VS 2005. I am getting a number of errors such as std is not a class or namespace name, cin undeclared identifier etc etc....

Hello,

I solved the errors i mentioned above. But one slight thing that is i can't make the for loop to work. It only creates one output file. Any reason why??

thanks

could you please re post the code you have now.

Hi,
Thank you very much for your reply and help. I've solved that problem. Can you guide me in the right direction for below please:

I want to create output files such that one of the three variables value is incremented at a time and that value is stored into the file. For example say i've the following

a=10, b=12,c=14 now wot i want to to acheive is to increment the value of a say by .1 and keep b and c the same for a number of times say 4. and next phase a and c are kept same but b and incremented by .1 and then save into the file and same for c. One way that I can think of doing this is by using loops and typing the same statements again and again which will make the code too lengthy.

I will appreciate any help and ideas.

Once again thanks for your help.

Cheers,

Can you please tell me how can I specify the output file path, the compiler is automatically saving all the files where the project is saved, I tried changing the path but got lods of errors.

Thanks

>>I tried changing the path but got lods of errors.
Post what you tried. Basically, its nothing more than this: char* path = "c:\\mydir\\myfile.txt"; . Note the double backslashes.

Hi,
Can you please tell me where do i use the above statement? Below are the statements that I tried:
sprintf(filename, "%s-%f-%f-%f.%s", "output", a,b,c, "txt");
ofstream data (filename, ios::app);

i have tried to put the file path in both locations i.e. ofstream data (c:\\files\\filename, ios::app)
also
in sprintf(filename, "%s-%f-%f-%f.%s", "c:\\files\\output", a,b,c, "txt");

can you please specify where exactly do i use that statement you mentioned and whether or not I've to make some changes in the above two statements.

Many thanks,

The line with sprintf() should have formatted the filename correctly. Why do you think it didn't?

Hi,
I didn't get what you pointed out there. Can you please type the statements so i can give it a try.

Many thanks,

Hi,
I didn't get what you pointed out there. Can you please type the statements so i can give it a try.

Many thanks,

Please read what you yourself posted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.