File I/O problem

This is a discussion on File I/O problem within the C++ Programming forums, part of the General Programming Boards category; why will this write the text to a file: Code: ofstream fout("info.txt"); char ch; char word[BUFSIZ]; int i = 0; ...

  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,662
    Good news! Neither work for me.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    >> 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,662
    >> 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, 08:01 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 03: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, 01:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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