-3

This is what I have so far; I am trying to have an array with probability of all chars and space in a text file, but I have a problem with the data type.

int main()
{
float x[27];
unsigned sum = 0;
struct Count {
    unsigned n;
    void print(unsigned index, unsigned total) {

        char c = (char)index;
        if (isprint(c)) cout << "'" << c << "'";
        else cout << "'\\" << index << "'";
        cout << " occured " << n << "/" << total << " times";
        cout << ", propability is " << (double)n / total << "\n";
    }
    Count() : n() {}
} count[256];
ifstream myfile("C:\\text.txt"); // one \ masks the other
while (!myfile.eof()) {
    char c;
    myfile.get(c);
    if (!myfile) break;
    sum++;
    count[(unsigned char)c].n++;
}
for (unsigned i = 0; i<256; i++)
{
    count[i].print(i, sum);
}
x[0] = count[33];
int j=68;
 for(int i=1;i<27;i++)
 {
     x[i]=count[j];
     j++;
 }
return 0;
}
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120

2 Answers2

2
#include <iostream>
#include <fstream>
#include <cctype>    
using namespace std;

double probabilities[256]; // now it can be accessed by Count

int main()
{
  unsigned sum = 0;
  struct Count {
    unsigned n;
    double prob;
    void print ( unsigned index, unsigned total ) {
      // if ( ! n ) return;
      probabilities[index] = prob = (double)n/total;
      char c = (char) index;
      if ( isprint(c) ) cout << "'" << c << "'";
      else cout << "'\\" << index << "'";
      cout<<" seen "<<n<<"/"<<total<<" times, probability is "<<prob<<endl;
    }
    Count(): n(), prob() {}
    operator double() const { return prob; }
    operator float() const { return (float)prob; }
  } count[256];
  ifstream myfile("C:\\text.txt"); // one \ masks the other
  while(!myfile.eof()) {
    char c;
    myfile.get(c);
    if ( !myfile ) break;
    sum++;
    count[(unsigned char)c].n++;
  }
  for ( unsigned i=0; i<256; i++ ) count[i].print(i,sum);
  return 0;
}

I incorporated various changes suggested - Thanks!

Now, who finds the 4 ways to access the actual probabilities?

Hans Klünder
  • 1,996
  • 10
  • 7
  • This `fopen` works for linux only it calls the linux kernel system call – Saher Ahwal Jan 01 '15 at 00:21
  • @Saher Nope, `fopen` is in the C and C++ standard. Should work on any system that has a file system in the first place. – Christopher Creutzig Jan 01 '15 at 00:23
  • Error 1 error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. – Ahmed EL Said Jan 01 '15 at 00:23
  • why does "count" need to be declared as static? – selbie Jan 01 '15 at 00:26
  • @AhmedELSaid - That's the standard Visual C++ warning for using legacy CRT functions. Just define _CRT_SECURE_NO_WARNINGS and that warning/error goes away. – selbie Jan 01 '15 at 00:27
  • @selbie declaring an array as static will, among other things, initialize it all to 0 `unsigned count[256] = {0};` would also work see [here](http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c). – ryanpattison Jan 01 '15 at 00:28
  • @AhmedELSaid Yes, that’s a problem with VS, and unfortunately, the best solution for C code I know of is to disable all these warnings, much as I hate to do such a thing. For C++, the iostream classes are easier to use anyway. Cf. http://stackoverflow.com/questions/14386/fopen-deprecated-warning – Christopher Creutzig Jan 01 '15 at 00:29
  • @ChristopherCreutzig I see I had no idea thanks. If I have windows and not planning to have the code run on other OS, I would stick with windows sdk, but just my opinion – Saher Ahwal Jan 01 '15 at 00:31
  • might wanna check `if (sum!=0)`, before the loop/output. – ryanpattison Jan 01 '15 at 00:32
  • @hans thanx for your help so far and for your last note if you can finally show me how to have array having these values better than just cout them – Ahmed EL Said Jan 01 '15 at 00:42
  • @rpattiso - true that `static` will zero-init on the *first* invocation of the function. But if you ever wanted to call the function again, the array will not be reset. Hence, ={0} works better. Also thread safe. – selbie Jan 01 '15 at 00:47
  • there is a proplem in the value of spaces (-1.07374e+008) – Ahmed EL Said Jan 01 '15 at 01:26
  • I changed the count for spaces a bit now. – Hans Klünder Jan 01 '15 at 01:36
  • space proplem is fixed but now there is no count fo the z – Ahmed EL Said Jan 01 '15 at 13:51
  • Then just delete the line `if ( ! n ) return;` – Hans Klünder Jan 01 '15 at 13:53
  • @hans done now what is the name of the array that holds the value for the proppability because i need to use it in the rest of the program – Ahmed EL Said Jan 01 '15 at 14:01
  • There is no such array, but you can create one. You see that there is code that prints every of the 256 probabilities. Now it should not be toot hard to spot that code and copy the probability into another array that you prepare somewhere. Or add a member to Count that keeps it. Or add a method to Count that computes it. There are countless ways to get the probability when you need it. At least try some of them – Hans Klünder Jan 01 '15 at 14:11
  • can i at least know the data type i will save to becease the compiler refuseing them all – Ahmed EL Said Jan 01 '15 at 14:36
  • cannot convert 'main()::Count' to 'float' in assignment – Ahmed EL Said Jan 01 '15 at 14:47
  • Ahmed, I appreciate that you compile my code together with yours, send me the output and wait for my work progress. I suggest you learn some C++ and then try to find out how to read a program. It's not too hard. After a while, you will be able to write some code yourself, and then it's fun. – Hans Klünder Jan 01 '15 at 14:56
  • thanx for the advice can you tell me about agood website to learn from – Ahmed EL Said Jan 01 '15 at 15:12
  • No, please google for C++ tutorials and see which seem understandable for you. And write lots of examples, maybe simple examples. Run them, see what they do and why. Sorry for my impatience, but everything here is simple C++, and if you don't learn to read a structure yourself, or have an idea about how to fill an array, then it won't help you to get source code here because you fail the next time you have to change a line. Please learn the basics if you want to develop in C++. I have added 4 ways to access the probabilities now, but finally you have to learn how to see it and how to do it. – Hans Klünder Jan 01 '15 at 15:18
  • in order to run your code take this line(double probabilities[256] = {};) inside the print function – Ahmed EL Said Jan 01 '15 at 15:27
