Calling system() to run an external .exe and checking error code upon errors:
#include <errno.h>
#include <stdlib.h>
function()
{
errno_t err;
if( system(tailCmd) == -1) //if there is an error get errno
{
//Error calling tail.exe
_get_errno( &err );
}
}
First two compile errors:
error C2065: 'err' : undeclared identifier
error C2065: 'errno_t' : undeclared identifier
Not sure why as I am including the required and optional header files?
Any help is appreciated. Thank You.
A typical usage is like:
if (somecall() == -1) {
int errsv = errno;
printf("somecall() failed\n");
if (errsv == ...) { ... }
}
which is taken from here.
Just use 'errno' without any declaration. It is a macro that expands to an int value.
In the world of Standard C, the type 'errno_t' is defined by TR24731-1 (see Do you use the TR 24731 'safe' functions? for more information) and you have to 'activate it' by defining '__STDC_WANT_LIB_EXT1__'.
However, you appear to be working on Windows (judging from 'tail.exe', and also the non-standard '_get_errno()'). The rules there may depend on the C compiler you are using.
You should be able to chase down the information from this MSDN article on 'Security Enhancements in the CRT'. My impression was that it should be defined unless you actively suppress the feature, so check out whether you are actively suppressing it in your compilations.
Be aware that the MSVC definition of functions such as vsnprintf_s() do not match the TR24731-1 definitions:
MSDN:
int vsnprintf_s(
char *buffer,
size_t sizeOfBuffer,
size_t count,
const char *format,
va_list argptr
);
TR 24731-1:
int vsnprintf_s(
char * restrict s,
rsize_t n,
const char * restrict format,
va_list arg
);
The difference is not just a question of type aliases or qualifiers (rsize_t, restrict) - there are two sizes in the MS version and one in the Standard version. So much for standardization!
Related
It is very common for any medium-to-large project to replace printf with a custom log function. Here is a minimal C++ example and its usage:
#include <stdio.h>
#include <stdarg.h>
#include <string>
void log_printf(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap); // real code obviously does something fancier
va_end(ap);
}
int main() {
std::string x = "Hello";
// correct code
printf("String is %s\n", x.c_str());
log_printf("String is %s\n", x.c_str());
// incorrect code
printf("String is %s\n", x); // bad line 1
log_printf("String is %s\n", x); // bad line 2
}
The simple logger receives a variable amount of arguments and calls vprintf to output them to standard output. The lines under 'correct code' demonstrate correct usage of this logger. My question involves the 'bad' lines, where a string object is incorrectly passed instead of a pointer to the character buffer.
Under GCC 4.6 (tested under Linux) neither of the bad lines can compile, which is a good thing because I want to catch such incorrect usage. The error is:
error: cannot pass objects of non-trivially-copyable type βstd::string {aka struct std::basic_string<char>}β through β...β
However in GCC 5.1 it has apparently become possible to pass non-trivially-copyable objects, and the compilation succeeds. If I use -Wall then only 'bad line 1' raises a warning about an unexpected argument type, but 'bad line 2' with the log_printf compiles without issue in any case. Needless to say both lines produce garbage output.
I can catch 'bad line 1' with -Wall -Werror, but what about 'bad line 2'? How can I cause it to also generate a compilation error?
For your own functions you need to use a common function attribute call format:
void log_printf(const char* fmt, ...) __attribute__((format (printf, 1, 2)));
void log_printf(const char* fmt, ...) {
...
}
Note that the attribute must be set on a function declaration, not the definition.
The first argument to the format attribute is the style, in this case printf (scanf is also possible, for functions that works like scanf), the second argument is the format string, and the third argument is where the ellipsis ... is. It will help GCC check the format strings like for the standard printf function.
This is a GCC extension, though some other compilers have adopted it to become GCC compatible, most notably the Intel C compiler ICC and Clang (the standard compiler used on OSX and some BSD variants). The Visual Studio compiler does not have this extension, and I know of no similar thing for Visual C++.
So I've tried to figure out what exactly the professor was writing on the board and how it answers the lab assignment we are to do.
This is the lab assignment:
Create a Hash Table and Hash map that holds all of the WORDS in the (given below) Declaration of Independence.
Handle collisions using the chain method. (Note we will not be modifying this table nor doing deletions!)
Programmatically answer the following questions:
What is the size of your hash table?
What is the longest collision (ie. Chain)
What is the most frequently used word and how did you determine it?
Create a (second) Hash Table that holds all of the LETTERS in the Declaration of Independence.
What is the size of your hash table
What letter has the longest collision?
And this is the pseudo-code with some modifications that I did to fix some errors:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <list>
using namespace std;
class Translate
{
string word;
public:
int trans(string word);
w = word.charAT(0); //gives a letter
return #num;
};
class HashTable
{
int size();
int collision();
int length();
char fword();
public:
Translate t;
list<string> hashTable[29];
bool insert(string word)
{
hashTable[t.trans(word)].push_back(word);
return true;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
HashTable h;
open file f("hash.txt");
//h.insert(word)
while (!f.eof())
{
h.insert(f.word());
}
cout << h.size;
cout << h.collision.length;
cout << h.fword;
return 0;
}
The errors that I have are:
Error 15 error C1903: unable to recover from previous error(s); stopping compilation
Error 5 error C2014: preprocessor command must start as first nonwhite space
Error 4 error C2059: syntax error : 'return'
Error 13 error C2065: 'f' : undeclared identifier
Error 10 error C2065: 'file' : undeclared identifier
Error 8 error C2065: 'open' : undeclared identifier
Error 6 error C2143: syntax error : missing ';' before '}'
Error 1 error C2143: syntax error : missing ';' before '='
Error 11 error C2146: syntax error : missing ';' before identifier 'f'
Error 9 error C2146: syntax error : missing ';' before identifier 'file'
Error 14 error C2228: left of '.eof' must have class/struct/union
Error 3 error C2238: unexpected token(s) preceding ';'
Error 7 error C2238: unexpected token(s) preceding ';'
Error 12 error C3861: 'f': identifier not found
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 19 IntelliSense: '#' not expected here
Error 17 IntelliSense: class "std::basic_string, std::allocator>" has no member "charAT"
Error 21 IntelliSense: expected a ';'
Error 18 IntelliSense: expected a declaration
Error 22 IntelliSense: identifier "f" is undefined
Error 20 IntelliSense: identifier "open" is undefined
Error 16 IntelliSense: this declaration has no storage class or type specifier
I've never used .c_str and I'm still pretty new to C++ so my knowledge is limited. I can tell that there are places that need an identifier but I think there is a better way to create a "open file". My previous knowledge is C#, HTML, and some Python in which C++ is giving me some difficulty in learning and understanding. Any help and/or insight would be greatly appreciated!
Code is too mangled to understand. However, I'm trying my best to help with the little knowledge of mine on C++ and hash.
Proposed Code Modification
Program entry point : instead of int _tmain(int, _TCHAR*), use int main().This should guarantee you the ability to test things out should you migrate to non-windows compiler.
Source : Unicode _tmain vs main
I would like to help with the remainder, however, the code posted is way too unintelligible. Would be kind if the algorithm is posted for reference.
There are a few things you should change:
Assuming trans() is supposed to be a function definition, not a declaration, and the lines following it are supposed to be the body:
Unless you specifically want to copy the passed string, you should use const string& instead of string.
It should have braces.
w is a char.
std::string defines operator[], so it can be indexed like an array.
I'm not sure what #num is (I assume it's from Python, but I'm not familiar with that), so I'm not sure how you intend to calculate the return value.
[I will thus assume that you want to return w, but as an int instead of a char. If this is the case, it would be simpler to just return word[0];.]
There are a few issues with HashTable's members.
Member functions size(), collision(), length(), and fword() are private. This doesn't appear to be intentional.
Member variables t and hashTable are public, when you likely wanted them to be private. Again, this doesn't appear to be intentional.
The functions aren't actually defined anywhere, unless you didn't show their definitions. This will cause a linking error when you call them.
While this doesn't need to be changed, there's no reason for HashTable::insert() to actually return a value, if it's hard-coded to always return true. Also, as mentioned in 1.1 above, the parameter should probably be const string&.
_tmain() and _TCHAR are a Microsoft extensions, which is available on Visual Studio and some (but not all) compilers aiming for compatibility with it (such as C++Builder). If you want your code to be platform-independent, you likely want main(). [Note that this doesn't need to be changed. If you're only compiling with Visual Studio, you can leave it as is. If you want platform independence, you can easily define _tmain and _TCHAR yourself.]
Opening a file:
Neither open nor file are keywords in C++, nor are they types (although FILE is a C type, it doesn't appear to be what you want). You appear to want std::ifstream.
You shouldn't use !f.eof() as a condition in a while loop, because eofbit won't be set until after reading fails.
fstream has no member function word(). However, the extraction operator, operator>>() will read a single word at a time, if given a parameter that can accept one.
HashTable::size(), HashTable::collision(), HashTable::length(), and HashTable::fword() are functions. To call them, you use operator(). If you just use a function's name directly, you don't call it, but instead refer to it (this can be used to create a function pointer or function reference).
int has no member function length(). Therefore, you cannot call h.collision().length(). In C++, if you chain function calls like that, each function in the chain is treated as if it were a member function of the directly preceding type, not the leftmost type; this means that for every function after the first, the return type of the preceding function is used. (In this case, h.collision() returns an int, so .length() attempts to call member function int::length(). int isn't a class type, and thus doesn't have any member functions.)
So, considering these, your code can be modified as follows:
// Assuming your stdafx.h contains "#include <string>" and "#include <tchar.h>".
// If it doesn't, either put them there, or #include them here.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <list>
// #4: Defining _tmain and _TCHAR
#ifndef _tmain
#define _tmain main
typedef char _TCHAR;
#endif
using namespace std;
class Translate
{
string word;
public:
// #1: Fixing trans().
int trans(const string& word)
{
char w = word[0]; // First letter of word.
return w; // Will be promoted to int.
}
};
class HashTable
{
// #2: Making member functions public, and member variables private.
Translate t;
list<string> hashTable[29];
public:
int size();
int collision();
int length();
char fword();
// #3: Making word a const reference. Changing return type to void.
void insert(const string& word)
{
hashTable[t.trans(word)].push_back(word);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
HashTable h;
// #5.1: Opening the file.
ifstream f("hash.txt");
//h.insert(word)
// #5.2 & 5.3: Reading a word.
std::string word;
while (f >> word)
{
h.insert(word);
}
// #6: Calling functions.
cout << h.size();
cout << h.collision(); // #7: Assuming you wanted to output both h.collision() and
cout << h.length(); // h.length(), I put them on separate lines.
// If you actually DID want h.collision().length(), then
// h.collision() should return a type (or reference to a type)
// with member function length(), or be an instance
// (or reference to an instance) of a class with member function
// length() (instead of being a function).
cout << h.fword();
return 0;
}
You still need to provide bodies for HashTable's member functions, apart from insert(), as well as make any other modifications you desire. You might also want to remove member word from Translate, if it doesn't actually need to store a string.
I have copied some code from another project which I downloaded (and which compiled fine) and get the compiler error message when compiling the same code ( a file called player.cpp) in my own project:
Error 1 error C2665: 'MATExceptions::MATExceptions' : none of the 3 overloads could convert all the argument types c:\users\daniel\documents\visual studio 2012\projects\mytest1\mytest1\player.cpp 137 1 Test1
The error occurs on this line in player.cpp:
EXCEP(DirectSoundErr::GetErrDesc(hres), _T("Player::CreateDS DirectSoundCreate"));
Here is the definition of EXCEP and GetErrDesc:
#define EXCEP(/*const wchar_t * */ desc, /*const wchar_t * */ from) throw( MATExceptions(__LINE__, _T(__FILE__), 0, from, desc) );
CComBSTR DirectSoundErr::GetErrDesc(HRESULT hres)
{
switch(hres)
{
case DSERR_ALLOCATED :
return _T("The request failed because resources, such as a
priority level, were already in use by another caller.");
...
default : return _T("Unknown error");
}
}
I don't know what is different (as I have not changed the source file player.cpp). Could it be due to different compiler settings in my project compared to the original (how would I check this)?
I changed the EXCEP definition to the following:
#define EXCEP(desc, from) throw(MATExceptions(__LINE__, (wchar_t *)(__FILE__), 0, (wchar_t *)from, (wchar_t *)desc));
...and changed the call from:
EXCEP(DirectSoundErr::GetErrDesc(hres), _T("Player::CreateDS DirectSoundCreate"));
to:
EXCEP(DirectSoundErr::GetErrDesc(hres), "Player::CreateDS DirectSoundCreate");
Is that acceptable?
The original "new" can be killed by defining these in project (since Visual Studio 2015 I guess):
__PLACEMENT_NEW_INLINE
__PLACEMENT_VEC_NEW_INLINE
But once they are gone, they are gone. Now you need to make sure to include the project-specific header file which redefines them.
I'm working on a C++ project where I must connect to redis database. I'm trying to get the credis code to work but when I compile it I get these sets of errors
1>c:\c++redis\credis.c(728): warning C4013: 'fcntl' undefined; assuming extern returning int
1>c:\c++redis\credis.c(728): error C2065: 'F_GETFL' : undeclared identifier
1>c:\c++redis\credis.c(729): error C2065: 'F_SETFL' : undeclared identifier
1>c:\c++redis\credis.c(729): error C2065: 'O_NONBLOCK' : undeclared identifier
1>c:\c++redis\credis.c(734): error C2065: 'EINPROGRESS' : undeclared identifier
1>c:\c++redis\credis.c(740): warning C4133: 'function' : incompatible types - from 'int *' to 'char *'
The error is in the credis.c file from line 728 to 746
/* connect with user specified timeout */
flags = fcntl(fd, F_GETFL);
if ((rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK)) < 0) {
DEBUG("Setting socket non-blocking failed with: %d\n", rc);
}
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != 0) {
if (errno != EINPROGRESS)
goto error;
if (cr_selectwritable(fd, timeout) > 0) {
int err;
unsigned int len = sizeof(err);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len) == -1 || err)
goto error;
}
else /* timeout or select error */
goto error;
}
/* else connect completed immediately */
Where can I find these missing typenames?
I'm using visual studio 2010 to compile this and the program must run on window.
I have tried to batch the code with this suggested answer, but that didn't help.
You have at least one header file missing:
#include <fcntl.h>
that should fix some of your issues. Generally, a good place to look for the header file names is in the help text for the function itself. In this case the header file has the same name as the function (fcntl) but most are not that easy.
EINPROGRESSis defined in:
#include <errno.h>
for future reference, the E prefix usually means it is an error macro, so errno.h is the first place to look.
'function' : incompatible types - from 'int *' to 'char *' probably means you have a prototype mis-match. Your prototype does not match the function itself. Update the prototype.
Edit:
Although that will fix some of your issues, it appears that this is UNIX code (see comments). F_GETFL and F_SETFL, for example, appears not to be supported on Windows. O_NONBLOCK is in unistd.h on UNIX.
You will need to rewrite the parts of the code requiring this functionality or, better yet, get the Windows version from your supplier.
From your updated post, these are done using sockets. Sockets are fairly portable, but there are some issues. For non-blocking sockets use ioctlsocket() on Windows.
Example:
int iRetn = ioctlsocket(s, FIONBIO, 1);
where s is the socket, the third parameter is 0 for blocking, non-zero for non-blocking.
You also need to #include <winsock.h> and call WSAStartup() before using any socket routines, and call WSACleanup() at end.
(To be honest that's all I can think of right now, I didn't realise I would be answering issues on sockets).
The library works well on linux,but not windows.
On Windows,I use https://code.google.com/p/libredic/
Here is the prototype:
void RecvProxy_ToggleSights( const CRecvProxyData* pData, void* pStruct, void* pOut );
And then the function itself:
void RecvProxy_ToggleSights( const CRecvProxyData* pData, void* pStruct, void* pOut ){
CBaseCombatWeapon *pWeapon = (CBaseCombatWeapon*)pStruct;
if( pData->m_Value.m_Int )
pWeapon->EnableIronsights();
else
pWeapon->DisableIronsights();}
And then the error message this code, both the prototype and the definition, generates:
Error 19 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int f:\Mods\CI Testbed\src\game\shared\basecombatweapon_shared.cpp 47
How can I resolve this error?
Is the type 'CRecvProxyData' defined? Your code is otherwise correct (assuming all user defined types are defined properly), although I suggest you place opening and closing braces for the function definition on their own lines.
Also, I take issue with void*: It's a bit of a hangover from C, you should aim to eliminate it from your source code where necessary. Could you use polymorphism or templates instead?
It's likely your missing a #include, or have made an error in your include guards. If you could post the contents of the filer where 'CRecvProxyData' is defined as well as the code surrounding that which you have posted, that would be a great help. Otherwise, I can only speculate :).