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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

waysideCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

rstaveleyCommented:
0
rstaveleyCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.