0

I wrote a function to read a text file, create an array from the integer values in the file and return the reference of that array to main function. The code I wrote(in VS2010):

//main.cpp
void main(){
int T_FileX1[1000]; 
    int *ptr=readFile("x1.txt");

    for(int counter=0; counter<1000; counter++)
        cout<<*(ptr+counter)<<endl;    
}

and the function is:

//mylib.h 
int* readFile(string fileName){
    int index=0;
            ifstream indata;
            int num;

    int T[1000];    
    indata.open("fileName");
            if(!indata){
                cerr<<"Error: file could not be opened"<<endl;
                exit(1);
            }
            indata>>num;
            while ( !indata.eof() ) { // keep reading until end-of-file
                T[index]=num;       
                indata >> num; // sets EOF flag if no value found
                index++;
            }
            indata.close();

            int *pointer;
            pointer=&T[0];
            return pointer;
}

the data in the file contains positive numbers like

5160
11295
472
5385
7140

When I write each value in "readFile(string)" function, it writes true. But when I wrote it to screen as U wrote in "main" function, it gives values strangely:

0
2180860
1417566215
2180868
-125634075
2180952
1417567254
1418194248
32   
2180736

irrelevant to my data. I have 1000 numbers in my file and I guess it raves these irrelevant values after a part of true writing. E.g. it writes first 500 values true, and then it writes irrelevant values to my data. Where is my fault?

JoshuaJeanThree
  • 1,382
  • 2
  • 22
  • 41
  • You're reading garbage into num. It seems that what you want is to parse the strings representing numbers and convert them to integers. – Diego Basch Dec 10 '12 at 06:53
  • 1
    @DiegoBasch.... wrong! The ifstream `operator >>` automatically parses the data according to destination data type. – sgarizvi Dec 10 '12 at 07:01
  • `while ( !indata.eof() )` is incorrect. read more about the "EOF anti-pattern" here: http://stackoverflow.com/questions/5431941 and http://drpaulcarter.com/cs/common-c-errors.php#4.2 – Michael Burr Dec 10 '12 at 07:05

3 Answers3

3
int T[1000]; 
...
pointer=&T[0];

you are returning a pointer to a local stack variable which is going to get destructed.

I think what you want to do is to pass in the array T_FileX1 that you have defined to the function and use that directly to read the data into.

Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • It doesn't crash because he's only reading stack memory and dumping it to `cout` as numbers. So there's no reason to crash, but the information on the stack is meaningless. – Michael Burr Dec 10 '12 at 07:02
  • @MichaelBurr ah yes that makes sense, only a write would trigger a crash right? – Karthik T Dec 10 '12 at 07:03
  • Reading may cause a crash as well if you're using the information in some other way (such as a pointer or an index). – Michael Burr Dec 10 '12 at 07:09
1

You return a pointer to the first element of an array which is allocated on the stack and gets destroyed after your function returns. Try using a vector instead:

vector<int> readFile(string fileName) {
    ifstream indata;
    int num;

    vector<int> T;
    indata.open("fileName");
    if(!indata){
        cerr<<"Error: file could not be opened"<<endl;
        exit(1);
    }
    indata>>num;
    while ( !indata.eof() ) { // keep reading until end-of-file
        T.push_back(num);
        indata >> num; // sets EOF flag if no value found
    }
    indata.close();

    return T;
}
prazuber
  • 1,352
  • 10
  • 26
0

This is a case of undefined behavior. You return a pointer to a local variable, when the function returns the part of the stack used by the function is no longer valid.

Pass the array as an argument to the function instead.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621