Menu

File reading and writing question

2003-02-07
2012-09-26
  • Nobody/Anonymous

    Hi,

    I want to read in a text file and place it in an edit box. I know how to use the WINAPI to do this but I would like to use C++ streams.

    eg. ifstream fin;

    fin.open ( filename, ios::beg );

    I then want to place the entire text file into 1 variable so that I can place it into the edit box using SetWindowText

    Is this possible. I usually use getline ( fin, string ) to retrieve one line. Is there any way to use a char * or something to get the entire text file into a LPCTSTR.

    Thanks a lot

     
    • Nobody/Anonymous

      Hi,

      I almost have the solution :

      #include <fstream.h>
      #include <iostream>

      int main()
      {
          ifstream fin("c:\\windows\\desktop\\test.txt");

          fin.seekg(0,ios::end);
          int size = fin.tellg();
          fin.seekg(NULL,ios::beg);

          char *buffer = new char[size];

          fin.read(buffer,size);

          std::cout << size << std::endl;

          std::cout << buffer << std::endl;

          std::cout << std::endl;

          delete[] buffer;

          return 0;
      }

      The only problem here is that the fin.tellg() function returns an invalid amount if there is more than one line eg. if you type :

      G
      R
      E

      A
      T

      It gives you 15 characters - can anyone help?

       
    • Curtis Sutter

      Curtis Sutter - 2003-02-08

      If you are doing WINAPI, why not use a resource file?

      eg.
      // something.rc  included into project
      LEVELS TEXT DISCARDABLE "ResLevels.txt"

      // Useage in main
      HGLOBAL textlevels;
      char *file;

      textlevels = LoadResource (_glInstance, FindResource(_glInstance, TEXT("LEVELS"), TEXT("TEXT")));

      file = (char*)LockResource(textlevels);

      // Don't forget this at the end
      FreeResource(textlevels);

      // This is actual *working* code from a game of mine.

      Curtis

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.