troubleshooting Question

need help - access violation - fstream

Avatar of polkadot
polkadot asked on
C++Editors IDEs
5 Comments2 Solutions605 ViewsLast Modified:
Im not sure what I'm doing wrong with this bit of code, just reading from one file, writing to another

Debuggin in VC 6.0 get these errors during run time:

First-chance exception in FunctionCalls.exe: 0xC0000005: Access Violation.
The thread 0x884 has exited with code -1073741819 (0xC0000005).
The program 'C:\Documents and Settings\Shana\Desktop\FunctionCalls\Debug\FunctionCalls.exe' has exited with code -1073741819 (0xC0000005).


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void txtStats(ifstream & in, ofstream & out);
void initalizeArray(int a[], int n);



int main()
{
      ifstream myFile ("example.txt");
      ofstream myData ("data.txt");


      if (myFile.is_open() && myData.is_open())
      {
            txtStats(myFile, myData);

            myFile.close();
            myData.close();
      }


      return 0;
}


void txtStats(ifstream & in, ofstream & out)
{
      int characterFequency[26];
      initalizeArray(characterFequency, 26);

      string word=""; //will hold one line of the file at a time
      int index=0; //will represent the index of the character in the array


      //scan the file word by word and count the number of accourances in the array

      while (! in.eof() )
    {

        in >> word;
        //check each character in the word
        for(int i=0; i< word.length() ; i++)
        {
            if(word[i] >='A' || word[i] <='Z')
                  index = word[i] - 'A';
            else
                  index = word[i] - 'a';

            characterFequency[index]++; //add one to the frequency of this character

        }

      }
        
      //output the results which are contained in the array to the output file called out

      for(int j=0; j< 26; j++)
      {
            char c = j+'a';
            out << c << "\t"<< characterFequency[j] <<"\n";
      }
}

void initalizeArray(int a[], int n)
{
      for(int i=0; i<n; i++)
            a[i]=0;
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros