strtok()

why doesn't the strtok() function work? I keep getting the error: This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.



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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

bool readInput(ifstream&, char[100]);
void tokenizeWords(char[100], char*, int&);
int main(int argc, char* argv[])
{
	ifstream inFile;
	ofstream outFile;
	char line[100];
	char* word;
	int numberOfWords = 0;
	bool error = false;

	if (argc = 3)
	{
		inFile.open(argv[1]);
		inFile.open(argv[2]);
	}
	else
		cout << "Too much or not enough argument(s). " << endl;

	while (!error)
	{
		error = readInput(inFile, line);
		if (!error)
		{
			tokenizeWords(line, word, numberOfWords);
		}
	}
	return 0;
}

bool readInput(ifstream& inFile, char line[100])
{
	if (inFile.fail())
	{
		cout << "Can't open input file." << endl;
		return true;
	}
	else
	{
		if (inFile.eof())
			inFile.getline(line, 100);
		else
			return true;
	}
}

void tokenizeWords(char line[100], char* word, int& numberOfWords)
{
	char* p;
	p = strtok(line, " .");
	while (p != NULL)
	{
		cout << p << endl;
		strcat(word, p);
		p = strtok(NULL, " .");
		numberOfWords++;
	}
}
But my strtok() function is still wrong. I dont make any progress in my strtok(), but isn't
p = strtok(NULL, " .");
suppose to look for a new token?
1
2
3
4
if (inFile.eof())
			inFile.getline(line, 100);
		else
			return true;

My guess is line is empty when you try to tokenize it.

Also, if (argc = 3) is a logic error.
Last edited on
Topic archived. No new replies allowed.