Reading a file, sorting and counting contents, help?

This is a discussion on Reading a file, sorting and counting contents, help? within the C Programming forums, part of the General Programming Boards category; Ok I have program due for my class. And it's a doozy, at least for me, I'm supposed to give ...

Reading a file, sorting and counting contents, help?

Ok I have program due for my class. And it's a doozy, at least for me, I'm supposed to give the user a choice of 5 files to read. Then tell them the first and last 10 words of the file and how many times that word occurs in the file. Then sort the words alphabetically and tell them the first and last 10 words again and how many times they occur. I need some help with a few aspects

1. How do I print the last 10 words?
2. How can I get the count to print in a table form. All pretty and even and stuff.
3. For some reason it only works when the user chooses 1. I have all the files saved in the same place. I can't see why it wouldn't work for the other choices.

Here's what I have so far, and just ignore that the count is 0 for every word, I haven't worked on that part yet. Thanks for any help in advance.

Sorry about the indentation, that's just how it copied and pasted into it. My professor told us we had to use fflush, and feof. He won't let us use things he hasn't taught us himself. So that didn't help, but thanks anyway.

I have read those links, I understand what you are saying. But I can't change my program to suit the content of those links due to restrictions from my professor. The fact that he is an idiot doesn't change the fact that I must program the way he wants, he's the one giving me the grade.

You're telling me you can't use the return value of fscanf, and are forced to rely on feof? I find that hard to believe. Also, do you know how to read a character at a time from a file? If so, that takes care of your input flush.

You're also saying that you're too lazy to edit your post and indent it I guess.

Now quite what that last entry is is a mystery, since the text file in question has only got TWO words in it.

> he's the one giving me the grade.
But you're the one paying for the education, and perhaps hoping to get a job at the end of it.
If he's nothing more than a "dung merchant", then isn't it better you figure that out sooner rather than later?

And yes I am saying that my professor forces me to use feof and fflush(stdin).

I know I'm the one paying for my education but I see no point in starting a revolution against my teacher because he uses some out of date coding, as long as I know that it's out of date and I know the better, more modern, alternative then it doesn't really matter what he makes me put in the program I turn in. Also I am in an intro to programming class, he's just introducing us to programming, I'm not expecting to come out of this class with a deep knowledge for all things programming related. Regardless I'd like to just code the way he wants, get a good grade in the class, and read online tutorials, read message boards, and talk to other programmers to get my programming knowledge.

Right now what I need help with is this program, that is restrained to the wishes of my professor. Can you please answer my questions?

It's not "out of date". It's wrong. Neither of those have ever been correct. They didn't just "update" them and now they're obsolete. They've never ever been the right way to do it.

Sure, I'll answer your question. Keep track of the total number of read items. Now subtract 10 from that. Start your loop there, end it when you've displayed ten items.

Perhaps your professor can teach you basic logic while they're at it.

Furthermore, I think you're a liar. I doubt very much that your professor said: "Do not use the return value of fscanf to see when you've reached the end. Instead, you MUST use feof to control your loop!"

Forgive me for saying they are out of date. Thank you for correcting me. Now I will know for future reference. But when this program compiles and run. It's what I've used for all previous homework assignments, it's what he uses in his examples.

And Furthermore I am not a liar. Who are you to say what my professor told me to do and what not to do. Do you honestly think anybody in their right mind would listen to so many people telling him that he is wrong and insist on doing it the wrong way anyway. THe only reason I am writing this program in this way is because I have been told to do it.

All I needed was a few questions answered. Is that so hard to do without confrontation?

Here's a sample code that I coded up (quite messy since I rushed it a bit )
Then again, it's better than nothing so! Note this uses fscanf() to control the loop not feof(), if you "must" use feof() then you should do something about the irregularities of using feof(). Such as being "1 loop" late at noticing the EOF indicator, which means you should minus 1 from count and discard the last reading. Even if your professor mentioned to use feof() I would recommend sending him an e-mail about it because like Quzah said it's wrong to do.

I've used different filenames to what you did but it shouldn't really matter. The sequential search algorithm is just used to count how many instances the word appears in the file after all the words are read in by the fscanf() function.

There are some small formatting issues that I would leave up to you to fix. It's primarily to give you an idea on how to approach the problem. I recommend you code small sections of the problem to see how it works before putting that section into your main code, it helps in both debugging and understanding the problem.

After you have implemented up to this stage with your files and how you want it formatted etc. you will need to make a sorting function or simply use the qsort() function from C as anonytmouse mentioned. Simply send in the strcmp() function as a function pointer to the qsort() function if you are using it.