Answer the question
In order to leave comments, you need to log in
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;
};
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;
}
struct.cxx: In function 'void Readbooks()':
struct.cxx:79:51: warning: overflow in implicit constant conversion [-Woverflow]
fin.getline(pRun->sBooks.chBook, 99, '"');
Answer the question
In order to leave comments, you need to log in
fin.getline(pRun->sBooks.chBook, 99, static_cast<char>('»'));
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 questionAsk a Question
731 491 924 answers to any question