-
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
-
Try
Code:
ofstream packetsent(fullName, ios_base::out|ios_base::trunc);
--
Mats
-
Quote:
Originally Posted by
matsp
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.
-
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, "%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.