Question

Implement a C++ map to store data in a file, such as any .dat or .txt...

Implement a C++ map to store data in a file, such as any .dat or .txt file filled with data. The data should be loaded to map from the data file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream> //for cout
#include<fstream> //for ifstream
#include<map> //for map
#include<iterator> //for iterator
using namespace std;

//main function
int main()
{
map<int,int> m; //declared variable of map
ifstream in; //declared in stream
in.open("input.txt"); //opens input.txt file
int x,y;
if(in==NULL){ //if file fails to open then prints message and terminates program
cout<<"Error occurred while opening file";
return -1;
}
while(in>>x>>y) //reads values into x and y
{
m.insert(pair<int,int>(x,y)); //inserts values accordingly into map
}
//creates and iterator for map
map<int,int>::iterator it = m.begin();
while(it!=m.end()){ //iterates till end of map and prints the values in map
cout<<it->first<<" "<<it->second<<endl;
it++;
}

return 0; //terminates the program
}

//output screenshot

//input.txt screenshot

//any query, post in the comment section

Add a comment
Know the answer?
Add Answer to:
Implement a C++ map to store data in a file, such as any .dat or .txt...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • create a program that sum value from a txt file in c++,the data in the txt...

    create a program that sum value from a txt file in c++,the data in the txt file are in columns

  • Language is C++ I am reading in data from a file. In the .txt file is...

    Language is C++ I am reading in data from a file. In the .txt file is a series of dates. I need help converting dates from the format "20-Apr-18" to the standard "4/20/18" I've gotten the rest taken care of, I just need help converting the dates from the file. Thank you.

  • C++ Problem: Power Plant Data. The data file power1.dat contains a power plant output in megawatts...

    C++ Problem: Power Plant Data. The data file power1.dat contains a power plant output in megawatts over a period of 10 weeks. Each row of data contains 7 floating-point numbers that represent 1 week's data. In developing the following programs, use symbolic constants NROWS and NCOLS to represent the number of rows and columns in the array used to store the data. Write a function to compute and return the average value in a two-dimensional vector of type double. Assume...

  • In C++, how would I read data from a .txt file into an array? For example,...

    In C++, how would I read data from a .txt file into an array? For example, I have an assignment that requires me to read temperatures from a file named data.txt into an array. How would I go about doing this?

  • Using java: Store the files in .txt format and use the readByte() and writeByte() methods. File...

    Using java: Store the files in .txt format and use the readByte() and writeByte() methods. File encryption is the science of writing the contents of a file in a secret code. Write an encryption program that works like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code....

  • Using C++ coin1.txt ..-'''''-.. .' ___ '. / .'\ `\ \ ; /, ( | ; ; /_ '._ / ; | |- '._`) | ; '-...

    Using C++ coin1.txt ..-'''''-.. .' ___ '. / .'\ `\ \ ; /, ( | ; ; /_ '._ / ; | |- '._`) | ; '-;-' \ ; ; '''' / \ ; \ '.__..-' / '._ QUARTER _.' ''----''' coin2.txt ..-'''''-.. .' T E X A S '. / __ \ ; | |_ ; ; ___ | `~\ ; | \ ) | ; \/~ / ; ; '''' \ / ; \ \_\ / '._ 2...

  • Hey, i was wondering how i get data from a .txt file and put it into...

    Hey, i was wondering how i get data from a .txt file and put it into an array? We have to create a structure to hold the information(firstname, lastname, and age) on some characters. That character info is stored on a .txt file. Any and all help is greatly appreciated. (We are using Visual studio 2017 if that makes a difference) txt file Bugs Bunny 1940 Elmer Fudd 1933 Porky Pig 1935 Daffy Duck 1937 Tweety Bird 1942 Taz Devil...

  • Write a c++ program to implement bubble sort of the data point given in the file...

    Write a c++ program to implement bubble sort of the data point given in the file data_points.txt file found in the Files area under assignments. Submit the source code in canvas. Do not change the data file name in your program if you are using file operations to load the values and should read from the same directory where your program reside

  • Language c++ 9.10 Money from File This exercise will read 3 fields from a file for...

    Language c++ 9.10 Money from File This exercise will read 3 fields from a file for an unknown number of rows. It mimics the listing of dollar bills of a particular denomination that belongs to a certain person. The following data read will be read in the following order: wallet owner,number of bills,denomination of bill The wallet owner will be a string, the number of bills and denomination will be ints. Further, the denominations will always be 0,1, 2, 5,...

  • txt file iuput in C. If there is a txt file which only contain one row...

    txt file iuput in C. If there is a txt file which only contain one row like 1 2 3 How can I read these three integers individually as a special value to pass to a integer i and the size of a two dimensional array like: int i = 1; array a[ 2 ][ 3 ] = {}; Thanks

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT