Solved

Read a TXT file into an array in C++

Posted on 2009-05-04
9
1,077 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
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.
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

791 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