Thread: Won't create new file.

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

    Won't create new file.

    So I'm still working on this project. I got the file saving feature working like I wanted it to a while back. Basically it creates an array of characters and then saves them to a file with the current date and time as the file name. This worked well for a few days.

    Then I added in serial port code(which I'll post later, because there's no simple examples around), and the file saving no longer works! One of the features is for the program to save the most recent 'packet' of characters to a file that's always there, so that a user can easily see what the last sent packet was. So the i/o works, but file creation does not.

    Code:
    time_t rawtime;
         struct tm * timeinfo;
         char buffer [80];
         memset(buffer, '\0', 80);
         time ( &rawtime );
         timeinfo = localtime ( &rawtime );
         cout << endl;
         string file2;
         string filePath = "C:\\packets\\sent";
         strftime(buffer,80, "%a_%b_%d__%H_%M_%S__%Y", timeinfo);
         string fileName = buffer;
         fileName += ".txt";
         file2 = filePath + "\\javacomport.txt";
         filePath += "\\" + fileName;
    
         string packetString = "";
         cout << filePath << endl;
         char fullName[100];
         char full2[80];
         for(int i = 0; i < filePath.length(); i++)
         {
                 fullName[i] = filePath[i];
         }
         for(int i = 0; i < file2.length(); i++)
         {
                 full2[i] = file2[i];
         }
         for(int i = 0; i < ready_sizepkt; i++)
         {
                 packetString += ready_packet[i];
         } 
         ofstream packetsent(fullName);      
         ofstream javaport(full2);
         if(!packetsent)
         {
              cout << "file not open\n";
         }
         packetsent << packetString << endl;
         javaport << packetString << endl;
         packetsent.close();
         javaport.close();
    I've been over and over this(I've had professors look at it too, and they don't see anything wrong). The thing that really gets me is that this worked a few days ago, and I don't remember changing anything in this part of the code.

    Hoping you guys can see something I've missed.
    -System_159

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try
    Code:
         ofstream packetsent(fullName, ios_base::out|ios_base::trunc);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Quote Originally Posted by matsp View Post
    Try
    Code:
         ofstream packetsent(fullName, ios_base::out|ios_base::trunc);
    --
    Mats
    Tried that, and it didn't work either. I did figure it out, though. Your idea of strictly defining the file interaction type gave me an idea that maybe I need to make sure that there's not something in my filepath/name array already. If I add

    Code:
    memset(fullName, '\0', 100);
    right after declaring fullName it works. Thanks for the help matsp, while it wasn't strictly your code that fixed it, you tipped me off to something that may.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    There is no need for the character arrays holding the filenames beyond an apparent belief that you must use them when opening the files... which is not true. You can use the string's c_str() member function to return a const char * pointer to the data in the string which can directly be used in the constructor for the ofstream objects in place of the character arrays. This should be a bit cleaner...
    Code:
    time_t rawtime;
    struct tm * timeinfo;
    char buffer [80];
    memset(buffer, '\0', 80);
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    cout << endl;
    
    strftime(buffer,80, "&#37;a_%b_%d__%H_%M_%S__%Y", timeinfo);
    string filePath = "C:\\packets\\sent\\";
    string fileName = filePath + buffer + ".txt";
    string file2 = filePath + "javacomport.txt";
    
    string packetString = "";
    cout << fileName << endl;
    
    for(int i = 0; i < ready_sizepkt; i++)
    {
        packetString += ready_packet[i];
    } 
    ofstream packetsent(fileName.c_str());      
    ofstream javaport(file2.c_str());
    if(!packetsent)
    {
        cout << "file not open\n";
    }
    packetsent << packetString << endl;
    javaport << packetString << endl;
    packetsent.close();
    javaport.close();
    You can probably also do something about that packetString loop depending on what ready_packet/ready_sizepkt is.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM