readig a .txt file

how to read .txt file that is in a given drive  (for example the file path is "c:\raj.txt") in VC++.net language.
Please give me the exact code that could run in VC++.net Environment.
HanuSoftwareAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
waysideConnect With a Mentor Commented:
Here's one way:

#include <stdio.h>

int main( void )
{
   FILE *stream;
   char list[30];
   int  i, numread, numwritten;

   if( (stream = fopen( "c:\\raj.txt", "r" )) != NULL )
   {
      /* Attempt to read in 25 characters */
      numread = fread( list, sizeof( char ), 25, stream );
      printf( "Number of items read = %d\n", numread );
      printf( "Contents of buffer = %.25s\n", list );
      fclose( stream );
   }
   else
      printf( "File could not be opened\n" );
}
0
 
waysideCommented:
Or did you want a managed code solution?

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("c:\\raj.txt"))
            {
                String line;
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}
0
 
waysideCommented:
Wow, I must be asleep this morning, that's C#, not C++.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
rstaveleyCommented:
0
 
rstaveleyConnect With a Mentor Commented:
If you want to do exception handling, try this:
--------8<--------
// Adapted from http://support.microsoft.com/kb/307384/EN-US/
// Compile with /Zi /clr

#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
using namespace System::Collections;

#ifdef _UNICODE
int wmain(void)
#else
int main(void)
#endif
try {
      StreamReader* objReader = new StreamReader(S"c:\\nonexistentdirectory\\reader_dot_net.cpp");
      String *sLine = "";
      ArrayList *arrText = new ArrayList();

      sLine = objReader->ReadLine();
      while (sLine != 0) {
            arrText->Add(sLine);
            sLine = objReader->ReadLine();
      }
      objReader->Close();

      // Write out the ArrayList to the console.
      for(int i = 0; i<arrText->Count; i++)
            Console::WriteLine(arrText->Item[i]->ToString());
      //Console::ReadLine();
      return 0;
}
catch (Exception* e) {
      Console::WriteLine(S"{0}: {1}",e->GetType()->ToString(),e->Message);
      //Console::WriteLine(S"Exception {0}: {1}\n{2}",e->GetType()->ToString(),e->Message,e->StackTrace);
}
--------8<--------
0
 
rstaveleyCommented:
Points to rstaveley, but I would say that wouldn't I? ;-)
0
All Courses

From novice to tech pro — start learning today.