1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
#define _CRT_SECURE_NO_WARNINGS #include "stdafx.h" #include <conio.h> #include <iostream> #include <fstream> #include <string.h> using namespace std; struct planeStruct { char tname[64]; int airport; char **stList; int seats; int sectora; int ***tickets; }; int ExtractPlaneTickets(planeStruct PlaneInfo); int ExtractPlanePyti(planeStruct *PlaneInfo); void SavePlaneTickets(planeStruct *PlaneInfo, int n); int GetPossiblePyti(planeStruct PlaneInfo); int IsPlaneExists(char plane_name[], planeStruct *PlaneInfo, int n); void ShowPlaneInfo(planeStruct planeInfo); int main() { setlocale(0, "Russian"); ifstream fin("Pyti.txt"); if (!fin) { cout << "Не можу вiдкрити файл pyti.txt"; _getch(); return 0; } int n = 0; char string[128]; while (!fin.eof()) { fin.getline(string, 128); n++; } fin.close(); planeStruct *PlaneInfo = new planeStruct[n]; int planes = ExtractPlanePyti(PlaneInfo); if (planes != n) return 0; ifstream tin("tickets.txt"); if (tin) { for (int i = 0; i < n; i++) ExtractPlaneTickets(PlaneInfo[i]); } else { for (int t = 0; t < n; t++) { int c_pyti = GetPossiblePyti(PlaneInfo[t]); for (int i = 0; i < c_pyti; i++) for (int j = 0; j < PlaneInfo[t].sectora; j++) for (int x = 0; x < PlaneInfo[t].seats; x++) PlaneInfo[t].tickets[i][j][x] = 0; } SavePlaneTickets(PlaneInfo, n); } for (int i = 0; i < n; i++) { cout << i + 1 << ") " << PlaneInfo[i].tname << ": Рейс:"; for (int j = 0; j < PlaneInfo[i].airport; j++) { cout << PlaneInfo[i].stList[j]; if (j != PlaneInfo[i].airport - 1) cout << " - "; } cout << endl; } while (true) { char plane_name[64]; int id = -1; cout << endl << "Введiть назву лiтака:n"; cin.getline(plane_name, 64); while (IsPlaneExists(plane_name, PlaneInfo, n) == -1) { cout << "Лiтак з такю назвою не iснує!" << endl; cout << "Введiть назву лiтака:n"; cin.getline(plane_name, 64); } id = IsPlaneExists(plane_name, PlaneInfo, n); //ShowPlaneInfo(PlaneInfo[id]); // робота користувача while (true) { cout << endl << "Введiть номер сектора (макc. " << PlaneInfo[id].sectora << "): " << endl; int b = 0; cin >> b; while (b < 1 || b > PlaneInfo[id].sectora) { cout << "Не вiрний номер сектора!" << endl; cin >> b; } cout << "Сектор №" << b << ":n"; b--; cout << endl << "Выберите от куда и куда нужно ехать Виберiть вiд куди i куди потрiбно їхати:n"; int w = 0; int pyti = GetPossiblePyti(PlaneInfo[id]), k = 0; for (int i = 0; i < PlaneInfo[id].airport - 1; i++) for (int j = i + 1; j < PlaneInfo[id].airport; j++) { cout << k + 1 << ") " << PlaneInfo[id].stList[i] << " - " << PlaneInfo[id].stList[j] << endl; k++; } do { cout << "Введiть номер пункта: "; cin >> w; } while (w > pyti || w < 1); w--; cout << endl << "Пошук першого вiльного мiсця..." << endl; int num = -1; for (int i = 0; i < PlaneInfo[id].seats; i++) { if (PlaneInfo[id].tickets[w][b][i] == 0) { PlaneInfo[id].tickets[w][b][i] = 1; num = i; break; } } cout << "Список мiсць:" << endl << "№ статус" << endl; for (int i = 0; i < PlaneInfo[id].seats; i++) { if (PlaneInfo[id].tickets[w][b][i] == 1) cout << i + 1 << ": продано; " << endl; else cout << i + 1 << ": вiльно; " << endl; } if (num == -1) cout << endl << "n!!! Не було знайдено жодного вiльного мiсця на даному промiжку i в даному секторi !!!" << endl; else cout << "nПерше знайдене вiльне мiсце, номер: " << num + 1 << " i було вiдзначено як продане " << endl; cout << "-------------------------------------------" << endl; SavePlaneTickets(PlaneInfo, n); } } _getch(); return 1; } int IsTrainExists(char train_name[], planeStruct *TrainInfo, int n) { for (int i = 0; i < n; i++) if (strcmp(train_name, TrainInfo[i].tname) == 0) return i; return -1; } int ExtractTrainTickets(planeStruct TrainInfo) { ifstream tin("tickets.txt"); char string[1024], *tmp; int n = 0; while (!tin.eof()) { tin.getline(string, 1024); char *name = strtok(string, ":"); if (name == NULL) continue; if (strcmp(TrainInfo.tname, name)) continue; char *pyti = strtok(NULL, ":"); int c_ways = GetPossiblePyti(TrainInfo); char **w = new char *[c_ways]; if (pyti[0] != 'w') { for (int i = 0; i < c_ways; i++) for (int j = 0; j < TrainInfo.sectora; j++) for (int x = 0; x < TrainInfo.seats; x++) TrainInfo.tickets[i][j][x] = 0; continue; } else { w[0] = strtok(pyti, "|"); for (int i = 1; i < c_ways; i++) w[i] = strtok(NULL, "|"); /*for(int i = 0; i < c_ways; i++) if(w[i] != NULL) cout<<w[i]<<endl;*/ } //------------------------- //cout<<"-----------------------"<<endl; for (int i = 0; i < c_ways; i++) { char **wagonslist = new char*[TrainInfo.sectora]; tmp = strtok(w[i], "="); tmp = strtok(NULL, "="); wagonslist[0] = strtok(tmp, "_"); for (int j = 1; j < TrainInfo.sectora; j++) wagonslist[j] = strtok(NULL, "_"); tmp = NULL; for (int j = 0; j < TrainInfo.sectora; j++) { if (wagonslist[j] != NULL) { tmp = strtok(wagonslist[j], ","); for (int x = 0; x < TrainInfo.seats; x++) { if (tmp != NULL) { TrainInfo.tickets[i][j][x] = atoi(tmp); tmp = strtok(NULL, ","); } } } } } n++; } return n; } int ExtractPlanePyti(planeStruct *TrainInfo) { ifstream fin("pyti.txt"); fin.clear(); fin.seekg(0); int n = 0; char *tmp; char string[128]; while (!fin.eof()) { fin.getline(string, 128); char str[64]; strcpy(str, string); tmp = strtok(str, ","); int fairport = 0; while (tmp != NULL) { fairport++; tmp = strtok(NULL, ","); } fairport -= 2; TrainInfo[n].airport = fairport; TrainInfo[n].stList = new char *[fairport]; strcpy(str, string); tmp = strtok(str, ","); for (int i = 0; i < fairport; i++) { TrainInfo[n].stList[i] = new char[32]; strcpy(TrainInfo[n].stList[i], tmp); tmp = strtok(NULL, ","); } strcpy(TrainInfo[n].tname, TrainInfo[n].stList[0]); strcat(TrainInfo[n].tname, " - "); strcat(TrainInfo[n].tname, TrainInfo[n].stList[fairport - 1]); TrainInfo[n].sectora = atoi(tmp); tmp = strtok(NULL, ","); TrainInfo[n].seats = atoi(tmp); n++; } cout << endl << endl; for (int m = 0; m < n; m++) { int pyti = GetPossiblePyti(TrainInfo[m]); TrainInfo[m].tickets = new int **[pyti]; for (int i = 0; i < pyti; i++) { TrainInfo[m].tickets[i] = new int *[TrainInfo[m].sectora]; for (int j = 0; j < TrainInfo[m].sectora; j++) { TrainInfo[m].tickets[i][j] = new int[TrainInfo[m].seats]; for (int x = 0; x < TrainInfo[m].seats; x++) TrainInfo[m].tickets[i][j][x] = 0; } } //ShowPlaneInfo(PlaneInfo[m]); } //SavePlaneTickets(PlaneInfo, n); return n; } void SavePlaneTickets(planeStruct *PlaneInfo, int n) { ofstream fout("tickets.txt"); for (int m = 0; m < n; m++) { int pyti = GetPossiblePyti(PlaneInfo[m]); //cout<<endl<<PlaneInfo[m].tname<<" "<<PlaneInfo[m].sectora<<" "<<PlaneInfo[m].seats<<endl; fout << PlaneInfo[m].tname << ":"; for (int i = 0; i < pyti; i++) { fout << "w="; for (int j = 0; j < PlaneInfo[m].sectora; j++) { for (int x = 0; x < PlaneInfo[m].seats; x++) { if (x != PlaneInfo[m].seats - 1) fout << PlaneInfo[m].tickets[i][j][x] << ","; else fout << PlaneInfo[m].tickets[i][j][x]; } fout << "_"; } fout << "|"; } fout << endl; } } void ShowPlaneInfo(planeStruct TrainInfo) { int pyti = GetPossiblePyti(TrainInfo); cout << endl << TrainInfo.tname << " " << TrainInfo.sectora << " " << TrainInfo.seats << endl; for (int i = 0; i < pyti; i++) { cout << i + 1 << ") "; for (int j = 0; j < TrainInfo.sectora; j++) { for (int x = 0; x < TrainInfo.seats; x++) cout << TrainInfo.tickets[i][j][x] << " "; cout << " "; } cout << endl; } } int GetPossiblePyti(planeStruct TrainInfo) { int pyti = 0, fairport = TrainInfo.airport; for (int i = 0; i < fairport - 1; i++) for (int j = i + 1; j < fairport; j++) pyti++; return pyti; } |
I am using Visual Studio Express 2012 for Windows Desktop.
I always get error
Error C4996: 'strtok': This function or variable may be unsafe.
Consider using strtok_s instead.
To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
See online help for details.
When I try to build the following:
#include "stdafx.h"
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char the_string[81], *p;
cout << "Input a string to parse: ";
cin.getline(the_string, 81);
p = strtok(the_string, ",");
while (p != NULL) {
cout << p << endl;
p = strtok(NULL, ",");
}
system("PAUSE");
return 0;
}
Why am I getting this error even though I define _CRT_SECURE_NO_WARNINGS
, and how do I fix it?
I need to print the components of a given char(in this case the numbers which are separated pe whitespece) and I don’t understand why this doesn’t work(doesn’t compile): (or here http://ideone.com/JSrqg5).
The errors are:
error C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:userselllydocumentsvisual studio 2013projectsconsoleapplication15446consoleapplication15446source.cpp 18 1 ConsoleApplication15446
error C2664: 'char *strcpy(char *,const char *)' : cannot convert argument 1 from 'char' to 'char *' c:userselllydocumentsvisual studio 2013projectsconsoleapplication15446consoleapplication15446source.cpp 21 1 ConsoleApplication15446
And the code:
#include <iostream>
#include <cstring>
using namespace std;
char s[20000], *p, a[5000], separator[] = " ";
int k = 0, i;
void separare();
void afisare();
int main()
{
cout << "Sirul de nr:"; cin.get(s, 100, 'n');
separare();
afisare();
system("pause");
return 0;
}
void separare()
{
p = strtok(s, separator);
while (p)
{
strcpy(a[k], p);
k++;
p = strtok(NULL, separator);
}
}
void afisare()
{
int i;
for (i = 0; i < k; i++)
cout << a[i] << " ";
}
Dettorer
1,2471 gold badge8 silver badges26 bronze badges
asked Dec 10, 2014 at 14:33
11
Your code seems ok, if you are running on linux though, you must check that the file /lib/modules/$(uname -r)/build points to a valid location of your header files (usually in /usr/src/linux-headers-XXXX).
A lot of times after installing new kernel and headers, this link gets broken, and then your programs cannot link to the libraries, and for that reason it won’t compile.
Hope it helps.
By the way you cannot use a[k] for strcpy, as this is dereferencing a single char, while the input must be char*. You should use pointer notation instead, i.e (a+k) would work in this case.
answered Dec 10, 2014 at 15:04
Error C4996 using strtok and strcat
Hello!
I programmed this member function for a «client» class, where I am receiving through command line something like this: host/path1/path2/path3/filename
And in this function I trying to separate the information between the ‘/’ and store it in my class variables host, path and filename (all of them are of type char*)
However this is not working, when compiling I get:
error C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. error C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
I would like to understand why this functions are «unsafe» and any information about this is well received.
Should I do this another way, like trying to do this with strings?
Should I disable deprecation with _CRT_SECURE_NO_WARNINGS?
I am working in visual studio
Here is my function checkCommand:
bool Client::checkCommand(int argc_, char* arguments) { if (argc_ == 2) { char* tokens[MAXTOKENS] = { }; char* tokenPtr = strtok(arguments, "/"); int i, totalTokens = 0; while (tokenPtr != NULL && totalTokens < MAXTOKENS) { tokens[totalTokens] = tokenPtr; tokenPtr = strtok(NULL, "/"); } this->host = tokens[0]; this->filename = tokens[totalTokens]; this->path = tokens[2]; /*here i concatenate my first path to the rest of the paths*/ for (i = 3; i < totalTokens; i++) { strcat(this->path, tokens[i]); strcat(this->path, "/"); } return true; } else return false; }
thank you!
- Remove From My Forums
-
Question
-
i have these warnings in program and my program have a problem,i think so that this warning causes these problems
somebody help me
Warning 2 warning C4996: ‘sprintf’: This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando
luizteste-3205ps3000.c 336 1 Programa 3205
Warning 3 warning C4996: ‘strcpy’: This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando
luizteste-3205ps3000.c 363 1 Programa 3205
Warning 4 warning C4996: ‘fopen’: This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando luizteste-3205ps3000.c
369 1 Programa 3205
Warning 5 warning C4996: ‘fscanf’: This function or variable may be unsafe. Consider using fscanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando
luizteste-3205ps3000.c 371 1 Programa 3205
Warning 6 warning C4996: ‘strtok’: This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando
luizteste-3205ps3000.c 374 1 Programa 3205
Warning 7 warning C4996: ‘strtok’: This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando
luizteste-3205ps3000.c 375 1 Programa 3205
Warning 8 warning C4244: ‘function’ : conversion from ‘long’ to ‘short’, possible loss of data f:fernando luizteste-3205ps3000.c 436 1 Programa 3205
Warning 9 warning C4996: ‘sprintf’: This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando
luizteste-3205ps3000.c 718 1 Programa 3205
Warning 10 warning C4996: ‘sprintf’: This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando
luizteste-3205ps3000.c 727 1 Programa 3205
Warning 11 warning C4996: ‘fopen’: This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando luizteste-3205ps3000.c
729 1 Programa 3205
Warning 12 warning C4996: ‘fopen’: This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando luizteste-3205ps3000.c
754 1 Programa 3205
Warning 13 warning C4996: ‘sprintf’: This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando
luizteste-3205ps3000.c 766 1 Programa 3205
Warning 14 warning C4996: ‘fopen’: This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando luizteste-3205ps3000.c
768 1 Programa 3205
Warning 15 warning C4996: ‘fopen’: This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando luizteste-3205ps3000.c
792 1 Programa 3205
Warning 16 warning C4996: ‘sprintf’: This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando
luizteste-3205ps3000.c 806 1 Programa 3205
Warning 17 warning C4996: ‘fopen’: This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando luizteste-3205ps3000.c
808 1 Programa 3205
Warning 18 warning C4996: ‘fopen’: This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando luizteste-3205ps3000.c
830 1 Programa 3205
Warning 19 warning C4996: ‘sprintf’: This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando
luizteste-3205ps3000.c 842 1 Programa 3205
Warning 20 warning C4996: ‘fopen’: This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando luizteste-3205ps3000.c
844 1 Programa 3205
Warning 21 warning C4996: ‘fopen’: This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando luizteste-3205ps3000.c
866 1 Programa 3205
Warning 22 warning C4996: ‘strcpy’: This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando
luizteste-3205ps3000.c 911 1 Programa 3205
Warning 23 warning C4996: ‘strcpy’: This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. f:fernando
luizteste-3205ps3000.c 939 1 Programa 3205
Answers
-
Yes. Those errors are warning only. Microsoft added new secure C Run time library functions for write secure applications. all the secure functions end with _s. It will validate the destination buffer size. If you want disable those warnings, as the
warning message explained you can disable all warning. But, your software may face buffer overflow or underflow error. Hackers can easily hack your software. So, if you want build your software in secured way or want handle secure data, you should use secure
CRT functions.You can check my article
http://www.codeproject.com/KB/security/SecureStringHandling.aspx for more details.If you want disable, you can follow the settings.
Project properties->Configuration Properties->C/C++->Preprocessor->Preprocessor Definitions -> _CRT_SECURE_NO_WARNINGS
Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/
-
Marked as answer by
Wednesday, May 25, 2011 8:19 AM
-
Marked as answer by
-
FILE._file have max 512 positions
_setmaxstdio
http://msdn.microsoft.com/en-us/library/6e3b887c.aspx«C run-time I/O now supports many more open files on Win32
platforms than in previous versions. Up to 512 files can be
open simultaneously at the stdio level (that is, opened and
accessed by means of the fopen, fgetc, fputc, and so forth
family of functions). The limit of 512 open files at the
stdio level can be increased to a maximum of 2,048 by
means of the _setmaxstdio function.»— Wayne
-
Marked as answer by
Rob Pan
Wednesday, May 25, 2011 8:19 AM
-
Marked as answer by