|
|
发表于 19-10-2008 01:24 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 19-10-2008 01:37 AM
|
显示全部楼层
原帖由 Wongkokchoy 于 19-10-2008 01:24 AM 发表 
我不是已经把 age和 name initiate了吗?
initiate data 跟在荧幕上显示文字是两回事吧?
你根本就没有任何接受输入的指令,也就是 cin, 所以你无法输入什么。
你根本就没有任何输出指令,也就是 cout, 所以你的荧幕上根本就没有任何输出。
你到底明不明白iostream?你整个program里面根本就没有任何console input /output (猜猜看 cin 和 cout 里面的 c 代表什么?),只有file i/o,你所有的输出都写进 file 里面,当然就什么都没出现在荧幕上啦。。。
可以请你,求求你,得空看一下Borland C++ 的 document file,了解一下什么是 iostream,以及 iostream 可以对应的输出/输入对象吗?
 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 19-10-2008 09:31 AM
|
显示全部楼层
大大对不起啊, 之前有一点忙,所以直接把朋友问我的东西直接post上来了,不过既然你还肯对我训话, 就是还关心我这个陌生人
无论如何, 还是要感谢大大帮我解决难题。
刚刚try ofstream 和 ifstream,都没问题了,可是现在在fstream 那边就很混淆了,- #include <iostream.h>
- #include <fstream.h>
- #include <stdlib.h>
- fstream file_in_out ("name.dat";
- void main()
- {
- char name[100];
- int matric;
- file_in.getline(name, 100);
- for (int x=0; !file_in.eof(); x++)
- {
- cout<<"name:\t\t\tmatric no.\n";
- cout<<name;
- cout<<"\t\t\t";
- cin>>matric;
- file_out<<matric<<endl;
- }
- file_in_out_close();
- }
复制代码 有三个error,
- Info : Compiling C:\Users\User\Desktop\test\name.cpp
- Error: name.cpp(5,27): Could not find a match for 'fstream::fstream(char *)'
- Error: name.cpp(11,9):Undefined symbol 'file_in'
- Error: name.cpp(18,11):Undefined symbol 'file_out'
- Error: name.cpp(20,19): Call to undefined function 'file_in_out_close'
复制代码
[ 本帖最后由 Wongkokchoy 于 19-10-2008 11:14 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 19-10-2008 11:30 AM
|
显示全部楼层
回复 62# geekman 的帖子
大大熄火
-----------------------------------
回复 63# Wongkokchoy 的帖子
fstream 是指file stream, file stream 可以是input ( read from file ) 或 output ( write to file), 这里你并没有指名是output stream 还是 input stream, compiler 当然出error啊。
在说, 你declare的事 file_in_out, 哪来的 file_in 与 file_out 呢? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 19-10-2008 11:52 AM
|
显示全部楼层
回复 64# onlylonly 的帖子
lecturer上课时说有fstream file_in_out 酱的东西,我就拿来试咯, 他说酱type的话, 就可以同时write 和 read file 了。
就好像说我从name。dat 那边拿名单, 然后加matric no 进去那个名单。
[ 本帖最后由 Wongkokchoy 于 19-10-2008 12:05 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 19-10-2008 12:05 PM
|
显示全部楼层
回复 65# Wongkokchoy 的帖子
的确是有, 只是你的写法写错了。
正确写法如
fstream file( <filename> , <mode> ;
e.g
- fstream file ( "abc" , ios::out ); // ofstream file( "abc" );
- fstream file ("abc" , ios::in); //ifstream file("abc")
- fstream file("abc, ios::in | ios::out ); // both input n output stream
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 19-10-2008 12:07 PM
|
显示全部楼层
回复 66# onlylonly 的帖子
我学的是酱的:
ifstream file_in ("name.dat")
和
ofstream file_out ("name.dat") |
|
|
|
|
|
|
|
|
|
|
发表于 19-10-2008 12:09 PM
|
显示全部楼层
回复 67# Wongkokchoy 的帖子
file_in 与 file_out 可以想像成一个variable, 实际上他们是stream。
他们未必是一定要 file_in / file_out 的, 你可以给他们什么名字都行, something, whatever, file_input, my_file, record, log .etc... |
|
|
|
|
|
|
|
|
|
|
发表于 19-10-2008 12:11 PM
|
显示全部楼层
回复 63# Wongkokchoy 的帖子
fstream file_in_out ("name.dat"); 这里你已经加入一个stream, 不过还未指名是input / output,
说以你可以写成
fstream file_in_out ("name.dat", ios::in) ; // or ios::out, or fstream::in or fstream::out or both ios::in | ios::out, or other, ios::app, ios::trunc etc.... |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 19-10-2008 12:16 PM
|
显示全部楼层
- #include <iostream.h>
- #include <fstream.h>
- #include <stdlib.h>
- fstream file ("name.dat", ios::in);
- void main()
- {
- char name[100];
- int matric;
- if (!file_in)
- {
- cout<<"Error opening file.\n";
- exit(1);
- }
- cout<<"Name\t\t\tmatric no.\n";
- for (int x=1; !file_in.eof(); x++)
- {
- file_in.getline(name, 100);
- cout<<""<<x<<" "<<name<<"";
- cout<<"\t\t\t";
- cin>>matric;
- }
- file_in.close();
- }
复制代码
error: undefined symbol "file_in"
我把全部的file_in 改成file_input 的话也不能。。。
[ 本帖最后由 Wongkokchoy 于 19-10-2008 12:17 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 19-10-2008 12:17 PM
|
显示全部楼层
fstream file ("name.dat", ios::in);
你 fstream file, 里面就必须用 file, 那来的 file_in?? |
|
|
|
|
|
|
|
|
|
|
发表于 19-10-2008 12:27 PM
|
显示全部楼层
回复 70# Wongkokchoy 的帖子
看来你对file handling 不很明白, 我写了一个 fstream 的 example, 你可以参考参考- #include <iostream>
- #include <fstream>
- using namespace std;
- int main()
- {
- //-------------- fstraem output --------------//
- // WRITE DATA TO FILE
- cout << "\n\n\nOUTPUT STREAM\n"
- << "Nothing show on screen, data is being written into file" << endl ;
- fstream file;
- file.open( "any.txt", ios::out);
- file << "asd\nase\nzxc" << endl;
- // any.txt
- //
- // asd
- // ase
- // zxc
- file.close();
- //---------------- fstream input -------------//
-
- cout << "\n\n\nINPUT STREAM\n";
- file.open( "any.txt", ios::in);
- string swap;
- while( file >> swap )
- cout << swap << endl;
- // data on screen
- //
- // asd
- // ase
- // zxc
- //
- file.close();
- //------------ BOTH INPUT OUTPUT ------------//
-
- cout << "\n\n\nBOTH INPUT AND OUTPUT\n";
- file.open( "any.txt", ios::out | ios::in ); // clear all data in any.txt
- file << "1st\n2nd\n3rd" << endl;
- // data on any.txt
- //
- // 1st
- // 2nd
- // 3rd
- file.seekg( 0, ios::beg ); // set get pointer back to begining of file
- while( file >> swap )
- cout << swap << endl;
- return 0;
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 19-10-2008 12:28 PM
|
显示全部楼层
回复 64# onlylonly 的帖子
呵呵,放心,我还没着火,只是有点脱力感罢了,也就是传说中的 Orz 。。。
其实很多时候,许多问题都能够从 IDE 的 document file 里面找到答案,里面不止有如何使用 IDE 的各种功能的解说,也包括了该 IDE 所支援的电脑语言的使用法,以及相关的 library functions 的使用法和各种解释,多看有益身心。 |
|
|
|
|
|
|
|
|
|
|
发表于 19-10-2008 12:33 PM
|
显示全部楼层
回复 73# geekman 的帖子
|
你用的是turbo 6 吗? 呵呵, 我的没有 documentation 的, 我用的是 vim + gcc ( 在 linux ), 我的documentation 就是google 。。 google 好东西啊。。 |
|
|
|
|
|
|
|
|
|
|
发表于 19-10-2008 12:38 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 19-10-2008 01:34 PM
|
显示全部楼层
大大,我很蒙
我试第一个fstream output.
我copy了,可是我的file还是空的。。。。 |
|
|
|
|
|
|
|
|
|
|
发表于 19-10-2008 01:47 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 19-10-2008 01:51 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 19-10-2008 01:52 PM
|
显示全部楼层
回复 78# onlylonly 的帖子
我create any.txt 也 create any.dat, 两个都是空的 |
|
|
|
|
|
|
|
|
|
|
发表于 19-10-2008 01:54 PM
|
显示全部楼层
回复 79# Wongkokchoy 的帖子
不用create 的, 则个program 自动对create file, 然后写入资料, 在读取资料。
整个是一个程式来得, 别把她分开。 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|