Solved

Read a TXT file into an array in C++

Posted on 2009-05-04
9
1,067 Views
Last Modified: 2013-12-19
I want to read the attached txt file into an array using C++. The array will look exactly the same as the input file. Should print on the screen.
A8.txt
0
Comment
Question by:eiyengars
  • 3
  • 2
  • 2
9 Comments
 
LVL 5

Expert Comment

by:rendaduiyan
ID: 24300994
you can
1)use ifstream to read each into string;
2)split string into two parts
3)convert them into double(float)
4)create a pair object to store those two number
5)insert  into a vector container
I cannot give you the code though.
0
 

Author Comment

by:eiyengars
ID: 24301090
Thank you but that doesn't help as I  have no experience writing a C++ code.
0
 
LVL 5

Expert Comment

by:rendaduiyan
ID: 24301234
I was told not to paste codes.
something like:
ifstream ifs("your.file")
//container
vector<pair<double, double>> array;
while(!ifs.eof())
{
    string line;
    getline(ifs, line);
     //split line into two substring(using sub_str())
     //make_pair
     array.push()
}
good luck.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:tculler
ID: 24301569
Since you're using Visual C++, not Native, you can just use the Managed classes. I'm guessing you want each line in the file to be read  into an index of an array? If so, consider this code. I hope it's what you're looking for; if not, let me know.
static System.String[] ReadFile(System.String myFile)

{

     return System.IO.File.ReadAllLines(myFile);

}
 

static void DisplayFileContents(System.String[] myFileContents)

{

     foreach(System.String line in myFileContents)

     {

          System.Console.WriteLine(line);

     }

}

Open in new window

0
 
LVL 9

Expert Comment

by:tculler
ID: 24311013
Whoops, sorry, that's C# syntax. Consider the following, instead, heh...
static cli::array<System::String^>^ ReadFile(System::String^ myFile)

{

     return System::IO::File::ReadAllLines(myFile);

}

 

static void DisplayFileContents(cli::array<System::String^>^ myFileContents)

{

     for each(System::String^ line in myFileContents)

     {

          System::Console::WriteLine(line);

     }

}

Open in new window

0
 

Author Comment

by:eiyengars
ID: 24385533
It didn't really help solve my problem....
0
 

Accepted Solution

by:
eiyengars earned 0 total points
ID: 24420113
Thanks for suggestion. I solved the problem on my own . None of the suggested solutions helped.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

746 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

10 Experts available now in Live!

Get 1:1 Help Now