reading a string from a text file into a 2d array

This is a discussion on reading a string from a text file into a 2d array within the C++ Programming forums, part of the General Programming Boards category; Hey fellow programmers i programmed a connect 4 game in c++ using an array for the field. I have a ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    reading a string from a text file into a 2d array

    Hey fellow programmers

    i programmed a connect 4 game in c++ using an array for the field.
    I have a function that saves after the input of a player the whole array in a text document

    I can also succesfuly read the file, but i am having problems converting string to int. (i used atoi and other stuff but in didn't work.)
    So the programm has to read the content of the file and put every number to 1 field in the 2 dimensional array.

    Spielstand= save state
    sZahlen = string numbers



    code to write:
    Code:
    void  vSpielstandSpeichern(int iFeld[GR_ARRAY][GR_ARRAY]) 
    		{
    		ofstream outfile;
            outfile.open ("viergewinntSpielstand.txt");
    		for (int x = 1; x < GR_ARRAY; x++){
                     for (int y = 1; y < GR_ARRAY; y++){
                     outfile<<iFeld[x][y]<<endl;
    				 
                     }  // end for
    				 
            }  // end for
    		outfile.close();
    		
    		
    		}
    here is the code to read:
    Code:
    void  vSpielstandLaden(int iFeld[GR_ARRAY][GR_ARRAY])
    		{
    	// Define constants.
    int iZahl=0;
       // Open input file.
    
    	ifstream Spielstand;  // Declare an input file object.
    
    	Spielstand.open("viergewinntSpielstand.txt", ios::in);  // Open the input file in project folder.
    	if (!Spielstand) // See if the file was opened sucessfully.
    	{ 
    		cout << "file can not be read!\n";
    		
    	}
    
       cout << "Spielstand wird gelesen...\n";
       
    
       string sZahlen;
    
       getline(Spielstand, sZahlen);
       while (!Spielstand.fail())
       {
    for (int x = 1; x < GR_ARRAY; x++){
                     for (int y = 1; y < GR_ARRAY; y++){     
    	  
    	  iZahl= atoi( sZahlen);
    	  iFeld[x][y]= iZahl;
          getline(Spielstand, sZahlen);
    
    				 }}
       }
       if (Spielstand.fail() && !Spielstand.eof())
       {
          cout << "Error while reading file. Aborting!\n";
          Spielstand.close();
         
       }
    i am very gratefull for any help'

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> iZahl= atoi( sZahlen);

    Are you getting compile errors? That line probably won't work unless you call c_str() on sZahlen:
    Code:
    iZahl= atoi( sZahlen.c_str() );
    Another option is to just read into the iZahl variable using a stringstream.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    2
    yeah compile errors

    Code:
    Error	1	error C2664: 'atoi' : cannot convert parameter 1 from 'std::string' to 'const char *'	c:\Users\kittyc4t\Documents\Visual Studio 2008\Projects\MK_connect4 - working - nicht animiert\connect4\c4.cpp	194	connect4

    thanks i think i found what i need i will try using sstreams


    :: with .c_str() i get no compile errors, have to check if it also works thanks alot ;D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading from a 2d array
    By roaan in forum C Programming
    Replies: 5
    Last Post: 09-04-2009, 12:42 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21