主题:[讨论]vs2008 编译问题
下面这段代码在vs2008中,编译时无法打开文件,总是显示"Input file opening failed."
但是在C-Free中却可以,请问为什么?
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
void read_line(ifstream& fin,int& line_num);
void change(ifstream& fin,int& line_num,int& m);
void change(ifstream& fin,ofstream& fout,int& line_num,int& m);
const int NUMBER=72;
int main()
{
ifstream fin;
ofstream fout;
int linenum=0,m=0,all=0;
while(all!=3)
{
fin.open("F:\\file\\ss.txt");
if(fin.fail())
{
cout<<"Input file opening failed.";
exit(1);
}
fout.open("F:\\file\\sso.txt");
if(fout.fail())
{
cout<<"Input file opening failed.";
exit(1);
}
if(linenum==0)
read_line(fin,linenum);
else
{
if(m==0)
change(fin,linenum,m);
else
change(fin,fout,linenum,m);
}
fin.close();
fout.close();
all++;
}
return 0;
}
void read_line(ifstream& fin,int& line_num)
{
char next;
int n=1;
do
{
fin.get(next);
if(next=='\n')
line_num++;
}while(!fin.eof());
while(line_num>9)
{
line_num=line_num/10;
n++;
}
line_num=n;
}
void change(ifstream& fin,int& line_num,int& m)
{
char next;
int n=0,i=0;
do
{
fin.get(next);
while(next==' ')
fin.get(next);
do
{
i++;
fin.get(next);
if(next==' ')
n=i+1;
if(i==NUMBER-line_num-2)
{
fin.get(next);
if(next=='\n')
{
m=i;
}
else
m=n;
}
}while(next!='\n');
}while(!fin.eof());
if(m==0)
m=NUMBER-line_num-2;
}
void change(ifstream& fin,ofstream& fout,int& line_num,int& m)
{
char next;
int n=0,i=0;
cout.setf(ios::right);
fout.setf(ios::right);
do
{
fin.get(next);
while(next==' ')
fin.get(next);
if(!fin.eof())
{
n++;
cout<<setw(line_num)<<n<<":"<<" ";
fout<<setw(line_num)<<n<<":"<<" ";
}
do
{
cout<<next;
fout<<next;
i++;
if(i==m)
{
cout<<'\n'<<setw(line_num+2)<<" ";
fout<<'\n'<<setw(line_num+2)<<" ";
i=0;
}
fin.get(next);
}while(next!='\n');
cout<<next;
fout<<next;
}while(!fin.eof());
}
但是在C-Free中却可以,请问为什么?
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
void read_line(ifstream& fin,int& line_num);
void change(ifstream& fin,int& line_num,int& m);
void change(ifstream& fin,ofstream& fout,int& line_num,int& m);
const int NUMBER=72;
int main()
{
ifstream fin;
ofstream fout;
int linenum=0,m=0,all=0;
while(all!=3)
{
fin.open("F:\\file\\ss.txt");
if(fin.fail())
{
cout<<"Input file opening failed.";
exit(1);
}
fout.open("F:\\file\\sso.txt");
if(fout.fail())
{
cout<<"Input file opening failed.";
exit(1);
}
if(linenum==0)
read_line(fin,linenum);
else
{
if(m==0)
change(fin,linenum,m);
else
change(fin,fout,linenum,m);
}
fin.close();
fout.close();
all++;
}
return 0;
}
void read_line(ifstream& fin,int& line_num)
{
char next;
int n=1;
do
{
fin.get(next);
if(next=='\n')
line_num++;
}while(!fin.eof());
while(line_num>9)
{
line_num=line_num/10;
n++;
}
line_num=n;
}
void change(ifstream& fin,int& line_num,int& m)
{
char next;
int n=0,i=0;
do
{
fin.get(next);
while(next==' ')
fin.get(next);
do
{
i++;
fin.get(next);
if(next==' ')
n=i+1;
if(i==NUMBER-line_num-2)
{
fin.get(next);
if(next=='\n')
{
m=i;
}
else
m=n;
}
}while(next!='\n');
}while(!fin.eof());
if(m==0)
m=NUMBER-line_num-2;
}
void change(ifstream& fin,ofstream& fout,int& line_num,int& m)
{
char next;
int n=0,i=0;
cout.setf(ios::right);
fout.setf(ios::right);
do
{
fin.get(next);
while(next==' ')
fin.get(next);
if(!fin.eof())
{
n++;
cout<<setw(line_num)<<n<<":"<<" ";
fout<<setw(line_num)<<n<<":"<<" ";
}
do
{
cout<<next;
fout<<next;
i++;
if(i==m)
{
cout<<'\n'<<setw(line_num+2)<<" ";
fout<<'\n'<<setw(line_num+2)<<" ";
i=0;
}
fin.get(next);
}while(next!='\n');
cout<<next;
fout<<next;
}while(!fin.eof());
}