Read string

Hi guys: Can any one please tell me how to read string from sequential file and than assign string to text line variable

#include<iostream>
#include<fstream>
using namespace test;
int main()
{
    ifstream outFeb;
    outFeb.open("febsales.txt", ios::in);
    if (outFeb.is_open())
      {
       while (!outFeb.eof())
         {
//        getline(outFeb);
          }
       }
     else
        {
           cout << "File could not be open" << endl;
        }
    outFeb.close();
    system("pause");
    return 0;
}
mustish1Asked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
Is this what you're after?
#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace test;

int main()
{
        ifstream outFeb;
        outFeb.open("febsales.txt", ios::in);

        if (outFeb.is_open())
        {
                string line;

                while (!outFeb.eof())
                {
                        getline(outFeb, line);

                        cout << "Line read was: " << line << endl;
                }
        }
        else
        {
                cout << "File could not be open" << endl;
        }

        outFeb.close();
        system("pause");

        return 0;
}

Open in new window

0
 
ulfurConnect With a Mentor Commented:
Are you asking how to read the content of a text file in C#?
Here's the long version that allows more control:
string content;
using (FileStream fileStream = new FileStream("febsales.txt", FileMode.Open, FileAccess.Read, FileShare.Read))
{
	using (StreamReader reader = new StreamReader(fileStream))
	content = reader.ReadToEnd();
}

Open in new window


And the short version:
string content;
using (StreamReader reader = new StreamReader("febsales.txt")) content = reader.ReadToEnd();

Open in new window


I hope this helps.
0
 
mustish1Author Commented:
Im sorry I have PIII laptop Im using the C++
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Carlos VillegasConnect With a Mentor Full Stack .NET DeveloperCommented:
string myText = System.IO.File.ReadAllText("C:\\febsales.txt");

Open in new window

0
 
mustish1Author Commented:
Error on line:
     string myText = System.IO.File.ReadAllText("C:\\febsales.txt");

Compiler: Default compiler
Executing  g++.exe...
g++.exe "C:\Users\Kathleen\Documents\test.cpp" -o "C:\Users\Kathleen\Documents\test.exe"    -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib"
C:\Users\Kathleen\Documents\test.cpp: In function `int main()':
C:\Users\Kathleen\Documents\test.cpp:11: error: `System' undeclared (first use this function)

C:\Users\Kathleen\Documents\test.cpp:11: error: (Each undeclared identifier is reported only once for each function it appears in.)

Execution terminated
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
C++? May be... Im not sure:

System::String *_myText;
_myText = System::IO::File::ReadAllText("C:\\febsales.txt");

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hey, you must include in your file:
#using <mscorlib.dll>

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
I like this page:
Managed C++
0
 
mustish1Author Commented:
Thanks a lot.
0
 
käµfm³d 👽Commented:
NP. Glad to help  : )
0
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.

All Courses

From novice to tech pro — start learning today.