tmp , , Word Definition. , , tmp .
, char . , , C/++ . ASCII () , . , , , . strtok , - .
, :
#include <iostream>
using namespace std;
class Entry
{
public:
Entry(const char *line);
char *Word;
char *Definition;
private:
char buffer[100];
};
Entry::Entry(const char *line)
{
strncpy(buffer, line, sizeof buffer);
buffer[sizeof buffer - 1] = '\0';
Word = strtok(buffer, ",");
Definition = strtok(0,",");
}
int main()
{
Entry *e = new Entry("drink,What you need after a long day work");
cout << "Word: " << e->Word << endl;
cout << "Def: " << e->Definition << endl;
cout << endl;
delete e;
e = 0;
return 0;
}
I changed the name from tmp to the buffer, since this is no longer a temporary value. I also used strncpy to prevent buffer overflows. Line buffer [sizeof buffer - 1] = '\ 0'; because if the string is larger than the buffer, there will be no terminator after the call.
source
share