Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8978817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:41:59+00:00 2026-06-15T19:41:59+00:00

I wrote a function to read a text file, create an array from the

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:42:00+00:00Added an answer on June 15, 2026 at 7:42 pm
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a function to read Morse code from one file, convert
I would like to write a function which will read values from a text
I write this function, but what's its return value. (defn read-data [file] (let [code
I'm going to read some function from a Unicode text file in matlab and
I want to write a function that read line by line from a socket
I am trying to read all content from a text file. Here is the
I am writing some code that downloads a file from a network resource to
I just wrote this function to read a series of email addresses from a
I have a simple file read and write function. private void WriteToFile(String filename, String
I'm trying to write a short function that will let me quickly read in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.