I'm having a ton of trouble reading the text from a file into an integer array, the file contains 3 lines of 1,000 integers from 0 to 1. The array is myArray[3][1000]. So far I've tried to use a stringstream to no avail and I've attempted to use a few other methods based on examples from the net but none of them seem to work, they tend to wind up reading all 0's. Please be gentle, I've only been working in C++ for 6 days now, and prior to that I hadn't programmed for about 7 years (and that was in assembly and VB).

Edit: myArray is global, this is just one of the functions within the program

Current attempt:

void readArray() 
{ 

int amountRead = 0;

ifstream in("output.txt",ios::in);
	while(in>>myArray[0][amountRead]&& amountRead < 1000)
	{

	amountRead++;
	
	}
	amountRead = 0;
	while(in>>myArray[1][amountRead ]&& amountRead < 1000)
	{

	amountRead++;

	}
	amountRead = 0;
	while(in>>myArray[2][amountRead ]&& amountRead < 1000)
	{

	amountRead++;

	}
}

I'm having a ton of trouble reading the text from a file into an integer array, the file contains 3 lines of 1,000 integers from 0 to 1.

I don't suppose you could attach the input file? And what do you mean by "integers from 0 to 1"? Just a bunch of ones and zeros? Or floating point values?

Edit: myArray is global, this is just one of the functions within the program

Show it's declaration.

The input file is random 1's and 0's, I've attached to code to the entire program in case this will help.

#include "stdafx.h"
#include <iostream>
#include <fstream>	
#include <time.h>
#include <string>
#include <sstream>
using namespace std;

int myArray[3][1000], flag = 1;


void generateArray()
{
int i;
srand( time(NULL) );

for(int j=0;j<3;j++)
{
for (i = 0; i < 1000; i++)	myArray[j][i] = rand() % 2;
}

cout << "Array has been generated" << endl;

}

	void saveArray()
	{
		ofstream output("output.txt");

		for(int j=0;j<3;j++)
		{
			for(int i=0;i<1000;i++) output<<myArray[j][i];
		}

		output.close();
	}


void readArray() 
{ 

int amountRead = 0;

ifstream in("output.txt",ios::in);
	while(in>>myArray[0][amountRead]&& amountRead < 1000)
	{

	amountRead++;
	
	}
	amountRead = 0;
	while(in>>myArray[1][amountRead ]&& amountRead < 1000)
	{

	amountRead++;

	}
	amountRead = 0;
	while(in>>myArray[2][amountRead ]&& amountRead < 1000)
	{

	amountRead++;

	}
}

void genAppendArray()
{
	generateArray();

	ofstream output;
	output.open("output.txt", ios::out |ios::app);
	
output<<endl;

	
for(int j=0;j<3;j++)
{
	for(int i=0;i<1000;i++) output<<myArray[j	][i];
}

	output.close();

}

void showMenu()
{
int choice;

cout << "Choose an option:" << endl << "1. Generate a random 3x1000 array of entries from 0-1." << endl 
	<< "2. Save array to a file" << endl << "3. Read array from a file." << endl << "4. Generate array and append it to a file."
	<< endl << "5. Quit." << endl << endl;	
	
	cin >> choice;

switch (choice) 
{
case 1:
generateArray();
break;

case 2:
saveArray();
break;

case 3:
readArray();
break;

case 4:
genAppendArray();
break;

case 5:
flag = 0;
break;
}
}

int main () 
{
	while (flag > 0)
	{
	cout << endl; 
	showMenu ();
	}
	return 0;
}

Consider separating the 1's and 0's with whitespace.
Otherwise, you may need to read each character as a char and convert to its equivalent digit.

Consider separating the 1's and 0's with whitespace.
Otherwise, you may need to read each character as a char and convert to its equivalent digit.

the file contains 3 lines of 1,000 integers. That will be a waste of time
if you want to separate each int with white spaces.

just read it as a char, like he suggested. Then subtract '0' from it
to get the int value. Something like this :

char c;
ifstream iFile("num.txt");
int i = 0;
while(iFile.get(c) && i < 1000)
{
     int num =  c - '0';
    myArray[0][i] = num;
     i++;
}

It works! I'm using:

void readArray() 
{ 

char c;
ifstream iFile("output.txt");
int i = 0;
while(iFile.get(c) && i < 1000)
{
     int num =  c - '0';
    myArray[0][i] = num;
     i++;
}
i = 0;
while(iFile.get(c) && i < 1000)
{
     int num =  c - '0';
    myArray[1][i] = num;
     i++;
}
i = 0;
while(iFile.get(c) && i < 1000)
{
     int num =  c - '0';
    myArray[2][i] = num;
     i++;
}

Thanks for all your help, I greatly appreciate it.

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.