Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

readig a .txt file

Posted on 2004-11-02
9
Medium Priority
?
273 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
Comment
Question by:HanuSoftware
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
9 Comments
 
LVL 14

Accepted Solution

by:
wayside earned 500 total points
ID: 12472525
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
ID: 12472672
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
ID: 12472706
Wow, I must be asleep this morning, that's C#, not C++.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 17

Expert Comment

by:rstaveley
ID: 12472908
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 500 total points
ID: 12473078
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
ID: 12694868
Points to rstaveley, but I would say that wouldn't I? ;-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

610 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