how to read a txt file into an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nar0122
    New Member
    • Nov 2009
    • 11

    how to read a txt file into an array

    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

    Code:
    int getFrequency(int arrayOfCharacters[])
    {
    	ifstream in;
    	in.open("A.txt");
    	cout<<"TEST POINT 2\n";
    	for(int i = 0; i < 256; i++)
    	{
    		arrayOfCharacters[i] = 0;
    	}
    
    	cout<<"TEST POINT 3\n";
    	int c = in.get();
    	arrayOfCharacters[c]++;
    
    	
    	while(c!=EOF)
    	{
    	c = in.get();
    	arrayOfCharacters[c]++;
    	
    	}
    
    	for(int j = 0; j < 256; j++)
    	{
    		cout<<arrayOfCharacters[c];
    	}
    
    	in.close();
    	return c;
    	
    }
    Last edited by Banfa; Nov 27 '09, 10:00 AM. Reason: Added [Code] ... [/code] tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Remember that arrays are 0 based. The 65th element for your A is array[64].

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      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).

      Comment

      • nar0122
        New Member
        • Nov 2009
        • 11

        #4
        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.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          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.

          Comment

          • jfwfmt
            New Member
            • Nov 2009
            • 12

            #6
            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

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              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.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                The following snippets show how to maximize portability by rooting out assumptions about implementation-dependent behavior.
                Code:
                #include <limits.h>        // Obtain UCHAR_MAX.
                #define NCHARACTERS (UCHAR_MAX + 1)    // Don't assume 256.
                ...
                int countingArray[NCHARACTERS];
                ...
                int i, c;
                ...
                for (i=0; i<NCHARACTERS; i++) {
                    countingArray[i] = 0;
                }
                ...
                do {
                    c = in.get();
                    if (c != EOF)
                    {
                        i = ((unsigned char) c);    // Protect from compilers where char is signed.
                        countingArray[i]++;
                    }
                } while (c != EOF);
                ...
                for (i=0; i<NCHARACTERS; i++) {
                    c = ((char) ((unsigned char) i));        // Recover printable character code (if you need it).
                    ...
                }
                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 interconvertibl e 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.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  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.

                  Comment

                  Working...