5
code movietitle
1 Kungfu Panda
2 Hop
3 Hikayat Merong Mahawangsa
4 The Roomate
5 Pirates Of The Carribean 4-On Stranger Tides
how to read this kind of data??
The library to use is <fstream> for file I/O. You can do simple input of a number or line (string) as follows:
#include <fstream>
#include <string>
int main() {
std::ifstream in_file("my_list.txt"); //opens the file "my_list.txt"
int code; //declare variable for the integer code
std::string name; //declare a variable for the name
in_file >> code; //read the code from the file
std::getline(in_file, name); //read the name from the file (remaining characters on the line).
return 0;
};
You can also verify that the file stream has not reached the end-of-file by a simply using the file stream in an if-statement. This allows you to do a loop like this:
#include <fstream>
#include <iostream>
int main() {
std::ifstream in_file("my_list.txt");
int code;
while(in_file >> code) { //this reads the code and checks that it did not reach end-of-file.
std::cout << "A code is " << code << std::endl;
};
return 0;
};
With the above, you should be able to solve your problem. Giving any more hints would be cheating.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.