• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

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;
}
0
mustish1
Asked:
mustish1
  • 4
  • 3
  • 2
  • +1
3 Solutions
 
ulfurCommented:
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
 
Carlos VillegasFull Stack .NET DeveloperCommented:
string myText = System.IO.File.ReadAllText("C:\\febsales.txt");

Open in new window

0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
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
 
käµfm³d 👽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
 
mustish1Author Commented:
Thanks a lot.
0
 
käµfm³d 👽Commented:
NP. Glad to help  : )
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now