Inserting a .txt file

Hey guys

Can someone please write and describe the steps for inputting a .txt file into a C++ program. I need my program to output information based on the .txt file through a struct but I'm having trouble inserting the .txt file into the program.

Thanks
Hi wizard25,

You can use ifstream and its associated functions. Look at the example on this page

http://www.cplusplus.com/reference/iostream/ifstream/open/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream> // dont forget this
#include<string>
using namespace std;

int main()
{

ofstream writeFile("myFile.txt",ios::app); //input/creates text file for writing
writeFile<<"write this"; // writes that to text file
writeFile.Close(); // closes text file

ifstream readFile("myFile.txt",ios::app); // open myFile.txt for reading
string line = ""; 
getline(readFile,line,'~'); // reads file line by line and stores it in string line;
cout<<line;


system ("pause");
return 0;
}
Last edited on
Topic archived. No new replies allowed.