Adding .txt to a char

This is a discussion on Adding .txt to a char within the C++ Programming forums, part of the General Programming Boards category; My confused mind is now real stuck! How do you add ".txt" to a char? I wanted to do something ...

  1. #1
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408

    Adding .txt to a char

    My confused mind is now real stuck!
    How do you add ".txt" to a char?
    I wanted to do something like this:

    charA = charB + ".txt"

    but since it's a char it aint working.
    Somebody please help me....

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Well, I'm assuming it's a char array, so just use strcat.

  3. #3
    0x01
    Join Date
    Sep 2001
    Posts
    88

    Try.

    Code:
    #include "stdio.h"
    #include "conio.h"   // getch ()
    #include "windows.h" // strlen()
    
    int get_strlen(char * string);
    
    int main()
    {
    	char filext[80] = { NULL }; // { NULL } sets all the subscript to NULL
    	int     idx = 0;
    
    	printf("\n Type a filename w/out an ext. : ");
    	gets(filext);
    
    	printf("\n filext = [ %s ] ",filext);
    	
    	// strlen() returns the number of chars in filext
    	idx = strlen(filext);
    	// idx = get_strlen(filext);
    
    	filext[idx] = '.';
    	filext[++idx] = 't';
    	filext[++idx] = 'x';
    	filext[++idx] = 't';
    
    	printf("\n filext = [ %s ] ",filext);
    	printf("\n ------\n\n");
    
    	getch(); // press any key to continue
    
    	return 0;
    }
    
    // Use this is you don't have strlen() or windows.h
    int get_strlen(char * str)
    {
    	int nbr = 0;
    
    	// \0 = NULL
    	while(str[nbr] != '\0')
    		nbr++;
    
    	return nbr;
    }
    *Note : you may want check 'filext', to see if the user has already entered an extenion. btw - code was written in C

  4. #4
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Ack!
    All that for just .txt?
    yikes....
    Well thank you.

  5. #5
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    that sure is...advanced...
    Paro

  6. #6
    0x01
    Join Date
    Sep 2001
    Posts
    88

    hm...

    Well, I kinda made a sample program... all you gotta do is
    --

    - Get the number of chars typed(basically your finding out where the last char is in the array)
    - From there you add the extention subscript by subscript

    Its that simple.

    I only added the strlen() and the get_strlen() thing in there just to make it easier.

  7. #7
    Unregistered
    Guest
    cout << "enter the file path and the program will add the file extension." << endl;
    char input[80];
    cin >> input;
    char ext[5] = ".txt";
    char filename[86];
    strcpy(filename, input);
    strcat(filename, ext);
    cout << "the full file name will be " << filename;

  8. #8
    Unregistered
    Guest
    if you use string class from STL.

    cout << "enter file name";
    string filename;
    cin >> filename;
    filename = filename + ".txt";

  9. #9
    0x01
    Join Date
    Sep 2001
    Posts
    88

    ...

    hm.. i suggest everyone use the above methods (not mine) ...
    I have never used strcat() before, it looks a lot better .. interesting.

  10. #10
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408

    Unhappy ErionD needs help continued

    For some reason the program just skips the code after the point defining and just restarts the loop.
    I cant find any reason for the program to do so so i hoped you could help me a little more...

    Code:
    #include <iostream>
    #include <cstring>
    #include <fstream>
    #include <stdio.h>
    
    int get_strlen(char * str)
    {
       int nbr = 0;
    
       while (str[nbr] != '\0')
       nbr++;
    
       return nbr;
    }
    
    
    int main() {
    
    char* filename;
    int idx = 0;
    int startoption;
    char* saveoption;
    char name[50];
    int age;
    char race[50];
    char raceexample[10];
    int classopt;
    char* charclass;
    char weapon;
    int points;
    points = 20;
    int Str,Con,Int,Dex,Cha;
    int strAdd,conAdd,intAdd,dexAdd,chaAdd;
    int loopon;
    loopon = 30;
    
    while(loopon == 30){ //start loop
              cout << "Hello and welcome to Daniel Bjorklunds RPG game.";
              cout << "What would you want to do?\n";
              cout << "1. Create a character\n";
              cout << "2. Load an existing character\n";
              cout << "3. Quit\n";
    
    cin >> startoption;
    switch(startoption) { //start switch
    case 1:
    cout << "\n-----CHARACTER CREATOR-----\n\n";
    cout << "Choose a name for your character: ";
    cin >> name;
    cout << endl <<"You have chosen " << name << " as your character" << endl;
    cout << "\n\aNext you need to choose an age for your character.\n";
    cout << "The age depends upon your characters race (decided later).\n";
    cout << "For example, humans can be from 15-50 while elves can be more than 2000 years old.\r";
    cout << "Now choose an age: ";
    cin >> age;
    cout << endl <<"Your age is " << age << endl;
    cout << "\n\aAnd now for the characters race.\n";
    cout << "Choose whatever you want (within reasonable limits).\n";
    cout << "\nExamples: Human, Elf, Dwarf, Goblin, Orc, Pixie\n";
    cout << "Choose a race: ";
    cin >> race;
    cout << endl <<"Your race is " << race << endl;
    cout << "Now for your characters class...\n";
    cout << "Here are the classes you can choose from:\n\n";
    cout << "1. Warrior, 2. Magician, 3. Ranger\n\n";
    cout << "Now choose: ";
    cin >> classopt;
    if(classopt == 1) {
    cout << endl << "You choose to be a Warrior!\n\n";
    Str = 12;
    Con = 12;
    Int = 7;
    Dex = 9;
    Cha = 8;
    charclass = "Warrior";
    }
    if(classopt == 2) {
    cout << endl << "You choose to be a Magician!\n\n";
    Str = 6;
    Con = 8;
    Int = 14;
    Dex = 9;
    Cha = 11;
    charclass = "Magician";
    }
    if(classopt == 3) {
    cout << endl << "You choose to be a Ranger!\n\n";
    Str = 10;
    Con = 10;
    Int = 9;
    Dex = 10;
    Cha = 9;
    charclass = "Ranger";
    }
    
    cout << "Now we are going to distribute some ability points.\n";
    cout << "Right now your abilitys are:\n\n";
    cout << "Strength (Str): " << Str;
    cout << "\nConstitution (Con): " << Con;
    cout << "\nDexterity (Dex): " << Dex;
    cout << "\nIntelligence (Int): " << Int;
    cout << "\nCharisma (Cha): " << Cha;
    cout << "\n\nYou have " << points << " to distribute.\n";
    cout << "You can only have a maximum of 18 points in each skill.\n\n";
    cout << "Str: " << Str << "     Points: " << points;
    cout << "\n\nHow much would you like to add to your Strength?\n";
    cout << "Add: ";
    cin >> strAdd;
    Str = Str + strAdd;
    points = points - strAdd;
    cout << endl << "Your Strength is now: " << Str << ".\n\n\n" << endl;
    cout << "Con: " << Con << "     Points: " << points;
    cout << "\n\nHow much would you like to add to your Constitution?\n";
    cout << "Add: ";
    cin >> conAdd;
    points = points - conAdd;
    Con = Con + conAdd;
    cout << endl << "Your Constitution is now: " << Con << ".\n\n\n" << endl;
    cout << "Dex: " << Dex << "     Points: " << points;
    cout << "\n\nHow much would you like to add to your Dexterity?\n";
    cout << "Add: ";
    cin >> dexAdd;
    Dex = Dex + dexAdd;
    points = points - dexAdd;
    cout << endl << "Your Dexterity is now: " << Dex << ".\n\n\n" << endl;
    cout << "Int: " << Int << "     Points: " << points;
    cout << "\n\nHow much would you like to add to your Intelligence?\n";
    cout << "Add: ";
    cin >> intAdd;
    Int = Int + intAdd;
    points = points - intAdd;
    cout << endl << "Your Intelligence is now: " << Int << ".\n\n\n";
    cout << "Cha: " << Cha << "     Points: " << points;
    cout << "\n\nHow much would you like to add to your Charisma?\n";
    cout << "Add: ";
    cin >> chaAdd;
    Cha = Cha + chaAdd;
    points = points - chaAdd;
    cout << endl << "Your Charisma is now: " << Cha;
    cout << "\n\nDo you want to save this character?\n";
    cin >> saveoption;
    if(saveoption == "yes") {
    filename = name;
    idx = get_strlen(filename);
    filename[idx] = '.';
    filename[++idx] = 't';
    filename[++idx] = 'x';
    filename[++idx] = 't';
    ofstream charfile(filename);
    charfile << name << "\n";
    charfile << age << "\n";
    charfile << race << "\n";
    charfile << charclass << "\n";
    charfile << 1 << "\n";
    charfile << 0 << "\n";
    charfile << weapon << "\n";
    charfile << Str << "\n";
    charfile << Con << "\n";
    charfile << Dex << "\n";
    charfile << Int << "\n";
    charfile << Cha;
    charfile.close();
    cout << "Your character has been succesfully saved in the file:\n\n";
    cout << filename;
    }
    continue;
    case 2:
    cout << "That function is unavailable right now.\n\n";
    continue;
    
    case 3:
    break;
     }
     break;
     }
    return 0;
    }

  11. #11
    Unregistered
    Guest
    filename is a char * and name is a char[]. If you try to assign name to filename you will probably give filename the address of name[0], which is not the same assigning name to filename. You cannot assign one c_style string to another using the assignment operator, ever. Use strcpy() in the cstring header file

    char filename[50];
    char name[50];

    strcpy(filename, name);

    Note that filename must have enough memory to hold the entire string in name in order for this to work. Declaring filename to be a pointer doesn't declare memory for holding a string, just a single char (unless you use dynamic memory to declare the pointer, but that's a different story).

    Note also that in order to make name a string after adding .txt to it the way you did you need to add a terminating null char. As it is name is a straight char array, not a null terminated char array, so it can't be used at all, ever, as a string.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >char* saveoption;
    Just a pointer. Should be:
    char saveoption[5];

    >if(saveoption == "yes") {
    Should be:
    if(strcmp(saveoption,"yes") == 0) {

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  2. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 10:40 PM
  3. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 12:57 PM

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