c++输入输出流的有关问题
c++输入输出流的问题
最近在看C++编程思想(2000年1月第一版),看到书上有个程序挺有意思,于是照着写下来,但是编译时出现了错误:
源程序如下:
#include <iostream>
#include <fstream>
#define D(a) T << #a <<endl; a
ofstream T("format.out");
using namespace std;
int main()
{
D(int i = 47;)
D(float f = 2300114.414159;)
char* s = "Is there any more?";
D(T.setf(ios::unitbuf);)
D(T.setf(ios::stdio);)
D(T.setf(ios::showbase);)
D(T.setf(ios::uppercase);)
D(T.setf(ios::showpos);)
D(T << i<< endl;)//Default to dec
D(T.setf(ios::hex,ios::basefield);)
D(T << i << endl;)
D(T.unsetf(ios::showbase);)
D(T.setf(ios::dec,ios::basefield);)
D(T.setf(ios::left,ios::adjustfield);)
D(T.fill('0');)
D(T << "fill char: " <<T.fill() << endl;)
D(T.width(10);)
T << i<<endl;
D(T.setf(ios::right,ios::adjustfield);)
D(T.width(10);)
T<<i<<endl;
D(T<<i<<endl;)//without width 10
D(T.unsetf(ios::showpos);)
D(T.setf(ios::showpoint);)
D(T<<"prec = "<<T.precision()<<endl;)
D(T.setf(ios::scientific,ios::floatfield);)
D(T<<endl<<f<<endl;)
D(T.setf(ios::fixed,ios::floatfield);)
D(T<<f<<endl;)
D(T.setf(0,ios::floatfield);)//Automatic
D(T<<f<<endl;)
D(T.precision(20);)
D(T<<"prec = "<<T.precision()<<endl;)
D(T<<endl<<f<<endl;)
D(T.setf(ios::scientific,ios;:floatfield);)
D(T<<endl<<f<<endl;)
D(T.setf(ios::fixd,ios::floatfield);)
D(T<<f<<endl;)
D(T.setf(0,ios::floatfield);)//automatic
D(T<<f<<endl;)
D(T.width(10);)
T<<s<<endl;
D(T.width(40);)
T<<s<<endl;
D(T.setf(ios::left,ios::adjustfield);)
D(T.width(40);)
T<<s<<endl;
D(T.unsetf(ios::showpoint);)
D(T.unsetf(ios::unitbuf);)
D(T.unsetf(ios::stdio);)
}
\main.cpp|4|error: `ofstream' does not name a type|
\main.cpp||In function `int main()':|
\main.cpp|10|error: `T' was not declared in this scope|
\main.cpp|15|error: `stdio' is not a member of `std::ios'|
\main.cpp|47|error: expected primary-expression before ';' token|
\main.cpp|47|error: expected primary-expression before ':' token|
\main.cpp|47|error: expected `;' before ':' token|
\main.cpp|49|error: `fixd' is not a member of `std::ios'|
\main.cpp|64|error: `stdio' is not a member of `std::ios'|
||=== Build finished: 8 errors, 0 warnings ===|
PS:这本书是不是太老了?一开始我包含头文件fstream.h时给出了一个警告,后来上网查询发现应该是#include <fstream>
,前者是较旧的版本。
------解决方案--------------------
很有趣的代码呀,虽然我看不懂:),但是帮你试了一下
最近在看C++编程思想(2000年1月第一版),看到书上有个程序挺有意思,于是照着写下来,但是编译时出现了错误:
源程序如下:
#include <iostream>
#include <fstream>
#define D(a) T << #a <<endl; a
ofstream T("format.out");
using namespace std;
int main()
{
D(int i = 47;)
D(float f = 2300114.414159;)
char* s = "Is there any more?";
D(T.setf(ios::unitbuf);)
D(T.setf(ios::stdio);)
D(T.setf(ios::showbase);)
D(T.setf(ios::uppercase);)
D(T.setf(ios::showpos);)
D(T << i<< endl;)//Default to dec
D(T.setf(ios::hex,ios::basefield);)
D(T << i << endl;)
D(T.unsetf(ios::showbase);)
D(T.setf(ios::dec,ios::basefield);)
D(T.setf(ios::left,ios::adjustfield);)
D(T.fill('0');)
D(T << "fill char: " <<T.fill() << endl;)
D(T.width(10);)
T << i<<endl;
D(T.setf(ios::right,ios::adjustfield);)
D(T.width(10);)
T<<i<<endl;
D(T<<i<<endl;)//without width 10
D(T.unsetf(ios::showpos);)
D(T.setf(ios::showpoint);)
D(T<<"prec = "<<T.precision()<<endl;)
D(T.setf(ios::scientific,ios::floatfield);)
D(T<<endl<<f<<endl;)
D(T.setf(ios::fixed,ios::floatfield);)
D(T<<f<<endl;)
D(T.setf(0,ios::floatfield);)//Automatic
D(T<<f<<endl;)
D(T.precision(20);)
D(T<<"prec = "<<T.precision()<<endl;)
D(T<<endl<<f<<endl;)
D(T.setf(ios::scientific,ios;:floatfield);)
D(T<<endl<<f<<endl;)
D(T.setf(ios::fixd,ios::floatfield);)
D(T<<f<<endl;)
D(T.setf(0,ios::floatfield);)//automatic
D(T<<f<<endl;)
D(T.width(10);)
T<<s<<endl;
D(T.width(40);)
T<<s<<endl;
D(T.setf(ios::left,ios::adjustfield);)
D(T.width(40);)
T<<s<<endl;
D(T.unsetf(ios::showpoint);)
D(T.unsetf(ios::unitbuf);)
D(T.unsetf(ios::stdio);)
}
\main.cpp|4|error: `ofstream' does not name a type|
\main.cpp||In function `int main()':|
\main.cpp|10|error: `T' was not declared in this scope|
\main.cpp|15|error: `stdio' is not a member of `std::ios'|
\main.cpp|47|error: expected primary-expression before ';' token|
\main.cpp|47|error: expected primary-expression before ':' token|
\main.cpp|47|error: expected `;' before ':' token|
\main.cpp|49|error: `fixd' is not a member of `std::ios'|
\main.cpp|64|error: `stdio' is not a member of `std::ios'|
||=== Build finished: 8 errors, 0 warnings ===|
PS:这本书是不是太老了?一开始我包含头文件fstream.h时给出了一个警告,后来上网查询发现应该是#include <fstream>
,前者是较旧的版本。
------解决方案--------------------
很有趣的代码呀,虽然我看不懂:),但是帮你试了一下
- C/C++ code
#define D(a) T << #a << endl; a ofstream T("format.out"); using namespace std;//运行出错
------解决方案--------------------
这段话的意思是说程序里面有至少一处使用了已经被摒弃的头文件包含方式。例如:<iostream.h>应该改成<iostream>的方式--在C++当中。
把出错的地方屏蔽掉。
- C/C++ code
int main() { D(int i = 47;) D(float f = 2300114.414159;) char* s = "Is there any more?"; D(T.setf(ios::unitbuf);) //D(T.setf(ios::stdio);) D(T.setf(ios::showbase);) D(T.setf(ios::uppercase);) D(T.setf(ios::showpos);) D(T << i<< endl;)//Default to dec D(T.setf(ios::hex,ios::basefield);) D(T << i << endl;) D(T.unsetf(ios::showbase);) D(T.setf(ios::dec,ios::basefield);) D(T.setf(ios::left,ios::adjustfield);) D(T.fill('0');) D(T << "fill char: " <<T.fill() << endl;) D(T.width(10);) T << i<<endl; D(T.setf(ios::right,ios::adjustfield);) D(T.width(10);) T<<i<<endl; D(T<<i<<endl;)//without width 10 D(T.unsetf(ios::showpos);) D(T.setf(ios::showpoint);) D(T<<"prec = "<<T.precision()<<endl;) D(T.setf(ios::scientific,ios::floatfield);) D(T<<endl<<f<<endl;) D(T.setf(ios::fixed,ios::floatfield);) D(T<<f<<endl;) D(T.setf(0,ios::floatfield);)//Automatic D(T<<f<<endl;) D(T.precision(20);) D(T<<"prec = "<<T.precision()<<endl;) D(T<<endl<<f<<endl;) //D(T.setf(ios::scientific,ios;:floatfield);) D(T<<endl<<f<<endl;) //D(T.setf(ios::fixd,ios::floatfield);) D(T<<f<<endl;) D(T.setf(0,ios::floatfield);)//automatic D(T<<f<<endl;) D(T.width(10);) T<<s<<endl; D(T.width(40);) T<<s<<endl; D(T.setf(ios::left,ios::adjustfield);) D(T.width(40);) T<<s<<endl; D(T.unsetf(ios::showpoint);) D(T.unsetf(ios::unitbuf);) //D(T.unsetf(ios::stdio);) }