Inserting a .txt file

May 20, 2009 at 6:01am
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
May 20, 2009 at 6:38am
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/
May 20, 2009 at 11:33am
May 21, 2009 at 4:27pm
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 May 21, 2009 at 4:28pm
Topic archived. No new replies allowed.