strcpy warning!

Apr 5, 2015 at 1:50pm
I have an assignment at school and every student is provided a code to run a program. I got trouble with the strcpy command. When I debug the program a warning appears.

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
  //Copyright: Nguyen Duc Minh Khoi (email: ducminhkhoi@gmail.com) - March 2015 NEW
//REMEMBER: Do not include any other library. Otherwise you will get 0 mark
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

char map[10][20]; //Variable stores map
vector<string> step; //Variable stores steps
int numberOfSteps = 0;

void readFiles(char *);
void printCurrentMap(char[10][20]); //convenience for checking your map

int main(int argc, char* argv[]){
	//for convenience just let filename = "testcase/testcase_1_X.txt" with X from 1 to 5: the order of your testcases. 
	//REMEMBER: change filename = argv[1] when submit !!!!!! Otherwise you will get 0 mark
	char * filename = argv[1];

	readFiles(filename);

	/////////////////////////////////////////////
	//TODO: Complete Your code after this line
	//Hint: You may find it easier to use functions to remove duplicate codes. 
	//      However, you can code anything else. Your input is read in 2 variables
	// 		char map[10][20] and vector<string> step (just like 1 dimensional array for convenience)



	//END TODO
	/////////////////////////////////////////////

	return 0;
}

//Description: print the current map for easier debugging
//INPUT: the current map variable
//OUTPUT: no output, but it will print the current map on screen
void printCurrentMap(char current_map[10][20]){
	cout << endl;
	cout << "Current Map:" << endl;
	cout << " - - - - - - - - - - - - - - - - - - - - - - " << endl;
	for (int i = 0; i < 10; i++){
		cout << 9 - i << "| ";
		for (int j = 0; j < 20; j++){
			cout << current_map[i][j] << " ";
		}

		cout << "|" << endl;
	}

	cout << " - - - - - - - - - - - - - - - - - - - - - - " << endl;
	cout << "   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9   " << endl;
}

//Description: read char map[10][20] and vector<string> step from file
//INPUT: file name in char *
//OUTPUT: no output, but it will change the global variables map[10][20] and vector step
//--------Given to Students----------//
void readFiles(char* testcase_1_1){
	string line;
	ifstream myfile(testcase_1_1);

	int i = 0;

	if (myfile.is_open()){
		while (getline(myfile, line)){
			if (i < 10){
				strcpy(map[i++], line.c_str());
			}
			else if (i >= 10){
				for (int j = 0; j < line.length(); j += 6){
					numberOfSteps++;
					string word = line.substr(j, 5);
					step.push_back(word);
				}
			}
		}

		myfile.close();
	}
	else {
		cout << "Unable to open file";
	}
}

Thanks inadvance
Last edited on Apr 5, 2015 at 2:01pm
Apr 5, 2015 at 1:57pm
When I debug the program a warning appears.


By the power invested in my, I shall make OP's warning Appear.

Could you please post the warning? And FYI, warnings don't prevent programs from compiling, you don't have to deal with the warning right now/at all, you can just continue writing your code. If its not compiling, they you got an error, not a warning. Always copy paste your errors.

In this case, you forgot to #include <cstring>

Edit:

*Turns out its error and not a warning*

A wild Error appears
ERROR wants to fight!
ERROR USES NO-COMPILE
A critical hit!
What will Programmer do?
Programmer selects "Run"
Got Away Safely!

Last edited on Apr 5, 2015 at 2:13pm
Apr 5, 2015 at 2:00pm
Error 1 error 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. c:\users\administrator\documents\visual studio 2013\projects\project5\project5\source.cpp 70 1 Project5
Warning 2 warning C4018: '<' : signed/unsigned mismatch c:\users\administrator\documents\visual studio 2013\projects\project5\project5\source.cpp 73 1 Project5
Here is my problem. An error and a warning for strcpy(map[i++], line.c_str());. thanks my friend
Apr 5, 2015 at 2:03pm
A wild Warning appears
Programmer used -w flag it's super effective
Warning fainted
Programmer is hurt by recoil



By the way, you can't use strcpy() because you'll need to #include <cstring> which you can't because ``Do not include any other library. Otherwise you will get 0 mark''


> 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.
that's just your compiler being too much helpful.
strcpy() doesn't check that you have enough space reserve to not write out of bounds, strcpy_s() is a not standard function that ask for the size of the buffer
Last edited on Apr 5, 2015 at 2:13pm
Apr 5, 2015 at 2:07pm
some of my friends use visual studio 2010 and they said "It works!". I'm using ver 2013 and I got stuck :(
Apr 5, 2015 at 2:07pm
This function or variable may be unsafe. Consider using strcpy_s instead.

That should be self-explanatory. Why do you use strcpy?

'<' : signed/unsigned mismatch

That refers to int j = 0; j < line.length();
The type of line.length() is unsigned. Consider size_t j = 0; j < line.length();
Apr 5, 2015 at 2:10pm
The lab tutor provided the code for us so do I need to fix for the code they gave?
Apr 5, 2015 at 2:14pm
To disable deprecation, use _CRT_SECURE_NO_WARNINGS
Topic archived. No new replies allowed.