LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2008, 06:17 PM   #1
Medoo
LQ Newbie
 
Registered: Apr 2008
Posts: 6

Rep: Reputation: 0
a code to read a text file plssssssssssssssssss


Hi all,

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

TXT File hi.txt:
aaabb. ccd. dd; rr; jj,


this file has:
3(a)
2(b)
4' ' (spaces)
2(.) (dots)
3(d)
2( (semicolon)
2(j)
1(,) (comma)

anyhelp I'll be Grateful,
 
Old 04-09-2008, 06:36 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,338

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
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.

----------------------
Steve Stites
 
Old 04-10-2008, 02:09 AM   #3
Medoo
LQ Newbie
 
Registered: Apr 2008
Posts: 6

Original Poster
Rep: Reputation: 0
visual C++ 6.0

hi,

actually I use

visual c++ 6.0,

thansk for the explanation, but it seams a bit confusing to me

thank u again,
 
Old 04-10-2008, 04:13 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk ' BEGIN{FS=""}
{
  for(i=1;i<=NF;i++) all[$i]++
}END{
  for(o in all) print all[o],o
}
' file
to calculate size of file, you can use du, ls or wc
 
Old 04-10-2008, 05:47 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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?
 
Old 04-10-2008, 10:24 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Nice code, ghostdog74! Here is another one
Code:
#!/bin/bash
for char in $(printf "%o " {32..126})
do
    echo -e \\$char:\ $(cat testfile | tr -cd  \\$char | wc -c)
done
This count all characters, even if there are zero occurences.

Last edited by colucix; 04-10-2008 at 02:02 PM. Reason: forgotten sha-bang
 
Old 04-10-2008, 12:00 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by colucix View Post
Nice code, ghostdog74! Here is another one
Code:
/bin/bash
for char in $(printf "%o " {32..126})
do
    echo -e \\$char:\ $(cat testfile | tr -cd  \\$char | wc -c)
done
This count all characters, even if there are zero occurences.
thks
just a thought, the loop will call testfile 90+ times. it might be better to loop the file once instead , not sure though.
 
Old 04-11-2008, 10:34 AM   #8
Medoo
LQ Newbie
 
Registered: Apr 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks

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.

Many thanks to all
 
Old 04-11-2008, 11:20 AM   #9
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by Medoo View Post
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.

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?
 
Old 04-11-2008, 12:56 PM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,652

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by Medoo View Post
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!!
Agreed.
 
Old 04-11-2008, 01:11 PM   #11
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
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.
 
Old 04-11-2008, 01:48 PM   #12
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,652

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by indienick View Post
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?"
 
Old 04-11-2008, 05:59 PM   #13
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Medoo,

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.

Graeme.
 
Old 04-11-2008, 08:31 PM   #14
shambler
Member
 
Registered: Sep 2006
Location: Canada
Distribution: openSUSE 11.3, Xubuntu 10.10, Ubuntu 11.04
Posts: 53

Rep: Reputation: 23
I can't believe anyone would write a program to do that.

lines, words and characters:

wc filename

File size, which is the exact length:
ls -l filename

If I was forced to do it in C, it would something like this (framework left out):

FILE *f = fopen( "filename", "r" );
fseek( f, 0, SEEK_END );
length = ftell( f );
fclose( f );
printf( "Length is %d characters\n", length );
 
Old 04-12-2008, 12:46 AM   #15
Medoo
LQ Newbie
 
Registered: Apr 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Ok

Hi,

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.

Regards,
Medoo
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
use fscanf to read text from a file emp1953 Linux - Newbie 2 03-14-2008 03:08 PM
Need C++ Program to Read from Text File waXed Programming 5 10-26-2007 01:21 PM
Code to find out text file size Deepak Inbasekaran Programming 10 04-11-2006 04:52 AM
code to read block from the file suchi_s Programming 5 09-08-2004 06:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration