Write your question here.
Hey Guys
I am Atta. Please help me.
My program is about file handling
In this program i am
1- creating a file then
2- opening it and then
3- writing employee name, salary and dept the then showing it and in last
4- closing it.
/*
* This program reads from a txt file “myfile.txt” which contains the
* employee information
*/
#include <iostream>
#include <fstream>
usingnamespace std;
main()
{
char name[50]; // used to read name of employee from file
char sal[10]; // used to read salary of employee from file
char dept[30]; // used to read dept of employee from file
ifstream inFile; // Handle for the input file
char inputFileName[] = "myfile.txt"; // file name, this file is in the current directory
inFile.open(inputFileName); // Opening the file
// checking that file is successfully opened or not
if (!inFile)
{
cout << "Can't open input file named " << inputFileName << endl;
exit(1);
}
// Reading the complete file word by word and printing on screen
while (!inFile.eof())
{
inFile >> name >> sal >> dept; cout << name << "\t" << sal << " \t" << dept << endl;
}
inFile.close();
system("pause");
}
your code is fine. it just exits because if cannot open the file.
most likely is that you don't have the file beside where your executable resides.
try changing "myfile.txt" to the full path or move it to where your executable is.
i put mine on "C:\\Temp\\myfile.txt"