ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to write a code that can read a txt file and count the characters(this include dots, spaces, comma and semicolon) and at the end calculate the size of this file, for example
What programming language are you using? here is the logic I would use with a reasonably low level language:
1. Set up an array of 256 words (4 byte integer) with all elements initialed to binary zero.
2. Go through the file character by character. Use the character as the index into the array. In some languages you might have to multiply the hex value of the character by 4 and use the product as the index into the array. Add 1 to the element that the index points to.
3. When you reach end of file loop through the array elements, 0 through 255. If the value of the element is non-zero then print the character and the number of times that it occurred.
4. Keep a running total of the number of characters and print the total at the end.
If you want to learn how to tackle this problem then you should start off with providing the code that you currently have, how about just reading the file character by character? Can you achieve that, what does your code look like to achieve that?
but can I just put this code in my c++ file source and it's ganna work, I don't think so, and I just know c++ language, so any help plssss.
Many thanks to all
Someone asking for help really should not give smart arse replies!!
As graemef has already said:
Quote:
If you want to learn how to tackle this problem then you should start off with providing the code that you currently have, how about just reading the file character by character? Can you achieve that, what does your code look like to achieve that?
Thanks all of u,
but can I just put this code in my c++ file source and it's ganna work, I don't think so, and I just know c++ language, so any help plssss.
Are you running Visual C++ under Linux? Seems like you've gotten two different solutions, using the Linux built-ins. This also seems a bit like homework to me....
Quote:
Originally Posted by dmail
Someone asking for help really should not give smart arse replies!!
Regardless of whether or not its homework, it's a f*cking simple task, and I'm a little upset that someone is more than willing to beg for help, before trying to figure it out for themselves.
As for getting the size of the file, grab yourself a copy of the Win32 API, and look for the class that represents a file, and the procedure inherited from that class for returning the file size.
Regardless of whether or not its homework, it's a f*cking simple task, and I'm a little upset that someone is more than willing to beg for help, before trying to figure it out for themselves.
Well, I was thinking that as well...along with "If you say you KNOW C++ already, why can't you already do this?"
Typically this forum is a very friendly place to learn programming. This is one of the very few occasions of the more than two years that I have been visiting that I have seen people, shall I say lose their cool, I'll just put it down to Friday frustrations. However, you have not helped the situation. All that we ask is that you try. Show us what you can manage, and there will be someone here who will help you get to the next stage. Programming is a journey and if you want to travel it then you will need to walk with us.
Please remember this forum will help you to learn programming it will not (directly) help you get grades.
Thanks for all who helped or at least try to help me, and yess I'll try to start writing the code first then I'll ask for help later, and I'm sorry if someone thought it's a homework or something like that. since I'm a begginer in programing and the most way I like to learn is reading the code and anaylsis it then understand it. and if someone has any resources about how to deal with files in C++, I'll be gratfull.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.