473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

how to read a txt file into an array

11
I have a text file called A.txt that I am trying to read each character frequency from. The best way to show my problem is by example. Say the file reads "AbCAAb",

I have created an array of 256 memory positions to store each character frequency. So, first we encounter a capital A, which is the ascii value 65. So I go to position 65 in the array, and increment the count to 1. Then little b is 97, so I go to position 97 in the array...once I hit the next capital A, it should go to position 65 in the array and increment its counter to 2, then 3 for the next A. In the end, I should have an array consisting of zeros, where there were some characters that were not found in the file, and the frequencies of characters in the file that were found.

************************************************** ********************
THIS IS MY CODE THUS FAR
************************************************** ********************
//ignore the test points, they are for debugging purposes

Expand|Select|Wrap|Line Numbers
  1. int getFrequency(int arrayOfCharacters[])
  2. {
  3.     ifstream in;
  4.     in.open("A.txt");
  5.     cout<<"TEST POINT 2\n";
  6.     for(int i = 0; i < 256; i++)
  7.     {
  8.         arrayOfCharacters[i] = 0;
  9.     }
  10.  
  11.     cout<<"TEST POINT 3\n";
  12.     int c = in.get();
  13.     arrayOfCharacters[c]++;
  14.  
  15.  
  16.     while(c!=EOF)
  17.     {
  18.     c = in.get();
  19.     arrayOfCharacters[c]++;
  20.  
  21.     }
  22.  
  23.     for(int j = 0; j < 256; j++)
  24.     {
  25.         cout<<arrayOfCharacters[c];
  26.     }
  27.  
  28.     in.close();
  29.     return c;
  30.  
  31. }
Nov 26 '09 #1
8 4200
weaknessforcats
9,208 Expert Mod 8TB
Remember that arrays are 0 based. The 65th element for your A is array[64].
Nov 26 '09 #2
whodgson
542 512MB
So what succes have you had to date? What is your question?
Woops how do you read a text file into an array?....right?
Use the cin.getline () function after opening the file in read mode (r).
Nov 27 '09 #3
nar0122
11
So far I am getting the right results in the array aside from the last character in the array. Everything looks to be right, then I am getting a large number like 4677907 in the very last position in the array? I also need to know how to make the out put say, "There are (number of characters) (character)."

Example: File has "AAAaaa"

Output should read:

There are 3 A's.
There are 3 a's.
Nov 27 '09 #4
weaknessforcats
9,208 Expert Mod 8TB
Are you using array[256] as your last element?

If you are, you need a 257 element array.

That would explain the garbage in the last element. Apparently my Post #2 wasn't clear enough.
Nov 27 '09 #5
jfwfmt
11
You are probably not checking for end of file correctly. Look at the documentation of .get() and its friends (google get())

/s/ Jim WIlliams

also note that 0-037, 0177 and 0200-0237 (octal) are not directly printable
Nov 27 '09 #6
donbock
2,426 Expert 2GB
Don't know about C++, but in C the char type can be either signed or unsigned. To be fully portable you should do something to insure that you never use a negative array index.
Dec 1 '09 #7
donbock
2,426 Expert 2GB
The following snippets show how to maximize portability by rooting out assumptions about implementation-dependent behavior.
Expand|Select|Wrap|Line Numbers
  1. #include <limits.h>        // Obtain UCHAR_MAX.
  2. #define NCHARACTERS (UCHAR_MAX + 1)    // Don't assume 256.
  3. ...
  4. int countingArray[NCHARACTERS];
  5. ...
  6. int i, c;
  7. ...
  8. for (i=0; i<NCHARACTERS; i++) {
  9.     countingArray[i] = 0;
  10. }
  11. ...
  12. do {
  13.     c = in.get();
  14.     if (c != EOF)
  15.     {
  16.         i = ((unsigned char) c);    // Protect from compilers where char is signed.
  17.         countingArray[i]++;
  18.     }
  19. } while (c != EOF);
  20. ...
  21. for (i=0; i<NCHARACTERS; i++) {
  22.     c = ((char) ((unsigned char) i));        // Recover printable character code (if you need it).
  23.     ...
  24. }
I'm not sure the casts on lines 16 and 22 are fully portable, but it seems reasonable to expect char and unsigned char to be interconvertible regardless of the integer encoding used by the compiler.

The double-cast on line 22 is probably excessive. On most (perhaps all) platforms it would be ok to cast directly from int to char.
Dec 1 '09 #8
weaknessforcats
9,208 Expert Mod 8TB
I am going to offend someone, I just know it, but here goes:

1) The C++ header is <limits> and not <limits.h>. None of the standard C++ headers is guaranteed to be C-compatible and <limits> is a good example. All .h standard header are fo C. C++ standard headers have no extension and are in the std namespace.

2) In C++ you use the numeric_limits template for info on your type rather than the C sizeof.

3) There is no C cast in C++. The cast form (char) is compilable in C++ only as a backwards compatible feature. It doesn't work the same in C++ as in C. Using this in C++ will call conversion operator, if available. You are supposed to use the C++ cast forms.

4) The only time you need to cast in C++ is a) you are calling a relic C function that has a void* argument or some such or b) your C++ design is screwed up.

5) Prefer using vector to using arrays.

6) Prefer using the accumulates algorithm to writing loops.

7) Do not use macros in C++. There are several features in C++ that are direct maco replacements: a) templates, b) inline functions, c) enums, d) namespaces.
Dec 1 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Chris | last post by:
Can anybody help. I need to read a txt file backwords line by line. Can anybody help me do this. Thanks Chris
5
by: deko | last post by:
I have a text file ("eighty.txt") that looks like this: 83|84|85|86 I can read the file into an array like this: $numbers= file("eighty.txt"); But how do I key the array? I'd like to use...
3
by: deko | last post by:
It's nice to be able to generate an html table from a PHP array. I know how to do this, but the array in question is built from a file. The file in question can be very long, and I only want the...
3
by: Wei-Chao Hsu | last post by:
There are some data files look like 1.) data1.txt ---------------- 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 2.) data2.txt
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
9
by: srikanth | last post by:
i have a text file like below, test.txt file (actually my test file file is with 10000 lines but here i tested with 3 lines) 3 06.09.2006 16:37:25 3 06.09.2006 16:40:02 3 06.09.2006 16:42:31...
14
by: chance | last post by:
Hello, I have a file on disk called TEMP.ZIP and I would like to somehow get this into a memory stream so I can eventually do this: row = dataStream.ToArray() However, I am not sure of the...
5
by: runsun | last post by:
Thanks in advance. This program is written in C. It needs to read all characters from a file; then write them into a 3D array (yes, 3D!). The file is a .prn file (one of the Excel types), which...
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.