input and output questions about file

D

Dic4000

ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË?

Ö»Ï붨ÒåÒ»¸öfstreamÀàÐÍÀ´Íê³ÉÊäÈëÊä³öµÄ¹¤×÷.
#include<iostream>
#include<conio.h>
#include<fstream>

using namespace std;

int main()
{
fstream file("a.txt",fstream::in|fstream::eek:ut|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?
 
L

Lew Pitcher

ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË?

Ö»Ï붨ÒåÒ»¸öfstreamÀàÐÍÀ´Íê³ÉÊäÈëÊä³öµÄ¹¤×÷.
#include<iostream>
#include<conio.h>
#include<fstream>

using namespace std;
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)
 
S

Simon Biber

ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË?

Ö»Ï붨ÒåÒ»¸öfstreamÀàÐÍÀ´Íê³ÉÊäÈëÊä³öµÄ¹¤×÷.

ÏÂÃæ³ÌÐòÓÖ²»ÊÇCÓïÑÔ£¬Äãµ½comp.lang.c++È¥°É¡£

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

Dic4000

ÏÂÃæ³ÌÐòÓÖ²»ÊÇCÓïÑÔ£¬Äãµ½comp.lang.c++È¥°É¡£

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

I see.
 
M

Martin Ambuhl

下é¢ç¨‹åºå»ºç«‹ä¸äº†æ–‡ä»¶,ä¸çŸ¥é“哪里出错了?

åªæƒ³å®šä¹‰ä¸€ä¸ªfstream类型æ¥å®Œæˆè¾“入输出的工作.
#include<iostream>
#include<conio.h>
#include<fstream>

<iostream> and <fstream> are C++ headers. They are not part of C;
questions involving anything associated with those headers belong in
<not <
<conio.h> is 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.
 
M

Mark McIntyre

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
 
C

CBFalconer

.... snip incomprehensible ...

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

using namespace std;

int main()
{
fstream file("a.txt",fstream::in|fstream::eek:ut|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++
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top