Thread: File I/O problem

  1. #1
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227

    File I/O problem

    why will this write the text to a file:
    Code:
      ofstream fout("info.txt");
      char ch;
      char word[BUFSIZ];
      int i = 0;
    
      while (1)
      {
          ch = getch();
          word[i++] = ch;
          fout << word;
      }    
      fout.close();
    and this do absolutely nothing


    Code:
      ofstream fout("info.txt");
      char ch;
    
      while (1)
      {
          ch = getch();
          fout << ch;
      }    
      fout.close();
    Keyboard Not Found! Press any key to continue. . .

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Good news! Neither work for me.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> fout << word;
    word is interpreted as a c-string, and must be null-terminated.

    >> while (1)
    Don't expect the file to have anything in it since fout.close() is never reached.

    >> getch();
    Mix'n C and C++ I/O.

    gg

  4. #4
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    ok thanks codeplug ill try to make it so both work

    but i still dont understand why one worked and the other didnt
    Keyboard Not Found! Press any key to continue. . .

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> dont understand why one worked and the other didnt
    I wouldn't say it "worked". You're just observing the results of running code with undefined behavior.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File i/o problem
    By tezcatlipooca in forum C++ Programming
    Replies: 18
    Last Post: 01-01-2007, 09:01 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 04:32 PM
  3. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM