有关文件操作的错误,
我写了如下程序:
#include <iostream>
#include <fstream>
using namespace std;
#define Temp 7; //两个预定义作为随机数最大值
#define Humi 6;
void main()
{
int month[8]={0, 31, 28, 31, 30, 31, 30, 31}; //1-7
int zz=0, weekday=0;
int i = 0, k = 0;
weekday = 1;
fstream logIn;
logIn.open( "log2007.txt ", ios::in); //打开一个文件
for(i=1; i <8; i++)
{
logIn < < "2007-0 " < < i < < "\n ";
for(k=0; k <month[i]; k++)
{
srand(zz);
logIn < < k + 1 < < "\t " < < 24.0 + rand() %Temp < < "\t "; // 向文件写数据
logIn < < 58 + rand() %Humi < < "\t ";
logIn < < 24.0 + rand() %Temp < < "\t ";
logIn < < 58 + rand() %Humi < < "\n ";
}
logIn < < endl;
cout < < "2007-0 " < < i < < "\n ";
}
logIn.close(); //关闭文件
}
却有如下错误:
E:\cpp\rands.cpp(20) : error C2143: syntax error : missing '; ' before ' < < '
E:\cpp\rands.cpp(21) : error C2143: syntax error : missing '; ' before ' < < '
E:\cpp\rands.cpp(22) : error C2143: syntax error : missing '; ' before ' < < '
E:\cpp\rands.cpp(23) : error C2143: syntax error : missing '; ' before ' < < '
执行 cl.exe 时出错.
看了半天也没有看到有什么缺的,本来错误还多,写成这样后就剩下四个了。怎么也找不到,现在只好用C, fopen(), fprintf().
请教高手,这到底是怎么回事呢?
[解决办法]
logIn.open( "log2007.txt ", ios::in); //打开一个文件
=======
这里你用的是ios::in表示输入啦,而你的意图却是想输出
其实你应该用
ofstream logOut( "log2007.txt ");
ofstream //输出流
ifstream //输入流
不要用fstream