佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

楼主: Wongkokchoy

关于C++的问题。。。。

[复制链接]
发表于 19-10-2008 01:24 AM | 显示全部楼层
原帖由 Wongkokchoy 于 18-10-2008 08:37 PM 发表
我试着改了, toupper可以用, 可是还是不能够cout


然后我的朋友说borland C++ 的function call, function definition一定要放在main 上面, 不可以放phototype, main 之后才 给function definition。 你认 ...


更正:PROTOTYPE,不是 Phototype....

只要你在main()之前有declare你的function,你就可以把function code放在main()后面。
  1. void myfunction(void); //function declaration

  2. void main(void)
  3. {
  4.    ///do something;
  5. }

  6. void myfunction(void)
  7. {
  8.    //do something;
  9. }
复制代码
回复

使用道具 举报


ADVERTISEMENT

发表于 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 那边就很混淆了,
  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdlib.h>

  4. fstream file_in_out ("name.dat";

  5. void main()
  6. {
  7. char name[100];
  8. int matric;
  9. file_in.getline(name, 100);
  10. for (int x=0; !file_in.eof(); x++)
  11. {
  12. cout<<"name:\t\t\tmatric no.\n";
  13. cout<<name;
  14. cout<<"\t\t\t";
  15. cin>>matric;
  16. file_out<<matric<<endl;
  17. }
  18. file_in_out_close();
  19. }
复制代码
有三个error,
  1. Info : Compiling C:\Users\User\Desktop\test\name.cpp
  2. Error:  name.cpp(5,27): Could not find a match for 'fstream::fstream(char *)'
  3. Error:  name.cpp(11,9):Undefined symbol 'file_in'
  4. Error:  name.cpp(18,11):Undefined symbol 'file_out'
  5. 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


  1. fstream file ( "abc" , ios::out ); // ofstream file( "abc" );
  2. fstream file ("abc" , ios::in); //ifstream file("abc")

  3. fstream file("abc, ios::in | ios::out ); // both input n output stream
复制代码
回复

使用道具 举报

Follow Us
 楼主| 发表于 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...
回复

使用道具 举报


ADVERTISEMENT

发表于 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 | 显示全部楼层
  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdlib.h>

  4. fstream file ("name.dat", ios::in);

  5. void main()
  6. {
  7. char name[100];
  8. int matric;
  9. if (!file_in)
  10. {
  11. cout<<"Error opening file.\n";
  12. exit(1);
  13. }
  14. cout<<"Name\t\t\tmatric no.\n";
  15. for (int x=1; !file_in.eof(); x++)
  16. {
  17. file_in.getline(name, 100);
  18. cout<<""<<x<<" "<<name<<"";
  19. cout<<"\t\t\t";
  20. cin>>matric;
  21. }
  22. file_in.close();
  23. }
复制代码



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, 你可以参考参考
  1. #include <iostream>
  2. #include <fstream>

  3. using namespace std;

  4. int main()
  5. {
  6.     //-------------- fstraem output --------------//
  7.     // WRITE DATA TO FILE

  8.     cout << "\n\n\nOUTPUT STREAM\n"
  9.          << "Nothing show on screen, data is being written into file" << endl ;

  10.     fstream file;

  11.     file.open( "any.txt", ios::out);


  12.     file << "asd\nase\nzxc" << endl;
  13.     // any.txt
  14.     //
  15.     // asd
  16.     // ase
  17.     // zxc

  18.     file.close();

  19.     //---------------- fstream input -------------//
  20.    
  21.     cout << "\n\n\nINPUT STREAM\n";

  22.     file.open( "any.txt", ios::in);

  23.     string swap;

  24.     while( file >> swap )
  25.         cout << swap << endl;

  26.     // data on screen
  27.     //
  28.     // asd
  29.     // ase
  30.     // zxc
  31.     //
  32.     file.close();

  33.     //------------ BOTH INPUT OUTPUT ------------//
  34.    
  35.     cout << "\n\n\nBOTH INPUT AND OUTPUT\n";
  36.     file.open( "any.txt", ios::out | ios::in ); // clear all data in any.txt

  37.     file << "1st\n2nd\n3rd" << endl;
  38.     // data on any.txt
  39.     //
  40.     // 1st
  41.     // 2nd
  42.     // 3rd


  43.     file.seekg( 0, ios::beg ); // set get pointer back to begining of file

  44.     while( file >> swap )
  45.         cout << swap << endl;

  46.      return 0;
  47. }
复制代码
回复

使用道具 举报

发表于 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 | 显示全部楼层

回复 73# geekman 的帖子

老大, 有个问题问你,

你在这行多久了?
回复

使用道具 举报

 楼主| 发表于 19-10-2008 01:34 PM | 显示全部楼层
大大,我很蒙

我试第一个fstream output.
我copy了,可是我的file还是空的。。。。
回复

使用道具 举报


ADVERTISEMENT

发表于 19-10-2008 01:47 PM | 显示全部楼层

回复 74# onlylonly 的帖子

linux rulez~!
回复

使用道具 举报

发表于 19-10-2008 01:51 PM | 显示全部楼层

回复 76# Wongkokchoy 的帖子

哪个file空的? 我的output是在 any.txt 里面的。

回复 77# solidx 的帖子
回复

使用道具 举报

 楼主| 发表于 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, 然后写入资料, 在读取资料。

整个是一个程式来得, 别把她分开。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-12-2025 06:46 PM , Processed in 0.156833 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表