![]() |
#2
rjsp2011-12-12 09:24
|
只有本站会员才能查看附件,请 登录
我在vs中,写了代码进行测试,结果是正确的,代码如下:

// t.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <ios>
#include <iostream>
#include <fstream>
#include <string>
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::ios;
using std::fstream;
void writeToFile(const string &filename)
{
fstream outFile(filename.c_str(),ios::in | ios::out | ios::app);
if(outFile.fail())
{
cerr << "Unable to open " << filename << endl;
return;
}
char ch;
int cnt = 0;
while(outFile.get(ch))
{
cout.put(ch);
++cnt;
if(ch == '\n')
{
ios::pos_type mark = outFile.tellg();
outFile.seekp(ios::end);
outFile << cnt << ' ';
outFile.seekg(mark);
}
}
outFile.clear();
cout << "[" << cnt << "]" << endl;
outFile << cnt;
cout << "Write data sucessed!" << endl;
outFile.close();
}
int _tmain(int argc, _TCHAR* argv[])
{
string filename = "G:/test.txt";
writeToFile(filename);
return 0;
}
在qt creator中写了代码进行测试,但是结果始终不正确。qt creator中的代码如下://
#include "stdafx.h"
#include <ios>
#include <iostream>
#include <fstream>
#include <string>
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::ios;
using std::fstream;
void writeToFile(const string &filename)
{
fstream outFile(filename.c_str(),ios::in | ios::out | ios::app);
if(outFile.fail())
{
cerr << "Unable to open " << filename << endl;
return;
}
char ch;
int cnt = 0;
while(outFile.get(ch))
{
cout.put(ch);
++cnt;
if(ch == '\n')
{
ios::pos_type mark = outFile.tellg();
outFile.seekp(ios::end);
outFile << cnt << ' ';
outFile.seekg(mark);
}
}
outFile.clear();
cout << "[" << cnt << "]" << endl;
outFile << cnt;
cout << "Write data sucessed!" << endl;
outFile.close();
}
int _tmain(int argc, _TCHAR* argv[])
{
string filename = "G:/test.txt";
writeToFile(filename);
return 0;
}

#include <QtCore/QCoreApplication>
#include <ios>
#include <iostream>
#include <fstream>
#include <string>
using std::ios;
using std::fstream;
using std::cout;
using std::cerr;
using std::endl;
using std::string;
void writeToFile(const string &filename)
{
fstream outFile(filename.c_str(),ios::in | ios::out | ios::app);
if(outFile.fail())
{
cerr << "Unable to open " << filename << endl;
return;
}
char ch;
int cnt = 0;
while(outFile.get(ch))
{
cout.put(ch);
++cnt;
if(ch == '\n')
{
ios::pos_type mark = outFile.tellg();
outFile.seekp(ios::end);
outFile << cnt << ' ';
outFile.seekg(mark);
}
}
outFile.clear();
cout << "[" << cnt << "]" << endl;
outFile << cnt;
cout << "Write data sucessed!" << endl;
outFile.close();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
string filename = "G:/test.txt";
writeToFile(filename);
return 0;
}
写入文件后,文件中的内容是这样的:#include <ios>
#include <iostream>
#include <fstream>
#include <string>
using std::ios;
using std::fstream;
using std::cout;
using std::cerr;
using std::endl;
using std::string;
void writeToFile(const string &filename)
{
fstream outFile(filename.c_str(),ios::in | ios::out | ios::app);
if(outFile.fail())
{
cerr << "Unable to open " << filename << endl;
return;
}
char ch;
int cnt = 0;
while(outFile.get(ch))
{
cout.put(ch);
++cnt;
if(ch == '\n')
{
ios::pos_type mark = outFile.tellg();
outFile.seekp(ios::end);
outFile << cnt << ' ';
outFile.seekg(mark);
}
}
outFile.clear();
cout << "[" << cnt << "]" << endl;
outFile << cnt;
cout << "Write data sucessed!" << endl;
outFile.close();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
string filename = "G:/test.txt";
writeToFile(filename);
return 0;
}
abcd
efg
hi
j
5 6 7 8 16
和vs执行出来的结果是不一样的。vs的结果是正确的,qt creator是不正确的,为什么一样的代码执行出来的结果是不一样的,qt creator是如何处理的,真是不懂了,请各位高手帮忙解答下。谢谢了!!!