File handling problem

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.

But it show output only for 1 sec.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  /* 
* This program reads from a txt file “myfile.txt” which contains the 
* employee information 
*/ 
#include <iostream> 
#include <fstream> 
using namespace 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"
ok Thnaks, I'll check and tell you back.
Topic archived. No new replies allowed.