importing numbers 1 by 1 in a 2d array

Hi all,

I have a working code here for importing numbers from a .txt file, one by one into a 2d array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <windows.h> 
using namespace std;

int map_array[25][25];

int main(int argc, char **argv)
{

   cout << "starting program" << endl;
   int i=0, j=0;
   string exstring;
   char character=NULL;

   //Opening file for data reception
   fstream map_file("C:\\map_file.txt");

   //Parsing data
     while(!map_file.eof()){
	      map_file >> character;
		  
		  if (character !=','){			 
			  exstring=character; //convert char to string
			  map_array[i][j]=atoi(exstring.c_str()); //convert string to int and input in array		   		  
		  }
		  else {i++; j=0;}

		  cout << "array" << i << j << "is" << map_array[i][j] << endl;
		  Sleep(200);
		  j++;
   }
   Sleep(1000);
}


+ a .txt file as in C:\\map_file.txt

Random number value such as
651654168448, on the first line
61516, one the second, etc...

There is a small problem however, at the end of every line in the txt file, I have a 0 inserted in my array....I don't know how to get rid of this 0.

Can anyone help ?

On a side note, If I want to include characters as well (a, b, c...) is there a minor tweak I can do for the code considering that I have currently an array of int ?

Many thanks,

Wissam
Last edited on
Topic archived. No new replies allowed.