0

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??

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by mike_2000_17
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.