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.