[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Read a TXT file into an array in C++

Posted on 2009-05-04
9
Medium Priority
?
1,085 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

 
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

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…
In this short web based tutorial, I wanted to show users how they can still use the powers of FrontPage in conjunction with Expression Web 3.  Even though Microsoft eliminated the use of Web components, we can still use them with FrontPage and edit …
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 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…

656 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