注册 登录
编程论坛 C++教室

为什么同样的代码,在vs中和在qt creator中结果却是不一样的?

songhuirong1 发布于 2011-12-11 10:43, 1324 次点击
题目如下:
只有本站会员才能查看附件,请 登录

我在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 <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;
}
写入文件后,文件中的内容是这样的:
abcd
efg
hi
j
5 6 7 8 16
和vs执行出来的结果是不一样的。vs的结果是正确的,qt creator是不正确的,为什么一样的代码执行出来的结果是不一样的,qt creator是如何处理的,真是不懂了,请各位高手帮忙解答下。谢谢了!!!
3 回复
#2
rjsp2011-12-12 09:24
因为你对变量和函数的命名很java化,所以我懒得多说些什么

程序代码:
#include <iostream>
#include <fstream>
#include <sstream>
#include <iterator>
using namespace std;

void WriteToFile( const char* filename )
{
    fstream file( filename );
    if( !file )
    {
        cerr << "Unable to open " << filename << '\n';
        return;
    }

    ostringstream os;
    streampos pos = 0;
    for( istreambuf_iterator<char> itor(file); itor!=istreambuf_iterator<char>(); ++itor )
    {
        pos += 1;

        if( *itor == '\n' )
            os << pos << ' ';
    }

    file << os.str() << os.str().size()+pos;
}

int main()
{
    WriteToFile( "d:/test.txt" );

    return 0;
}

#3
songhuirong12011-12-12 21:05
回复 2楼 rjsp
我想知道为什么我的代码是一样的,但是在vs和qt creator中执行出来的结果是不一样的,不是想用其它算法来实现。你把算法都改了,那就偏离我的意思了。
#4
songhuirong12011-12-12 21:10
回复 2楼 rjsp
我的代码java化你从哪里看出来的?我是按照我们公司的code style来写代码的,我们写C代码的时候就是这样命名的。
1