cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
Simple file handling program
Simple file handling program
Apr 20, 2015 at 6:02pm UTC
hg13190
(1)
The following c++ program is not producing the desired output. Please help.
//File1.cpp Program to open a file and display its contents on console
// and to calculate its size
#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
char readFile(char[]);
void main()
{
clrscr();
char filename[20],ch[100];
cout<<"Enter file name - ";
gets(filename);
for(int i=0;i!='\0';i++)
{
ch[i]=readFile(filename);
cout<<ch[i];
}
getch();
}
char readFile(char fname[20])
{
ifstream fin;
char ch;
fin.open(fname,ios::in);
if(!fin)
{
cout<<"File does not exist";
//exit (1);
}
while(!fin.eof())
{
fin.get(ch);
return ch;
}
}
Apr 20, 2015 at 6:24pm UTC
ShiroAisu
(27)
1. What is the desired output?
2. Use code tags and properly format your code.
3. Where do you think the problem lies? Have you tried any forms of debugging? Even simple output messages to do simple assertions can be very helpful.
Topic archived. No new replies allowed.