免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1887 | 回复: 9
打印 上一主题 下一主题

開file時有問題 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-05-16 08:36 |只看该作者 |倒序浏览
#include <iostream>;
#include <fstream>;
#include <iomanip>;
using namespace std;

int main()
{
        string str1,str2;
        char temp[100],buffer[100];
        ifstream openFile;
        ofstream writeFile;
        while(1)
        {
        cout << "Enter a input file name: ";
        cin >;>;str1;
        openFile.open(str1.c_str());

        if (!openFile)
        {
        cout<< "Error in opening the input file! \n\n";
        openFile.clear();
        }
        else
        break;
        }
        while(1)
        {
        cout << "Enter a output file name: ";
        cin >;>; str2;
        writeFile.open(str2.c_str(),ios::app);

        if (!writeFile)
        {
        cout<< "Error in opening the input file! \n\n";
        openFile.clear();
        }
        else
        break;
        }
        while (openFile.getline(buffer,100))
                writeFile << buffer;
       
        openFile.close();
        writeFile.close();
        return 0;
}

论坛徽章:
0
2 [报告]
发表于 2003-05-16 08:37 |只看该作者

開file時有問題

Deleting intermediate files and output files for project 'Assignment 2 - Win32 Debug'.
--------------------Configuration: Assignment 2 - Win32 Debug--------------------
Compiling...
2.cpp
d:\program files\microsoft visual studio\myprojects\assignment 2\2.cpp(15) : error C2679: binary '>;>;' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>;,class std::allocator<cha
r>; >;' (or there is no acceptable conversion)

Error executing cl.exe.

Assignment 2.exe - 2 error(s), 0 warning(s)

论坛徽章:
8
白羊座
日期:2015-01-21 18:35:03巳蛇
日期:2015-02-03 17:30:37处女座
日期:2015-02-03 17:31:02羊年新春福章
日期:2015-02-03 17:31:21巨蟹座
日期:2015-02-05 16:01:06申猴
日期:2015-02-05 16:01:31摩羯座
日期:2015-02-05 16:01:41酉鸡
日期:2015-02-05 16:02:37
3 [报告]
发表于 2003-05-16 09:35 |只看该作者

開file時有問題

No error:

F:\C\Copy>; mycc open
==》 C++源文件【open.cpp】编译完毕!
==》 开始执行可执行文件【open.exe】!
Enter a input file name: feeling
Error in opening the input file!

Enter a input file name: open.cpp
Enter a output file name: feeling.cpp
==》 可执行文件【open.exe】执行完毕!

没有什么问题啊!
不过要稍微修改一下,不然拷贝后的文件没有换行

  1. while (openFile.getline(buffer,100))
  2.         writeFile << buffer << endl;
复制代码

论坛徽章:
0
4 [报告]
发表于 2003-05-16 09:56 |只看该作者

開file時有問題

但我compile時真的compile不到呀
會不會是我的compiler有問題呢...
我用的是vc++

论坛徽章:
8
白羊座
日期:2015-01-21 18:35:03巳蛇
日期:2015-02-03 17:30:37处女座
日期:2015-02-03 17:31:02羊年新春福章
日期:2015-02-03 17:31:21巨蟹座
日期:2015-02-05 16:01:06申猴
日期:2015-02-05 16:01:31摩羯座
日期:2015-02-05 16:01:41酉鸡
日期:2015-02-05 16:02:37
5 [报告]
发表于 2003-05-16 11:23 |只看该作者

開file時有問題

我用的是Borland的编译器:
Borland C++ 5.6.2 for Win32

上面的输出格式看起来与该编译器的标准输出信息有所不同是因为我自己将它的编译过程稍微改造了一下

论坛徽章:
8
白羊座
日期:2015-01-21 18:35:03巳蛇
日期:2015-02-03 17:30:37处女座
日期:2015-02-03 17:31:02羊年新春福章
日期:2015-02-03 17:31:21巨蟹座
日期:2015-02-05 16:01:06申猴
日期:2015-02-05 16:01:31摩羯座
日期:2015-02-05 16:01:41酉鸡
日期:2015-02-05 16:02:37
6 [报告]
发表于 2003-05-16 13:45 |只看该作者

開file時有問題

对了,可能的原因是这样的:
你少写了一个头文件,

  1. #include <string>;
复制代码


看到有位高手这样说:
ostream& operator << (ostream& o, const string& s);
这个函数的处理不同,
Borland的STLPort在<iostream>;中实现,而VC的STL在<string>;中实现。
不知道标准中有没有关于这方面的规定,如果没有VC也没有错。
再说,其实放在<string>;中更合理,因为应该让自定义类型去感知ostream,而没有必要让ostream在<iostream>;中去感知其他类型。

论坛徽章:
0
7 [报告]
发表于 2003-05-16 18:31 |只看该作者

開file時有問題

feeling 说的有道理
因为VC的STL有许多与标准STL不兼容的
所以使用时要注意一下

这个问题就是feeling 后说的没有重定义<<操作原因

论坛徽章:
0
8 [报告]
发表于 2003-05-17 18:36 |只看该作者

開file時有問題

明白了...謝

论坛徽章:
0
9 [报告]
发表于 2003-05-17 19:26 |只看该作者

開file時有問題

redhat9下g++编译正常

论坛徽章:
0
10 [报告]
发表于 2003-05-17 20:02 |只看该作者

開file時有問題

redhat使用的是sgi的SYL
就是最标准的STL库

所以正常
MS使用自己的STL库,并且可能有它自己的理由吧
故意与标准不兼容
所以编译时会出问题

其它的STL中MS也是与标准有区别的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP