由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Quant版 - (更新)求助:C++里的fstream究竟该怎么用?
相关主题
A C++ quiz question问一个c++问题
求助:C++里的fstream究竟该怎么用? (转载)C++ 一问?
fstream 扫盲,谢谢!C++ ofstream binary output slow
请问一个入门级 dynamic memory 的问题C++ ofstream的一个诡异问题
C++ problemvirtual table存在memory的哪块啊?
Why no output file generate? What is wrong?C++在vector里找>50的数,怎么找?
一个极简单的程序求教现在C++的码农还有公司要吗?
C++: Static initialization dependency问一个基本os问题
相关话题的讨论汇总
话题: fstream话题: ofstream话题: std话题: outfile话题: using
进入Quant版参与讨论
1 (共1页)
w********0
发帖数: 1211
1
非常感谢sunnyedinken, jyjyjjyy, binrose,现在编译倒是通过了,无论是加上using std::fstream, 或者整个
的using namespace std都行。但新问题又来了,运行起来打不开文件。
我照着Lippman那本书写的:
ofstream outfile;
outfile.open("test.txt");
if (!outfile) {
cerr << "error: unable to open output file: \n ";
}
outfile.close();
运行起来总是告诉我 error: unable to open output file:
也就是文件根本就没打开。
我尝试着建立一个文件放在目录里(和source,header同一个目录下) ,也没用。
ifstream,ofstream都不行。
也尝试过定义的时候直接bind: ofstream outfile("test.txt"), 还是不行。
看来我实在太弱了。
************************************************************
今天想上机实践一下读写文件,最简短不过的小程序了,但是很失败。哪位有经验的帮
忙看一下,先谢过了。
我header file 里是这么写的:
#include
#include
#include
using std::cin;
using std::cout;
using std::string;
source file 里是这么写的:
#include "march_05_2011.h"
int main(){
string s;
s = "hello world! \n";
cout << s;
fstream testfile;
return 0;
}
可编译起来死活不认那个fstream, 改成ifstream, ofstream都没用。
如果把fstream testfile给去掉,则能打出hello world,说明纯粹是fstream那里出了
问题。究竟我漏掉了什么?看书没看出结果。
感觉很多时候书看得再多,上机还是有麻烦,因为很多东西书上都没有。
s*********n
发帖数: 41
2
std::fstream

【在 w********0 的大作中提到】
: 非常感谢sunnyedinken, jyjyjjyy, binrose,现在编译倒是通过了,无论是加上using std::fstream, 或者整个
: 的using namespace std都行。但新问题又来了,运行起来打不开文件。
: 我照着Lippman那本书写的:
: ofstream outfile;
: outfile.open("test.txt");
: if (!outfile) {
: cerr << "error: unable to open output file: \n ";
: }
: outfile.close();
: 运行起来总是告诉我 error: unable to open output file:

j******y
发帖数: 180
3
using namespace std;
ifstream xx;
ofstream xx;
b*****e
发帖数: 22
4
using std::fstream; 另外,你真正用到它时,还是选ifstream或ofstreeam。
w********0
发帖数: 1211
5
非常感谢你们几位,现在编译倒是通过了,无论是加上using std::fstream, 或者整个
的using namespace std都行。但新问题又来了,运行起来打不开文件。
我照着Lippman那本书写的:
ofstream outfile;
outfile.open("test.txt");
if (!outfile) {
cerr << "error: unable to open output file: \n ";
}
outfile.close();
运行起来总是告诉我 error: unable to open output file:
也就是文件根本就没打开。
我尝试着建立一个文件放在目录里(和source,header同一个目录下) ,也没用。
ifstream,ofstream都不行。
也尝试过定义的时候直接bind: ofstream outfile("test.txt"), 还是不行。
看来我实在太弱了。

【在 b*****e 的大作中提到】
: using std::fstream; 另外,你真正用到它时,还是选ifstream或ofstreeam。
b*****e
发帖数: 22
6
可能是路径问题,试试给出文件的全路径。
z**k
发帖数: 378
7
贴error message
你应该先看Thinking,再看Lippman
j********t
发帖数: 97
8
默认当前路径问题。
如果是在windows下命令行执行程序,把test文件和可执行文件放在同一目录下。
如果是在VC内按F5执行,请把test文件放在.sln文件所在的目录。
z****g
发帖数: 1978
9
...你VC里没设置debug里的working dir吧....
c******d
发帖数: 98
10
就是路径问题啦,用FULL 路径,要么你编译之前先设置好working dir。
l********y
发帖数: 1327
11
路径问题,或者你如果用vista不是admin级别,无权操作当前文件夹
把文件夹改成全部权限
H***a
发帖数: 735
12
如果当前路径下没有该文件,open能够create这个文件,所以不应该是路径问题.
这个是楼上说的文件夹修改权限的限制问题. 如果在Unix下, 通过改变这个文件夹的写
权限, LZ应该很容易能够比较出来.
j******y
发帖数: 180
13
outfile.open("xxx") will search the local directory for xxx. If exists, it
will be successful, else it will create a new one. Then you can run your code
successfully. The only possible issue is the directory does not exists or you do not have the privilege to create.
So, no problem with your code.
If you are under linux, use chmod to make writable.
H***a
发帖数: 735
14
file "xxx" doesn't need to exist, if you have write permission for that
directory. It will create the file "xxx" locally and won't fail.

【在 j******y 的大作中提到】
: outfile.open("xxx") will search the local directory for xxx. If exists, it
: will be successful, else it will create a new one. Then you can run your code
: successfully. The only possible issue is the directory does not exists or you do not have the privilege to create.
: So, no problem with your code.
: If you are under linux, use chmod to make writable.

1 (共1页)
进入Quant版参与讨论
相关主题
问一个基本os问题C++ problem
C++ questionWhy no output file generate? What is wrong?
请教一个boost::bind的问题一个极简单的程序求教
呼唤大侠们,我实在不能实现C++泛型的精神。C++: Static initialization dependency
A C++ quiz question问一个c++问题
求助:C++里的fstream究竟该怎么用? (转载)C++ 一问?
fstream 扫盲,谢谢!C++ ofstream binary output slow
请问一个入门级 dynamic memory 的问题C++ ofstream的一个诡异问题
相关话题的讨论汇总
话题: fstream话题: ofstream话题: std话题: outfile话题: using