File creating with string for name

This is a discussion on File creating with string for name within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a text editor using console and char array[100] strings. If this isn't possible, tell me ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    10

    File creating with string for name

    I am trying to make a text editor using console and char array[100] strings.

    If this isn't possible, tell me now My error code is as follows:

    Code:
        char fileName[100];
        cout << "What do you want you're .editor to be today?\n";
        cin.getline ( fileName, 100, ' ');
        ofstream file(fileName ".editor");

    The compiler error is 'expected ) before string constant'

    Is it possible to use c style strings in this case?
    Last edited by Loknar Gor; 09-23-2006 at 09:01 AM.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I'd do it like this -

    Code:
    std::string NameOfFile;
    std::cout<< "What do you want to call it: ";
    std::cin >> NameOfFile;
    
    // or std::getline( cin, NameOfFile );
    
    ofstream out ( (NameOfFile + ".editor").c_str() );
    
    // do stuff
    
    out.close();
    Yours doesn't work because

    >> (fileName ".editor");

    doesn't work. If you wish to remain with c style strings, instead of std::strings, use strcpy(), or strcat() or something. std::strings .c_str() returns a const char*, so it can be used for all sorts of things. They're great, research them, and you'll never use char*'s when there's a choice of use, IMO.
    Last edited by twomers; 09-23-2006 at 09:01 AM.

  3. #3
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    ofstream file(fileName ".editor");
    I don't think this is legal
    Try using strncat to append ".editor" to the end of filename, then try
    Code:
    ofstream file(filename);
    or you can use strings instead of char arrays and just add the ".editor" to the end of the filename.
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >ofstream file(fileName ".editor");
    There's nothing between fileName and ".editor", which in this case is a syntax error. C-style strings are harder to work with. If you were using C++ strings then the fix would be as simple as:
    Code:
    ofstream file((fileName + ".editor").c_str());
    But as it is, you need to concatenate ".editor" onto the end of fileName using something like strcat:
    Code:
    #include <cstring>
    
    // ...
    
    char fileName[100];
    
    cout << "What do you want you're .editor to be today?\n";
    
    // Leave room for the extension
    if (cin.getline (fileName, 93, ' ')) {
      strcat(fileName, ".editor");
    
      ofstream file(fileName);
    
      // ...
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Ok, thanks. I was just using C style because I didn't know how to get input to C++ style strings

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Heh

    #include <string>


  7. #7
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main() {
        string str;
        cout<<"Enter a string : ";
        getline(cin, str);
        cout<<"\n" <<str;
        cin.get();
        return 0;
    }
    Last edited by rwmarsh; 09-23-2006 at 09:15 AM.
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> cout"\n" <<str;

    Change to - cout << "\n" <<str;

    And the use of code tags is always good!

  9. #9
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    dang, I thought I edited that fast enough so no one would notice
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    [Offtopic] That's why a code tag button would be good for the quick reply!! [/offtopic]

    Still forgot the <<

    EDIT - Whoops, just edited in. heh.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Okay, there is another problem I don't know how to solve.

    I use a while loop to dictate wether to continue asking the user for input, or end at their request. If they type the number 1, the loop stops and the file is closed, program ends. The problem is that 1 is still sent to the file. Here is the loop:

    Code:
    while(toFile != "1")
        {
        cout << "Type in your text, pressing 'enter' when finished with the line (typing 1 will end the loop and close your file): \n";
        getline(cin, toFile);
        out << toFile;
        }

  12. #12
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Are you using CStrings or std strings? If it's C strings, you'll need to use strcmp()

  13. #13
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    You need a conditional statment to test if ToFile is a '1' or not, maybe something like this
    Code:
    while (true) {
        cout << "Type in your text, pressing 'enter' when finished with the line (typing 1 will end the loop and close your file): \n";
        getline(cin, toFile);
        if (toFile != "1")
            out << toFile;
        else
            break;
    }
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Std strings. I fixed it with an if statment before the line that prints to the file, though.

    EDIT: Typing the code tags in Quick Reply doesn't work ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 05:25 AM
  2. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  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

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