Thread: File output not working.

  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
    39,656
    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.

  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-21-2008, 11:19 PM
  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, 10:08 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM