By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,657 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,657 IT Pros & Developers. It's quick & easy.

input and output questions about file

P: n/a
下面程序建立不了文件,不知道哪里出错了?

只想定义一个fstream类型来完成输入输出的工作.
#include<iostream>
#include<conio.h>
#include<fstream>

using namespace std;

int main()
{
fstream file("a.txt",fstream::in|fstream::out|fstream::app );
string s1,s2;

if(!file) cerr<<"error"<<endl; //why the program have
//error when connecting
up "a.txt"

s1="abcd 1234\n";
file<<s1; //why don't write in a.txt?
file.flush();
file.seekg(0);
file>>s2;
cout<<"s2="<<s2<<endl;//s2 is empty

file.close();

getch();
return 0;}

the program can run without any error,but the result is:

error

s2=

the program don't create file "a.txt",How do i do?

Feb 24 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Feb 24, 10:13 am, "Dic4...@163.com" <Dic4...@163.comwrote:
下面程序建立不了文件,不知道哪里出错了?

只想定义一个fstream类型来完成输入输出的工作.
#include<iostream>
#include<conio.h>
#include<fstream>

using namespace std;
<snip>
the program can run without any error,but the result is:

error

s2=

the program don't create file "a.txt",How do i do?
You go talk to the guys in comp.lang.c++
Just down the hall, second door on the right (just after the koolade
dispenser)

--
Lew
Feb 24 '07 #2

P: n/a
Di*****@163.com wrote:
下面程序建立不了文件,不知道哪里出错了?

只想定义一个fstream类型来完成输入输出的工作.
下面程序又不是C语言,你到comp.lang.c++去吧。

The program below is not even in the C language. I suggest you go to
comp.lang.c++

--
Simon.
Feb 24 '07 #3

P: n/a
On 2月24日, 下午11时41分, Simon Biber <n...@ralmin.ccwrote:
Dic4...@163.com wrote:
下面程序建立不了文件,不知道哪里出错了?
只想定义一个fstream类型来完成输入输出的工作.

下面程序又不是C语言,你到comp.lang.c++去吧。

The program below is not even in the C language. I suggest you go to
comp.lang.c++

--
Simon.
I see.
Feb 24 '07 #4

P: n/a
Di*****@163.com wrote:
涓嬮潰绋嬪簭寤虹珛涓嶄簡鏂囦欢,涓嶇煡閬撳摢閲屽嚭 敊浜?

鍙兂瀹氫箟涓涓猣stream绫诲瀷鏉ュ畬鎴愯緭鍏ヨ緭 嚭鐨勫伐浣.
#include<iostream>
#include<conio.h>
#include<fstream>
<iostreamand <fstreamare C++ headers. They are not part of C;
questions involving anything associated with those headers belong in
<news:comp.lang.c++>, not <news:comp.lang.c>.

<conio.his not a standard C or C++ header. Any questions involving
anthing associated with that header should go to an
implementation-specific newgroup, mailing-list, or technical support.
They do not belong here (comp.lang.c) or in comp.lang.c++.
>
using namespace std;
That is a syntax error in C.
Feb 24 '07 #5

P: n/a
On 24 Feb 2007 07:13:08 -0800, in comp.lang.c , "Di*****@163.com"
<Di*****@163.comwrote:
>using namespace std;
comp.lang.c++ is down the hall, second left.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 24 '07 #6

P: n/a
"Di*****@163.com" wrote:
>
.... snip incomprehensible ...
>
#include<iostream>
#include<conio.h>
#include<fstream>

using namespace std;

int main()
{
fstream file("a.txt",fstream::in|fstream::out|fstream::app );
string s1,s2;

if(!file) cerr<<"error"<<endl; //why the program have
//error when connecting
up "a.txt"

s1="abcd 1234\n";
file<<s1; //why don't write in a.txt?
file.flush();
file.seekg(0);
file>>s2;
cout<<"s2="<<s2<<endl;//s2 is empty

file.close();

getch();
return 0;}

the program can run without any error,but the result is:
Don't believe you.

junk.c:1:19: iostream: No such file or directory (ENOENT)
junk.c:2:18: conio.h: No such file or directory (ENOENT)
junk.c:3:18: fstream: No such file or directory (ENOENT)
junk.c:5: parse error before "namespace"
junk.c:5: warning: type defaults to `int' in declaration of `std'
junk.c:5: ISO C forbids data definition with no type or storage
class
junk.c: In function `main':
junk.c:9: `fstream' undeclared (first use in this function)
junk.c:9: (Each undeclared identifier is reported only once
junk.c:9: for each function it appears in.)
junk.c:9: parse error before "file"
junk.c:10: `string' undeclared (first use in this function)
junk.c:12: `file' undeclared (first use in this function)
junk.c:12: `cerr' undeclared (first use in this function)
junk.c:12: `endl' undeclared (first use in this function)
junk.c:12: parse error before '/' token
junk.c:17: `s1' undeclared (first use in this function)
junk.c:17: parse error before '/' token
junk.c:17:24: missing terminating ' character
junk.c:17:24: warning: character constant too long
junk.c:20: `s2' undeclared (first use in this function)
junk.c:21: `cout' undeclared (first use in this function)
junk.c:21: parse error before '/' token
junk.c:25: warning: implicit declaration of function `getch'

c != C++

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Feb 24 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.