Solved

readig a .txt file

Posted on 2004-11-02
229 Views
Last Modified: 2010-04-01
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.
0
Question by:HanuSoftware
    6 Comments
     
    LVL 14

    Accepted Solution

    by:
    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
     
    LVL 14

    Expert Comment

    by:wayside
    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
     
    LVL 14

    Expert Comment

    by:wayside
    Wow, I must be asleep this morning, that's C#, not C++.
    0
     
    LVL 17

    Expert Comment

    by:rstaveley
    0
     
    LVL 17

    Assisted Solution

    by:rstaveley
    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
     
    LVL 17

    Expert Comment

    by:rstaveley
    Points to rstaveley, but I would say that wouldn't I? ;-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now