Solved

Read a TXT file into an array in C++

Posted on 2009-05-04
9
1,074 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
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. …
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

777 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