Thread: File I/O

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    i was thinking of doing something like that but i was hoping there was an easier/faster way... well whatever works.
    thx

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    ok im at my computer now and it works better now but it doesnt strore the white spaces(ie. " "). any other ideas cause im stuped

  3. #18
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include <iostream>
     
    void render(char [2][2], int , int);
     
    int main()
    {
      char ch = 'X';
      char temp;
      char cArray[2][2];
      int i, j;
      for(i = 0; i < 2; ++i)
       for(j = 0; j < 2; ++j)
    	cArray[i][j] = ch;
     
     //render original array
     for(i = 0; i < 2; ++i)
     {
      for(j = 0; j < 2; ++j)
      {
    	cout << cArray[i][j];
      }
      cout << endl;
     }
     
     cin >> temp;
     
     //redraw array putting space in each of 
     // the four elements of the array, one at a time
     for(i = 0; i < 2; ++i)
     {
       for(j = 0; j < 2; ++j)
       {
    	 render(cArrray, i, j);
    	 cout << "enter any key to render a new picture" << endl;
    	 cin >> temp;
      }
    }
     
     cin >> temp;
     return 0;
    }
     
    void render(char cArray[2][2], int a, int b)
    {
     int k, L;
     char temp;
     
     temp = cArray[a][b];
     cArray[a][b] = ' ';
     
     for(k = 0; k < 2; ++k)
     {
       for(L = 0; L < 2; ++L)
       {
    	 cout << cArray[k][L];
       }
       cout << endl;
     }
     
     cout << endl;
     
     cArray[a][b] = temp;
    }
    Last edited by elad; 10-04-2004 at 08:34 PM.

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    i kindof understand what this does and how it works...
    it makes an array
    XX
    XX

    and on input it puts a space were one of the x's are but my problem is i dont know were the spaces are going to be. ill have a file say like this
    Code:
    +-----------------------+
    |000                    |
    |0I0                    |
    | *0  ***********       |
    | *****         ***** 00|
    | *       000000    ****|
    | **      0W00A0      00|
    |  ************0        |
    |TT                     |
    |TTT TTTTTTT  TTT       |
    |TT  TTTT  TT TTTT      |
    |TTT TTTTTT  TTTT       |
    |    TTTT  TT TTT       |
    |MTTTTTTTTT  TTTTT      |
    +-----------------------+
    and on output it looks like this
    Code:
    +-----------------------+
    00||0I0||*0***********||*
    *******00||*000000****||*
    W00A000||************0||T
    |TTTTTTTTTTTTT||TTTTTTTTT
    T||TTTTTTTTTTTTT||TTTTTTT
    ||MTTTTTTTTTTTTTT|+------
    ---------------+
    the code you posted doesnt check to see what is supposed to be there and i dont know how to do that because during fin >> szMap it doesnt read the space.

    sorry for taking up so much time but i am stumped

  5. #20
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Instead of using >> to read the file one char at a time try using getline(). >> will ignore whitespace, getline() won't.
    Code:
    ofstream fout("myFile.txt");
    //read line to file that contains spaces
    fout << "|	 f	 |\n" << endl;
    fout << "|**** **|\n" << endl;
     
    ifstream fin("myFile.txt");
    char str[80];
    int i, y;
    int length;
    int x = -1;
    //read file one line at a time--maintain spaces
    while(fin.getline(str, 80, '\n'))
    {
      //set counters for each new line read in
      i = 0;
      x++;
      y = 0;
      length = strlen(str);
     
      //extract each non-null char from line
      while (i < length)
      {
    	myMap[x][y++] = str[i];
    	//display each char of each line to screen
    	cout << str[i++];
      }
     
      //add back the newline char to get proper display on screen
      cout << endl;
    }
    If using STL strings rather than C style strings then syntax for getline is different.

    The new line char is the default value of the third parameter for getline() so you could use
    fin.getline(str, 80); if you want to.
    Last edited by elad; 10-07-2004 at 11:07 AM.

  6. #21
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    thx alot for your help elad. ill try this today when i get home.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM