Answered Help me to read .txt file

  • Thursday, April 26, 2012 2:01 PM
     
      Has Code
    Hello.
    std::string path = "c:\\test.txt";
    std::string readText = File.ReadAllText(path);

    I get error "File:undeclared idintifier"



    • Edited by Demaunt Thursday, April 26, 2012 2:11 PM
    •  

All Replies

  • Thursday, April 26, 2012 2:36 PM
     
     

    If you are doing this in C++, then that is understandable since the File class is only from C++/CLI (otherwise known as managed C++).

    The standard way of reading files in C++ is using fstream.

    But you seem to have gotten confused between standard C++ and the Microsoft C++/CLI extension. I would suggest you pick up a good book on C++ to learn it, this is one of those cases where just randomly following guides on the internet makes learning it harder.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

  • Thursday, April 26, 2012 2:42 PM
     
      Has Code
    Hello.
    std::string path = "c:\\test.txt";
    std::string readText = File.ReadAllText(path);

    I get error "File:undeclared idintifier"



    Yes because i can't see File is declared anywhere doesn't matter what language you are using but atleast you need some kind of object to access the class functionality . Write in a file

    #include <iostream>
    #include <fstream>
    using namespace std;
    int main () {
      ofstream myfile;
      myfile.open ("c:\\test.txt");
      myfile << "Writing this to a file.\n";
      myfile.close();
      return 0;
    }

    // reading a text file

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main () {
      string line;
      ifstream myfile ("example.txt");
      if (myfile.is_open())
      {
        while ( myfile.good() )
        {
          getline (myfile,line);
          cout << line << endl;
        }
        myfile.close();
      }
      else cout << "Unable to open file"; 
      return 0;
    }

    Thanks


    Rupesh Shukla




  • Thursday, April 26, 2012 2:54 PM
     
      Has Code

    FILE *fp = fopen("c:\\test.txt", "r");               
    std::string readText = fp.ReadAllText();
    I get error.
    .ReadallText must have class struct union
    • Edited by Demaunt Thursday, April 26, 2012 2:55 PM
    •  
  • Thursday, April 26, 2012 3:00 PM
     
      Has Code

    FILE *fp = fopen("c:\\test.txt", "r");               
    std::string readText = fp.ReadAllText();
    I get error.
    .ReadallText must have class struct union

    What are you doing . Now you are back to C File handling . I had never heard about ReadAlltext() in case of C File handling. You are mixing everything C,C++,C# pick one of them and do whatever you want.

    Thanks


    Rupesh Shukla


  • Thursday, April 26, 2012 3:08 PM
     
     

    You are still mixing apples and oranges. First decide which language you want to program in. If you wish to use C or C++ then do not call .NET functions. 

    fopen is a C library function. It knows nothing about ReadAllText.

    ReadAllText is a .NET library function. It knows nothiing about FILE*

  • Thursday, April 26, 2012 3:11 PM
     
      Has Code
    Ok.
    I dont get File.ReadAllText stuff.
    Is it possible to use it (VS 2010, WFA) ?

    Another variant.
    FILE *fp = fopen("c:\\test.txt", "r");
    char chr [2000] = "";
    while(!feof(fp))
    {
      fgets (chr, 2000, fp)
    }


    So I get char with elements like "any number" + " " +some garbage
    Can I use atoi or scanf to convert non-space and non-garbage elements to of integer massive?

    http://msdn.microsoft.com/en-us/library/ms143368.aspx as you can see here, examples dont declare files.
    • Edited by Demaunt Thursday, April 26, 2012 3:16 PM
    •  
  • Thursday, April 26, 2012 3:27 PM
     
      Has Code
    Ok.
    I dont get File.ReadAllText stuff.
    Is it possible to use it (VS 2010, WFA) ?

    Another variant.
    FILE *fp = fopen("c:\\test.txt", "r");
    char chr [2000] = "";
    while(!feof(fp))
    {
      fgets (chr, 2000, fp)
    }


    So I get char with elements like "any number" + " " +some garbage
    Can I use atoi or scanf to convert non-space and non-garbage elements to of integer massive?

    http://msdn.microsoft.com/en-us/library/ms143368.aspx as you can see here, examples dont declare files.

    I am all lost garbage and all . What actually you are trying to do here . Why not just pick a book and read it. Your only problem is you are missing everything nothing else.

    Edit :-

                      I think your actual problem is you are using wrong framework to write your code .

    Thanks


    Rupesh Shukla



  • Thursday, April 26, 2012 3:33 PM
     
     Answered Has Code

    Well, interestingly enough, in a Windows Form Application you shouldn't be using C style file access.

    For File.ReadAllText to work, first it is a static member, so it should really be File::ReadAllText, and secondly you must first put the simple line

    using namespace System::IO;

    in before you use the File class. Otherwise you will have to fully qualify it as

    System::IO::File::ReadAllText();

    Also, do not use std::string in your Window Form Application use System::String^ instead. So your very first example should be

    using namespace System;
    using namespace System::IO;
    String^ path = "C:\\test.txt";
    String^ readtext = File::ReadAllText(path);

    in a WinForms application. You can put the using namespace lines in a block near the top of the source file.

    Oh, and this is why you really should give pleanty of information about what you are doing. If you had said you were using a C++/CLI Windows Form application from the start, then you wouldn't have been lead in a weird direction.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.