| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1324 人关注过本帖
标题:为什么同样的代码,在vs中和在qt creator中结果却是不一样的?
只看楼主 加入收藏
songhuirong1
Rank: 2
等 级:论坛游民
帖 子:116
专家分:38
注 册:2010-6-15
结帖率:94.12%
收藏
已结贴  问题点数:20 回复次数:3 
为什么同样的代码,在vs中和在qt creator中结果却是不一样的?
题目如下:
图片附件: 游客没有浏览图片的权限,请 登录注册

我在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是如何处理的,真是不懂了,请各位高手帮忙解答下。谢谢了!!!
搜索更多相关主题的帖子: 测试 include 
2011-12-11 10:43
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:14 
因为你对变量和函数的命名很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;
}

2011-12-12 09:24
songhuirong1
Rank: 2
等 级:论坛游民
帖 子:116
专家分:38
注 册:2010-6-15
收藏
得分:0 
回复 2楼 rjsp
我想知道为什么我的代码是一样的,但是在vs和qt creator中执行出来的结果是不一样的,不是想用其它算法来实现。你把算法都改了,那就偏离我的意思了。
2011-12-12 21:05
songhuirong1
Rank: 2
等 级:论坛游民
帖 子:116
专家分:38
注 册:2010-6-15
收藏
得分:0 
回复 2楼 rjsp
我的代码java化你从哪里看出来的?我是按照我们公司的code style来写代码的,我们写C代码的时候就是这样命名的。
2011-12-12 21:10
快速回复:为什么同样的代码,在vs中和在qt creator中结果却是不一样的?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029947 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved