reading and writing array to txt file

hello
i have a project
i need to read array from txt file and rotate the array and after this write the new array to new txt file. the arrays size is [600][800] and i have a problem when i try to read big array.

i have to read the original array to new array in my progrram and rotate my program array,and write this program array to new file. when i try to compile my program the cmd show me a massage "stack overflow". what wrong with my program?

in addition, when i try to write small arrray, the program compile my code and create new file with my array,but instead writing numbers, the program write kind of circles.

please help me.
thank you.
Maybe making the array static might help(to store it on the heap).
But showing your code will make "us" help you better.

Thanks,
Aceix.
hi,

thank you for the quick answer.
please look at the next link:

http://www.pastebin.com/kxfg5MHr

best regards
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    // Arrays.cpp : Defines the entry point for the console application.
    //
     
    #include <iostream>
    #include <string>
    #include <fstream>
     
    using namespace std;
    void ReadFromFile(char* FileName)
    {
            ifstream file(FileName);
            char Header[256], CurrentPixel[4], a[10], b[10];
            file>>a;
            file>>a;
            file>>b;
            int NumOfRows = atoi(b);
            int NumOfCols = atoi(a);
            int pic[500000];
            for (int i=0 ;i<NumOfRows;i++) //i=ROWS
            {
                    for (int j=0 ;j<NumOfCols ; j++)//j=COLS
                    {
                            file>>CurrentPixel;
                            pic[i * NumOfCols + j] = atoi(CurrentPixel);
                    }
            }
           
            file.close();
    }
    /*     
    void WriteToFile(char* FileName)
    {
            ofstream file(FileName);
           
            file << "Header: " << Rows << " " << Columns << endl;
     
            for (int iRowIndex = 0 ; iRowIndex < Rows ; ++iRowIndex)
            {
                    for (int iColIndex = 0 ; iColIndex < Columns ; ++iColIndex)
                    {
                            file << Pic[iRowIndex * Columns + iColIndex] << " ";
                    }
                    file << endl;
            }
           
            file.close();
    }
    }*/
    void main()
    {
            ReadFromFile("t3.pgm");
    //      WriteToFile("t3LAST.pgm");
    }


You should not use the extraction operator when working with C-strings, unless you specify the maximum number of characters to retrieve with a call to setw(). And don't forget that C-strings must have room for the end of string character ('\0').

file >> setw(4) >> CurrentPixel;
I would recommend using a const to specify the size of your array and use this const in the setw() call. Don't forget to include <iomanip> to use the manipulator setw().

And I recommend using Dynamic memory for the following:
int pic[500000];
As that array is probably too big to fit in stack memory.

Last edited on
hi,

i tried to make it simple, just to see if it only works.
i try to work with static array first.
this is my new code:
http://pastebin.com/3Xci2tLb

i get red mark under line 43 - [i] (Expression must have pointer to object type)

please advise.

regards
In the following snippet:
1
2
3
4
void WriteToFile(char* FileName,int& pic,int& NumOfRows, int& NumOfCols )
{
 ...
                file<<pic[i][j];


You are passing a variable named pic into this function, this parameter is not an array it is a pointer to an int. Since you made the array global (not static) you don't need to pass it into the function.
Topic archived. No new replies allowed.