savoy@iro.umontreal.ca (Jacques Savoy) (10/04/89)
BUG : The flag a_createonly in GNU C++ Version 1.35
In the class File, i found a bug when i use the following
instruction :
if (stto.open(to, io_writeonly, a_createonly) == NULL) {
The flag a_createonly (create the file, fail if it already exists)
doesn't do the correct jog.
If the file doesn't exist, the file is not open.
If the file exists, the file is truncated.
Here is my program :
------------------------------------------------------------------------
// File bugfile.cc
#include <stream.h>
void error(char* msg)
{
cerr << msg;
exit(-1);
}
void copyFile(char* from, char* to)
{
File stfrom, stto;
char c, msg[128];
// Open for read only (io_readonly), fail if file doesn't exists (a_useonly)
if (stfrom.open(from, io_readonly, a_useonly) == NULL) {
strcpy(msg, "can't open file ");
strcat(msg, from);
error(msg);
}
// Open for write only (io_writeonly),
// fail if file doesn't exists (a_createonly)
if (stto.open(to, io_writeonly, a_createonly) == NULL) {
strcpy(msg, "can't open file ");
strcat(msg, to);
error(msg);
}
while (stfrom.get(c))
stto.put(c);
if (stfrom.close() != NULL) {
strcpy(msg, "can't close file ");
strcat(msg, from);
error(msg);
}
if (stto.close() != NULL) {
strcpy(msg, "can't close file ");
strcat(msg, to);
error(msg);
}
}
main(int argc, char* argv[])
{
char *prgname;
prgname = argv[0];
if (argc != 3) {
error("I need two arguments\n");
exit(-1);
}
cout << "Copy from file " << argv[1];
cout << " to file " << argv[2] << "\n";
copyFile(argv[1], argv[2]);
cout << "End of job\n";
exit(0);
}
// EOF File bugfile.cc
------------------------------------------------------------------------------
result of $ls t*
t1
test.c
test.cc
test1.cc
test2.cc
treebinptr.cc
ttt
result of $cat ttt
aaaa
bbbb
cccc
dddd
result of $cat t1
Hello
How
Are
You
Today
?
result of $bugfile ttt t2
Copy from file ttt to file t2
error in File t2: No such file or directory
can't open file t2
result of $bugfile ttt t1
Copy from file ttt to file t1
End of job
result of $cat t1
aaaa
bbbb
cccc
dddd
day
?
J.SAVOY (savoy@iro.umontreal.ca)