我的fstream总是失败

时间:2011-03-10 20:54:19

标签: c++

请帮助,我有以下小的剪辑代码来测试fsteam,但它总是失败,我的strace文件跟在代码之后。 它适用于unix但不适用于linux。 谢谢你的帮助。


#include <fstream.h>
#include <iostream.h>

using namespace std;

int main () {

        fstream filestr;

        filestr.open ("test.txt",ios::out | ios::in | ios::app );
        if(filestr.bad()) 
        {        
                cout << "Bad input." << endl; 
        }        
        if(filestr.fail()) 
        {        
                cout << "Open failed." << endl; 
        }       
        filestr << " dsgesvsvds";

        filestr.close();

        return 0;
}

execve("./a.out", ["./a.out"], [/* 83 vars */]) = 0
brk(0)                                  = 0x10754000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b8cf1e9d000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b8cf1e9e000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("tls/x86_64/libstdc++.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
open("tls/libstdc++.so.6", O_RDONLY)    = -1 ENOENT (No such file or directory)
open("x86_64/libstdc++.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
open("libstdc++.so.6", O_RDONLY)        = -1 ENOENT (No such file or directory)
open("/products/oracle/11.2.0/client/lib/tls/x86_64/libstdc++.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/products/oracle/11.2.0/client/lib/tls/x86_64", 0x7fffd262c040) = -1 ENOENT (No such file or directory)
open("/products/oracle/11.2.0/client/lib/tls/libstdc++.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/products/oracle/11.2.0/client/lib/tls", 0x7fffd262c040) = -1 ENOENT (No such file or directory)
open("/products/oracle/11.2.0/client/lib/x86_64/libstdc++.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/products/oracle/11.2.0/client/lib/x86_64", 0x7fffd262c040) = -1 ENOENT (No such file or directory)
open("/products/oracle/11.2.0/client/lib/libstdc++.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/products/oracle/11.2.0/client/lib", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/roscodev/rosco/bin/tls/x86_64/libstdc++.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/roscodev/rosco/bin/tls/x86_64", 0x7fffd262c040) = -1 ENOENT (No such file or directory)
open("/roscodev/rosco/bin/tls/libstdc++.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/roscodev/rosco/bin/tls", 0x7fffd262c040) = -1 ENOENT (No such file or directory)
open("/roscodev/rosco/bin/x86_64/libstdc++.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/roscodev/rosco/bin/x86_64", 0x7fffd262c040) = -1 ENOENT (No such file or directory)
open("/roscodev/rosco/bin/libstdc++.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/roscodev/rosco/bin", {st_mode=S_IFDIR|0775, st_size=12288, ...}) = 0
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=80518, ...}) = 0
mmap(NULL, 80518, PROT_READ, MAP_PRIVATE, 3, 0) = 0x2b8cf1e9f000
close(3)                                = 0
open("/usr/lib64/libstdc++.so.6", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0000\364\304\3528\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=976312, ...}) = 0
mmap(0x38eac00000, 3145600, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x38eac00000
mprotect(0x38eace6000, 2093056, PROT_NONE) = 0
mmap(0x38eaee5000, 36864, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xe5000) = 0x38eaee5000
mmap(0x38eaeee000, 73600, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x38eaeee000
close(3)                                = 0
open("tls/x86_64/libm.so.6", O_RDONLY)  = -1 ENOENT (No such file or directory)
open("tls/libm.so.6", O_RDONLY)         = -1 ENOENT (No such file or directory)
open("x86_64/libm.so.6", O_RDONLY)      = -1 ENOENT (No such file or directory)
open("libm.so.6", O_RDONLY)             = -1 ENOENT (No such file or directory)
open("/products/oracle/11.2.0/client/lib/libm.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/roscodev/rosco/bin/libm.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib64/libm.so.6", O_RDONLY)      = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`>\200\3438\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=615136, ...}) = 0
mmap(0x38e3800000, 2629848, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x38e3800000
mprotect(0x38e3882000, 2093056, PROT_NONE) = 0
mmap(0x38e3a81000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x81000) = 0x38e3a81000
close(3)                                = 0
open("tls/x86_64/libgcc_s.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("tls/libgcc_s.so.1", O_RDONLY)     = -1 ENOENT (No such file or directory)
open("x86_64/libgcc_s.so.1", O_RDONLY)  = -1 ENOENT (No such file or directory)
open("libgcc_s.so.1", O_RDONLY)         = -1 ENOENT (No such file or directory)
open("/products/oracle/11.2.0/client/lib/libgcc_s.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/roscodev/rosco/bin/libgcc_s.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib64/libgcc_s.so.1", O_RDONLY)  = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\36\200\3468\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=58400, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b8cf1eb3000
mmap(0x38e6800000, 2151784, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x38e6800000
mprotect(0x38e680d000, 2097152, PROT_NONE) = 0
mmap(0x38e6a0d000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xd000) = 0x38e6a0d000
close(3)                                = 0
open("tls/x86_64/libc.so.6", O_RDONLY)  = -1 ENOENT (No such file or directory)
open("tls/libc.so.6", O_RDONLY)         = -1 ENOENT (No such file or directory)
open("x86_64/libc.so.6", O_RDONLY)      = -1 ENOENT (No such file or directory)
open("libc.so.6", O_RDONLY)             = -1 ENOENT (No such file or directory)
open("/products/oracle/11.2.0/client/lib/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/roscodev/rosco/bin/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib64/libc.so.6", O_RDONLY)      = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\332\1\3438\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1722304, ...}) = 0
mmap(0x38e3000000, 3502424, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x38e3000000
mprotect(0x38e314e000, 2097152, PROT_NONE) = 0
mmap(0x38e334e000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x14e000) = 0x38e334e000
mmap(0x38e3353000, 16728, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x38e3353000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b8cf1eb4000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b8cf1eb5000
arch_prctl(ARCH_SET_FS, 0x2b8cf1eb4bf0) = 0
mprotect(0x38e334e000, 16384, PROT_READ) = 0
mprotect(0x38e3a81000, 4096, PROT_READ) = 0
mprotect(0x38eaee5000, 24576, PROT_READ) = 0
mprotect(0x38e2e1b000, 4096, PROT_READ) = 0
munmap(0x2b8cf1e9f000, 80518)           = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b8cf1e9f000
write(1, "Open failed.\n", 13Open failed.
)          = 13
exit_group(0)                           = ?

3 个答案:

答案 0 :(得分:1)

#include <fstream.h>
#include <iostream.h>

应该是

#include <fstream>
#include <iostream>

答案 1 :(得分:1)

您使用的是C ++,标准标题没有C中的.h扩展名。因此,您需要#include <fstream><iostream>

答案 2 :(得分:0)

如果我更正了#include错误(应该是#include <fstream>#include <iostream> - 没有.h),你的代码在Ubuntu 9.10下运行正常。但我怀疑这不是你的问题,因为这会导致编译错误。你有权限错误吗?如果我创建test.txt并删除写入权限,我会收到“打开失败”。消息。