Hi guys having a single problem with some code.

The program is statically linked to another which is suppose to take an input from a text file into an array; and then return a pointer to the array to the main program. The text file contains 10 ints one per line and read in line by line.

System calls and the cin >> stream seem to be the problems but have been trying for days to find a solution.

Sorry if the code isn't great been playing around with it for days. Here is the main:

#include "stdlib.h"
#include "stdafx.h"
#include "FileLib.h"
#include <iostream>
#include "amplify_dll.h"

using namespace std;

FileFunc  myFile; // = new FileFunc;

int _tmain(int argc, _TCHAR* argv[])
{
	int *pSignals;//The pointer to the signal file array.
	char fileName[280];
	int state = 0; // The programs state.
	int signals[280];
	int i;

	int	select = 0;

		//Entry point for application.
		cout <<"To begin please load a signal file."<< endl; 
		cout <<"Please enter a .txt file name to load:"<< endl;
		cout <<">";
		cin.getline(fileName, 280);
		pSignals = myFile.LoadFile(fileName);//loads file and points to array.
		state = 1;	
	
		
	if (state == 1)
	{
		cout << "Please select from the following functions:" << endl;
		cout << "1: Display signal files" << endl;
		
		cin >> select;//when this and the if statment are removed pointer is passed to display function as expected.

		if (select == 1)
		{
			myFile.Display(pSignals);
		}	


	}
		
	system ("pause");
	return 0;
}

Here is the load file function:

int * FileFunc::LoadFile(char *fileName)
{
	int *pSignals = 0;
	int arraySize = 0; // First variable; number of readings in the file.
	int i;
	int Signal[280];//The array which will store the signal files.
	
		ifstream myStream;
		myStream.open(fileName, ios::in);//opens input stream with given file name.
		
		if(!myStream){
			cout << "Unable to open file with the name " << fileName << endl;
			return (0);
		}
		else {

			myStream >> Signal[0]; // reads the first number in the file and passes on to array.
			arraySize = Signal[0];

			for(i = 1; i <= arraySize; i++){ //starts at position 1 to offset previous reading.
					
					myStream >> Signal[i];
					
			  }

				cout << "Load successful" << endl;
								
				pSignals = Signal;//points at the first integer in the array.
				
				return pSignals;

		}

}

Here is the display function, in future version need to out put the whole contents of the array but current just trying to read any value from it. :

void FileFunc::Display(int *pSignals)
{
	int i;
	int arraySize;
	cout << pSignals[3];
	cout << pSignals[5]; 

	cout << endl;
}

When the commented cin is removed the function displays the correct ints; when cin is present displays 2617860. If any other information is needed let me know.

Thanks in advance.

Recommended Answers

All 4 Replies

int * FileFunc::LoadFile(char *fileName)
{
	int *pSignals = 0;
	int Signal[280];//The array which will store the signal files.
        // Irrelevant code skipped
	pSignals = Signal;//points at the first integer in the array.
	return pSignals;
}

Signal is a local array. Once the function completes, it is gone. It is a pure (bad) luck that you ever display anything correctly.

Not sure thats the problem as the right address is passed to main its only when it hits the Cin>> select that it goes awol.

I replaced the Cin>> with system("pause") and system ("CLS") and still have the same issues but when they are removed the pointer is passed to the display class correctly.

I replaced the Cin>> with system("pause") and system ("CLS") and still have the same issues but when they are removed the pointer is passed to the display class correctly.

The int Signal[280]; being local to the FileFunc::LoadFile() function is a guaranteed source of trouble for you. You may detect correct'ish-like behaviour to some extent by rearranging your code, but as long as the array is local to FileFunc::LoadFile() , you are simply wasting your time trying to get the thing work.

Ok thanks alot. Will try declaring the array in main and passing as a parameter to the function.

EDIT: Thanks guys seems to be working now, can sleep peacefully tonight!! Much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.