Hello everyone, i'm new in c++ and i found a problem when created a code.
I want to create a code which read from file .txt and then count the number of each value of the attribute, as the text file below: the attribute is'bembi' and the value is '1,2,3'. And i want my code return the number of the value which will return 3 in this case.
The file:

bembi: 1, 2, 3.
lala: continuous.
kaka: continuous.
ohmy: 4,5.

My code is:

fseek(fInNames, position, SEEK_SET);
num=0;
while(fgets(string, BUFFSIZE, fInNames))
{
	token = strtok(string, ":\t\n");
	if ((token != NULL) && !strchr(token, '|'))
	{
		num2[num]=0;
		notEnd = true;
		while (notEnd)
		{
			MaxAttribute++;
			token = strtok(NULL, ",\t");
			if(token == NULL)
			{
				fgets(string, BUFFSIZE, fInNames);
				token = strtok(string, ",\t");
			}
			else if (strchr(token, '|'))
				notEnd = false;
			else
			{
				num2[num]++;
				if(token[strlen(token)-1] == '.')
					notEnd = false;
			}
			printf("Sum of the atribut is %d\n", MaxAttribute++);
		}
		num++;
	}
	strcpy(string, "");
}

but, when i ran my code it keep looping and i don't know where exactly the error was..
Thank you so much.

Regards,
Sa

Recommended Answers

All 4 Replies

You should look into using ifstream. fseek, fgets, strtok, strchr, etc are all depricated.

@daviddoria: Ok, thank you for your suggestion, actually i don't know the function of ifstrem, i'm looking forward it, but could u give me some example?

And, actually i've created another code with 'fseek, fgets, strtok' and it works, maybe it's not the function but my algorithm is the problem as well, i really didn't know where is the problem in this code.. :(

Of course those functions still work, ifstream just makes life easier :)

Some googling should get you a long way - let us know if you have any specific questions.

Of course those functions still work, ifstream just makes life easier :)

Some googling should get you a long way - let us know if you have any specific questions.

i've read some at http://www.cplusplus.com/reference/iostream/ifstream/, and i think the problem of my code isn't in the function, when i block(by putting /**/) the second while in the code, the program run, but when i remove the block, it keep looping printed: "Sum of the atribut is %d\n" till i stop with Ctrl+C.
Any idea please? :(
thank you so much

note: i want my output to give me the number of the value of each attribut

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.