File output not working.

This is a discussion on File output not working. within the C++ Programming forums, part of the General Programming Boards category; Hey all, Been a while since I've done any file output, so I'm needing some help here. I basically just ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    163

    File output not working.

    Hey all,

    Been a while since I've done any file output, so I'm needing some help here.

    I basically just want to save a one line string to a file with the current date and time as the file name. Everything compiles and runs fine, but it just doesn't save.

    Code:
    time_t now;
         time(&now);
         
         string filePath = "C:\\Kysat\\Ground Station\\Sent Packets";
         string fileName = ctime(&now);
         fileName += ".txt";
         filePath += "\\" + fileName;
    
         string packetString = "";
         cout << filePath << endl;
         char fullName[100];
         for(int i = 0; i < filePath.length(); i++)
         {
                 fullName[i] = filePath[i];
         }
         for(int i = 0; i < (int)pkt_size; i++)
         {
                 packetString += packet[i];
         } 
         ofstream packetsent(fullName);
         packetsent << packetString << endl;
         packetsent.close();
    Any help on why it's not creating the file would be awesome. Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's quite likely that something in your filename is conflicting with the naming rules for the OS. For example, ":" is not allowed within Windows file-names. Other OS's and file-systems may have other rules. Try replacing any ":" with "_" or "-" or some such. Or use strftime() to format the name as you wish without ':' in the name.

    --
    Mats

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Why do you bother to create a char array version of the filename, from the look of it, without a \0 at the end as well.

    Use
    ofstream packetsent( filePath.c_str() );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    Why do you bother to create a char array version of the filename, from the look of it, without a \0 at the end as well.

    Use
    ofstream packetsent( filePath.c_str() );
    Good spot, Having some random garbage at the end will OBVIOUSLY not help making the name valid.

    --
    Mats

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Well, I went ahead and implemented what matsp suggested and it worked.

    Code:
    time_t rawtime;
         struct tm * timeinfo;
         char buffer [80];
    
         time ( &rawtime );
         timeinfo = localtime ( &rawtime );
         
         string filePath = "C:\\Kysat\\Ground Station\\Sent Packets";
         strftime(buffer,80, "&#37;a_%b_%d__%H_%M_%S__%Y", timeinfo);
         string fileName = buffer;
         fileName += ".txt";
         filePath += "\\" + fileName;
    
         string packetString = "";
         cout << filePath << endl;
         char fullName[100];
         for(int i = 0; i < filePath.length(); i++)
         {
                 fullName[i] = filePath[i];
         }
         for(int i = 0; i < (int)pkt_size; i++)
         {
                 packetString += packet[i];
         } 
         ofstream packetsent(fullName);
         packetsent << packetString << endl;
         packetsent.close();

    As for why I made a character array instead of doing filePath.c_string(), I tried that, but got compile errors, so decided to do something different. I may try again like that if I've got time later.

    Thanks for the help.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    As for why I made a character array instead of doing filePath.c_string(), I tried that, but got compile errors
    That's because it's: filePath.c_str() not .c_string()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 11:08 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 04:28 PM

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