So I’m attempting to create a Befunge interperter and reading a text file into an array.
I’m using this code:
char map[100][100]; //not 85 x 20
//load the source
ifstream f;
f.open("file.txt", ios::in);
string s;
int i = 0;
while(f.good() && i < 100)
{
getline(f, s);
map[i] = s.c_str();
i++;
}
This doesn’t work, does anyone know a way to do it without manually looping through the string?
Use
strncpy()and specify the number of bytes:instead of:
By specifying the number of bytes copied as, at most,
100, you ensure that you don’t overflowmap[i]. Thestrncpy()function will padmap[i]with terminators, ifstrlen(s.c_str()) < 100. In the case wherestrlen(s.c_str()) >= 100, the string will be truncated in order to providemap[i]with the requisite null terminator.