Hi, I need some help writing a text file into an array. I've got the text file opening and displaying but its at the bottom of the page instead of where the array is. Would it work if I just set the position to (0,0)? Heres the code for the text file:

int textfile(int bd[N][N])
{
    ifstream insert;
    ofstream output;
 
    string lineread; 
 
    insert.open("test.txt");
 
    if(insert.fail())
    {
        cout<<"File could not be opened"<<endl;
        }
 
        //while(!insert.eof())
        {
			
          getline(insert,lineread); 
          cout<<lineread<<endl;
          }
 
          
  system("PAUSE");
    return (0);
}

its at the bottom of the page instead of where the array is

I'm not exactly sure what you mean by that, can you show a sample output?

I mean when the text is displayed, its below the array it looks like

000  000  000
000  000  000
000  000  000

Text is displayed here

Where are you printing out the array?

I'm very confused, because the code you pasted above reads in a line and immediately prints it out. Is that the text to which you are referring or another line of text?

You won't be able to select where the text goes unless you are able to use a console library.

Thats not the full code, thats just the text I need to write into the array. The array is initialised to all 0s. And yeah I'm using a console. Basically I'm just not sure how to go about getting it in the right position.

Hmm. Apologies that I'm not quite getting this. Are you trying to write numbers that are in the lines of text into the "bd" array? If so, you have to extract them somehow. What does text.txt look like?

Ha, no problem I'm just not explaining it properly. Yeah the text file has numbers from 0-9 that I want to put in the array so its not just all 0s, I'm not sure if I did the text file correctly, its just the numbers layedout to look like the array. I've got the text file displayng under the grid, but I need the numbers of the text file to replace the curent numbers in the array.

If you want, I can post a screenshot.

Eliminate line 19 from your code.

If you use the >> operator, you can read in the numbers to an int variable. Is it guaranteed that your text file will contain NxN numbers. A nested for loop will help you to read them in, and it will look like insert >> (the integer variable).

Yeah its always going to be 9x9. So would it be something like:

int a 

  for (i=0; i<9; i++)
	{
         for (j=0; j<9; j++)

insert >> a

How would I make the a equal to the for loop though?

Substitute bd[i][j] for a

Hmm, still not working. It just gives me the "press any key to continue" line but my array is still zeroed. The code I have is

int textfile(int bd[N][N])
{
	 ifstream insert;
    ofstream output;
 
    string lineread; //line read  from the text file

 
    insert.open("test.txt");
 
    if(insert.fail())
    {
        cout<<"File couldn't open"<<endl;
        }
 
        while(!insert.eof())
        {
			
          getline(insert,lineread); 
          //cout<<lineread<<endl; //test to see if it prints the right text file
		  int i,j;
		  int bd[N][N];
 
			for (i=0; i<9; i++)
			{
				 for (j=0; j<9; j++)
 
				insert >> bd[N][N];
			}


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

I used [N][N] since it needed a const

How do you know it's not working, you didn't print out anything!

With the extraction operator >> you don't need to read in the whole line, as if that were the case, you'd have to parse out all of the integers. Get rid of lines 16-22, you'd only need those if you were reading in line by line, but since you know how many elements there are, the for loop is taking care of it for you. You don't need to redeclare bd, as it's being passed in from whatever parent function you are calling this function from.

Keep 24-29, but it needs some changes. Your array is 3x3, correct? Check your loop indexes, trace through the loops on paper and see which elements are being read. No matter what N is, bd[N][N] is not an element of that array anyhow, you're one past the end in both dimensions.

No, the array is 9x9 and N=9. I dont really understand what you mean though, and how would I go about printing it out? Sorry for being so bad at this, really grateful for all your help.

If N=9 in your prior example, each pass you'd be writing into bd[9][9], right? Well, the array only goes up to bd[8][8]. When you declare an array, you give it's dimensions, but when you're accessing the elements you have to start numbering at 0.

In your loop, to access the (ith,jth) element, just use bd[j], which will work for input and when you're printing the values out. So within your for loops:

insert >> bd[i][j];
cout<<bd[i][j];  //add in your own spaces and newlines for formatting purposes

Woo! Thanks so much, works perfectly. I am eternally grateful :)

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.