Solved

Read a TXT file into an array in C++

Posted on 2009-05-04
9
1,078 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.Net Errors Service Crashing 5 142
eclipse luna javeEE perspecive missing 5 119
YouTube Friendly Video Addresses 3 88
Build and evolve an interactive experience 1 99
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…
Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …

730 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