0

you are allocating a buffer with size 1000000 1 million characters.

char file[1000000] = "C:\text.txt";

This is not good as the extra values in the buffer are not guaranteed to be zero, the can be anything.

For Windows to read a file you need something like this. I will not give you the solution, you need to learn using msdn and documentation to understand this fully::

you need to include the #include <windows.h> header from the SDK first.

Look at this example here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363778(v=vs.85).aspx

this example as appending a file to another. Your solution will be similar, instead of writing list to other file, process the buffer to increment your local variables and update the state of the table.

Do not set a large number you come up with for the buffer, as there will risk of not enough buffer space, and thus overflow. You should do like example:

  • read some bytes in buffer
  • process that buffer and increment the table
  • repeat until you reach end of file

    while (ReadFile(hFile, buff, sizeof(buff), &dwBytesRead, NULL) && dwBytesRead > 0) { // write you logic here }

Saher Ahwal
  • 8,230
  • 28
  • 75
  • 136
  • iam trying to save the text file in an array of char but i dont no the size of the array so i assumed the nuber will be good – Ahmed EL Said Jan 01 '15 at 00:04
  • That is not the way you do it then, you should read the file into a buffer and do the counting – Saher Ahwal Jan 01 '15 at 00:06
  • iam sorry idont know how if you can show me this will be great – Ahmed EL Said Jan 01 '15 at 00:07
  • You should use the fstream class, which behaves very much like cin/court, as it's from the same family of classes. http://www.cplusplus.com/doc/tutorial/files/ – Martyn Shutt Jan 01 '15 at 00:12
  • @saher The example by Hans Klunder will work on windows, not linux – linux doesn’t allow the file name `c:\text.txt`. I really suggest to use `fopen` or, probably better, the iostream classes, everywhere. It’s just the portable way of writing file I/O. – Christopher Creutzig Jan 01 '15 at 00:17
  • but windows doesn't have fopen system call, does it? – Saher Ahwal Jan 01 '15 at 00:20
  • I agree, use the language. fopen or ifstream (and yes, of course windows has it, although you just might have to #define DONT_GIVE_ME_FAKE_ERRORS or something similar on some microsoft compilers.) – Kenny Ostrom Jan 01 '15 at 00:34