Thread: Names of Text Files

  1. #1
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560

    Names of Text Files

    How do I create a text file with the name of a string that was input by the user?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    75
    Code:
    #include <fstream.h>
    #include <iostream.h>
    
    int main()
    {
      char str[30];
      cin>>str;
      ofstream a_file(str);
      //Whatnot...
      return 0;
    }
    I was born; I shall die. Between those two events there is life. I don't know why, and I don't question it; I merely live.

  3. #3
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    PHP Code:

    #include <fstream.h>
    #include <iostream.h>

    int main()
    {
      
    char str[30];
      
    cin>>str;

      
    char str2[34];
      for (
    int i 0i<=30i++)
      {
         
    str2[i]=str[i];
      }

      
    str2[31]='.';
      
    str2[32]='t';
      
    str2[33]='x';
      
    str2[34]='t';

      
    //took str, copied it, added .txt (called str2)

      
    ofstream a_file(str2);
      
    a_file << "sadfasf";
      return 
    0;

    It doesn't work.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
      char str[30];
      cin>>str;
      strcat(str,".txt");
      ofstream a_file(str);
      a_file << "sadfasf";

  5. #5
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Of course it doesn't work. Try:

    ofstream f;

    f.open (.....);
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  6. #6
    Unregistered
    Guest
    char str[30];
    cin >> str;

    char str2[34];
    for (int i = 0; i<=30; i++)
    {
    str2[i]=str[i];
    }

    str2[31]='.';
    str2[32]='t';
    str2[33]='x';
    str2[34]='t';

    Two problems with this approach.

    1) what if the user inputs only 4 char instead of thirty. Say the input is:

    data

    that means that

    str[0] is d
    str[1] is a
    str[2] is t
    str[3] is a
    str[4] is 0 or '\0', whatever you call it

    but what is str[5] through str[29]? they are zilch, nada, undefined, garbage. therefore when you copy all 30 elements of str into str2 you copy the same worthlessness. Only copy the used elements of str, not all the elements of str (use strlen() to figure out how many elements were used) or use the strcpy() function to do the copying for you.

    2) the second problem is this line:

    str2[34] = 't';

    there are actually two problems here, too. First, there is no element str2[34] in str2. Since str2 is declared as having 34 elements by this line:

    char str2[34];

    the last element of str2 is str2[33]. Remember, the values of array indexes start at zero, not one. So valid indexes for str2 range from 0 to 33, not 0 t0 34, or even 1 to 34.

    The second problem is that in order to be a string, str2 needs to have the null terminating char and str2 doesn't have a null terminating char. Therefore, str2 is a char array, but it is not a string, and therefore it can't be used when trying to open an ifstream.

    your syntax with the ofstream is appropriate otherwise.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 11:21 AM
  2. Replies: 5
    Last Post: 02-11-2008, 01:36 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM