Arrays

Pages: 12
Hi Folks,

I need to know how to use arrays to open a file containing a text and read it into my program. I'd be happy to have some examples support.

Thanks
You can't use arrays to open and/or read files.

You can use std::ifstream to open and read files into arrays:
http://www.cplusplus.com/reference/iostream/ifstream/

7
8
9
10
11
12
13
14
15
16
17
18
19
std::ifstream in ("my file.txt");
unsigned int size = 0;
int *array = 0;
if(in.is_open())
{
    in >> size;
    array = new int[size];
    for(unsigned int i = 0; i < size; ++i)
    {
        in >> array[i];
    }
    in.close();
}
Last edited on
If i have the following text i need to determine the code used for the frequency analysis using array how to proceed?

afn pli rc imy lcqmfniyk efqjzfiynt pa imy lcaftmrpcfeoy yck pa imy zytiync thrnfo fnx pa imy sfofgd oryt f txfoo lcnysfnkyk dyoopz tlc. pnerircs imrt fi f krtifcqy pa nplsmod crcyid-izp xroorpc xroyt rt fc liiynod

Thank you for your help guys.

I'd recommend a std::map<std::string, unsigned int> to keep track of how many times each string occurs. I don't think a single array could be suitable for this unless you used std::pair, in which case you're just remaking the std::map anyway.
I came up with the following to start with code, I need to know if I'm on the right path.


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

int main() {
string in;
fstream input;
int a = 0 ;
int i;
char b;
int arr[27]; // array to store the number of charac
arr[0] = 0;

input.open("hw2.txt"); // opens the file

while (input.good() ) { // check if you have reached the end of file
getline(input,in); // copy a line at a time in in

for( i = 0 ; i < in.length(); i++ ) {
b = in.substr(i,1) ; // take out a character at a time

if (b == 'a') {

arr[1]++;
}
else if ( b == 'b') {

arr[2]++;
}

Feel free to make all necessary corrections for me please..

Thank you
This is a very bad idea...

Like I said, you should use a map<string, unsigned int> or map<char, unsigned int> so in your code you can do MyMap[b]++;
OK L B, but now how to input my text to do the frequency analysis?
Another thing, when you this error message: 'arr' was not declared in this scope, where to go to fix it and how?

Thank you for your help.
You already have the loop to input and analyze each char...
I copy and paste my text it gives a huge number for each character. I don't know why.
How to read the followings guys:

MyInts i = {0,0,{0}};

int z = i.MyAdd(2);

What is i in these functions?
You can use an array to hold the data. Each character is a number in the range 0-127, most of which you'll never encounter. Printable characters start at 32, which is SPACE.

So you can decare an array of counts. The array must large enough to deal with all characters, that is size 128. And it should start off being initialised with zeros.

Each time you read a character from the file, you use the character as the index into the array and increment that number.

When you've read all data from the file, you can print the array content, that gives you the frequency of the character. As you're not interested in non-printable characters, you can start printing from SPACE, which is 32 (instead of starting from zero).
what does that mean instantiate an object in c++?
what the difference between a method and a function? Do they have the same syntax?
To instantiate an object to to create an instance of a class.

"Method" is other languages' word for C++'s "Function". In C++ there is no such thing as a Method ;)
Is 'object' of a class is the same as 'instance' of the class?
Yes. An object is an instance of a class, an instance of a class is an object.
Last edited on
Thank you LB.

"Method" is other languages' word for C++'s "Function". In C++ there is no such thing as a Method ;)


Most people I know use "function" and "method" to distinguish between free and member functions, respectively.
Ah, I've always used the term "Member Function" for Functions that are Members of a class. Sorry for the confusion.
Pages: 12