M
M
mlwrm2016-03-17 13:50:15
C++ / C#
mlwrm, 2016-03-17 13:50:15

How to treat char warning overflow when reading from a file?

Hello, when building the code (under Ubuntu) it gives a warning, it does not seem to affect the operation of the program, but I would like to eliminate it.
There is a code

#include<iostream>
#include<fstream>
#include<iomanip>
#include <cstring>

using namespace std;
//--------------------------------------------------------------------------------
struct SBooks
{
  char chAuthorname[100],chAuthorpatronimic[100], chBook[100];
  int nYear, nPages, nPrise;
};
//--------------------------------------------------------------------------------
struct list
{
  SBooks sBooks;
  list* pNext;
};

when reading from a file
ifstream fin(g_chNameOfFile);
    if (!fin)
    {
      cout << "File doesn't exist." << endl;
      Readbooks();
      return;
    }
    list* pRun = g_pBegin;
    while (!fin.eof())
    {
      fin >> pRun->sBooks.chAuthorname >> pRun->sBooks.chAuthorpatronimic;
      fin.getline(pRun->sBooks.chBook, 99, '»');
      strcat(pRun->sBooks.chBook, "»");
      fin>> pRun->sBooks.nYear >> pRun->sBooks.nPages >> pRun->sBooks.nPrise;
      if (fin.eof())
        pRun->pNext = NULL;
      else
        pRun = pRun->pNext = new list;
    }

Geany Issues
struct.cxx: In function 'void Readbooks()':
struct.cxx:79:51: warning: overflow in implicit constant conversion [-Woverflow]
fin.getline(pRun->sBooks.chBook, 99, '"');

Please tell me the solution

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2016-03-17
@Mercury13

fin.getline(pRun->sBooks.chBook, 99, static_cast<char>('»'));

Everything you have is correct. And in C char is (usually) signed for some reason.

M
MiiNiPaa, 2016-03-17
@MiiNiPaa

It's possible that '"' is not really a char, but an int. Google multicharacter literals. It depends on the encoding of the file and the IDE. Try this and post the result:

#include <iostream>
#include <type_traits>

int main()
{
    std::cout << typeid('»').name() << '\n';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question