I am working on a control break algorithm in C, and part of this algorithm is to have while loop check for the end of file and whether two strings equal each other.

the first string is a part of a typedef struct structure called 'record' rec.state, and the second one is a variable called statecontrol which I set to:e

while((fgets(line,MAXLINE-1,filein) != NULL)
		sscanf(line,"%s %s %s %s %d",rec.state,rec.county,rec.city,rec.salesman,&rec.sales);
		while(fgets(line,MAXLINE-1,filein) != NULL && rec.state == statecontrol)

I did various tests, and both strings print out the same information.

Recommended Answers

All 2 Replies

Strings are not compared with the == sign in C. Use strcmp(). When it returns 0, then the strings are equal.

>>rec.state == statecontrol

You can not compare two strings like that. All that is comparing is the address of the two character arrays, not their contents. call strcmp() to compare the two.

Also, I suspect you will have big algorithm problems because of the two while loops that both read the same file. Reading should be done only in one spot, not two.

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.