Thread: extentsion name of a file

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    10

    extentsion name of a file

    when assigning a path name to a file stream is there any way to make the extension the part after the period a variable and tack it onto the name of the file?? If so how

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Not quite sure what you mean. If you want to extract the extension from a filename, look up fnsplit in dir.h. This is not an ANSI C function however.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    317
    I would take a look at strings, it would have answered your two posts:

    Code:
                   string filename = "myfile";
                   string ext = ".txt";
    	
                   filename+=ext;
    
                   fstream file;
                   file.open(filename, ios:: out);

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    208
    When I was making a login in program I need to do the same thing you are asking. I will post my signup function below. It shows how to do this.

    Code:
    void signup()
    { 
      char name[10],pass[10],filename[12],combo[19];//variables I need
    cout << "Enter 8 character user name:";
    cin >> name;
    cout << "Enter 8 characater password:";
    cin >> pass;
    strcpy(filename,name);//copy name to filename so i have two to manipulate
    strcat(filename,".txt");//add ".txt" to the end of file name
    strcpy(combo,name);//copy name to combo
    strcat(combo,".");//add a dot to the end of combo
    strcat(combo,pass);//add pass to the end of combo
    ofstream a_file(filename);//create a file named "username.txt"
    a_file<<combo;//write the text combo into it
    cout<<"\n\nYou Have Successfully Signed Up.\n\n";
    system("PAUSE");
    }
    Be sure to include <string.h>.
    If you need more explaination just say so and I will help you more with it. I think my commented code will be enuff though. and <fstream.h>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM