user53027
user53027

Reputation:

C++, windows->linux porting, mapfile problem

I'm porting a small C++ console application from windows to linux, GCC 4.3.2. When compiling I get strange error that I'm unable to solve.

Labels.cpp: In function ‘void DumpSymbols()’:
Labels.cpp:68: error: invalid conversion from ‘int’ to ‘std::_Ios_Openmode’
Labels.cpp:68: error:   initializing argument 2 of ‘std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]’

Labels.cpp:

#include <string>
#include <fstream>
#include <iostream>
#include "stdafx.h"
using namespace std;

#include "Opcodes.h"
#include "Labels.h"


Label LABELS[1024];
int labelcounter = 0;
int OffsetCounter = 0;

void AddLabel(string name, int line)
{
        LABELS[labelcounter].name = name;
        LABELS[labelcounter].line = line;
        LABELS[labelcounter].offset = OffsetCounter;
        printf("Adding label: %s[0x%X]\n", name.c_str(), OffsetCounter);
        labelcounter++;
}

bool IsLabel(string name)
{
        for(int i=0;i<labelcounter;i++)
        {
                if (LABELS[i].name.compare(name) == 0)
                {
                        return true;
                }
        }
        return false;
}

int GetOffset(string lbl)
{
        for(int i=0;i<labelcounter;i++)
        {
                if (LABELS[i].name.compare(lbl) == 0)
                {
                        printf("Refers to label '%s':0x%X\n", lbl.c_str(), LABELS[i].offset);
                        return LABELS[i].offset;
                }
        }
        return -1;
}

void DumpSymbols()
{
        ofstream mapfile("symbols.map", ios::out|ios::beg);  //this line causes error

        //mapfile.write(
        char numbuf1[32];
        itoa(labelcounter, numbuf1, 10);
        mapfile.write((string(numbuf1) + "\n").c_str(), strlen(numbuf1)+1);

        for(int i=0;i<labelcounter;i++)
        {
                string dump;
                char numbuf[32];
                itoa(LABELS[i].offset, numbuf, 10);
                dump = string(LABELS[i].name) + "\t" + string(numbuf) + "\n";
                mapfile.write(dump.c_str(), strlen(dump.c_str()));
        }
}

stdafx.h:

#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cstdlib>

Thanks.

Upvotes: 0

Views: 1015

Answers (2)

Laserallan
Laserallan

Reputation: 11312

Is ios::beg really a valid value for the mode parameter of the ofstream constructor?

According to http://www.cplusplus.com/reference/iostream/ofstream/ofstream.html it's not.

I guess what happened is that you accidentally borrowed it from a call to ofstream::seekg (where it is a valid parameter) to enforce that the writing will start from the beginning of the file rather than the end of it.

If you are trying to force the file to be completely replaced if it already existed, try using ios::trunc instead of ios::beg.

Upvotes: 1

Robert Eccardt
Robert Eccardt

Reputation: 21

Just remove "|ios::beg":

ofstream mapfile("symbols.map", ios::out);

It's type is ios_base::seekdir, which is not an opening mode; it's for seeking to a position. You'll automatically be at the beginning anyway.

Upvotes: 2

Related Questions