Hello all,
I had a question regarding strings and arrays, and how to properly use strcmp.
I have an assignment where one of the objective is to have the user input a .txt file and to use C to analyze some statistics on it. We have to do error checking on it, so we must check to see if the file the user inputed ended in ".txt". If the file does not end in .txt, an error message must print to the screen saying only .txt files may be analyzed. All of my program is working correctly except for this part:
It should be noted that fn is the filename that has been passed through to this function and is being analyzed. We MUST use strcmp to determine whether or not a file is .txt. As an example:Code:if (strcmp(".txt",&fn[strlen(fn)-4] != 0)) { printf("Only files ending with .txt can be analyzed.\n"); printf(" Your file: %s\n",fn); }
Let's say I have a filename of file.txt. In this program, it's stored in C as:
file.txt\n\0
The reason the \n character is in there is because we had to use the "fgets" function to retrieve the filename, and it automatically adds the \n. In another function, I removed the \n character, so by the time it gets to the above if statement, the filename will be:
file.txt\0
So I need to make my strcmp check to see if the filename ends in ".txt". The index number in the above example is 8. So the first index I would check would be position [4] to see if it is a "."
Next, I would check position [5] to see if it was a "t" and so on..
I realize the code I have above is completely wrong because I'm only calling on one part of fn and asking it whether it's equal to ".txt", which will obviously be false. Would I need to use a while or for loop?
The complier error code is what the subject of the thread is, so any help would be greatly appreciated. Thank you.