open file for different name everytime it runs

This is a discussion on open file for different name everytime it runs within the C++ Programming forums, part of the General Programming Boards category; Code: char id[10]; cout << "enter file name" cin.getline(id,10); if i entered "123456", how to wirte the syntax for openning ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    open file for different name everytime it runs

    Code:
    char id[10];
    cout << "enter file name"
    cin.getline(id,10);
    if i entered "123456", how to wirte the syntax for openning the file 123456.txt?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    use strcat() from string.h or cstring. Look it up in your helpfiles.
    Make an attempt. If you find its broken and u cant fix it then repost in this thread.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    mmmmm...what i mean is.....

    Code:
    char id[10];
    cout << "please enter the file name:";
    cin.getline(id,10);
    if i entered 123456,
    what i mean is how to write the syntax so that the progam can reconize what i type as:

    ifstream file1(C:\\dir1\\dir2\\123456.txt) ???

    strcat() is to combine two string right??so i think it is different from what i am trying to do.....

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well dont you think strcat(input,".txt"); might be a good starting point?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    ...i can make the statement...but cant create the file...

    Code:
    char id[10]="565656";
    char buf1[100] = "\"C:\\testing\\script\\data\\";
    
    strcat(buf1,id);
    strcat(buf1,".txt\"");
    
    cout << buf1 ;
    
    ofstream filex(buf1);
    filex << "testing";
    filex.close();
    while the output of buf1 is "C:\testing\script\data\565656.txt"
    but the file have not created in the directory.

    is the problem happened in ofstream filex(buf1)???

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    problem fixed

    problem fixed....i should not add the " sign at the beginning and the end of C:\.........

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    all you needed to do was put it into a string and then use the + operator to add the ".txt"
    C++ can hurt.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    no it cant

    i have tried that before but it cant......

    or can you send me the code?

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    for example
    Code:
    fstream instream;
    string filename;
    string name;
    cin>>name;
    string addon = ".txt";
    filename = name + addon;
    instream.open(filename.c_str())
    ...
    and so on
    C++ can hurt.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Don't confuse the poor guy with STL...he should atleast implement his own String class first

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21