Hi.. can anyone help me.. I having problems in displaying the output read from an input file. when I run the code, it only display the output of the first character in the input file. Anyone can help me figure out the problem? & I'm using borland.

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <conio.h>



int main ()
{
	int character;
   int count = 0;
	ifstream inputFile;
   char fileName[100];
   cout<<"Enter file name :";
   cin>>fileName;
   inputFile.open(fileName);

   if (!inputFile)
   	cout<<"Eror opening file. File name "<<fileName<<" does not exist."<<endl;
   else
   {
   	while (count <= 99)
      {
      	inputFile >> character;
         cout<<character<<endl;
         count++;
      }
   }

Recommended Answers

All 5 Replies

cin is getting an error on the first character because it is not numeric digit. You declared variable character as int, so cin expects to read numeric digits. Change the data type of character to char and it should work as expected.

Suggestion: Change the while loop to read all the characters in the file

while( cin >> character )
{
   cout << character << endl;
   ++count;
}

cin is getting an error on the first character because it is not numeric digit. You declared variable character as int, so cin expects to read numeric digits. Change the data type of character to char and it should work as expected.

Suggestion: Change the while loop to read all the characters in the file

while( cin >> character )
{
   cout << character << endl;
   ++count;
}

Thanks for your help~ I appreciate it!!~ ^_^

read from input file using C

Did you mean C, or do you mean C++? You're using C++ (a very old C++ that will not work on modern compilers; looks like pre-standard C++ to me, as if you were using a decade old compiler... using Dev-C++, by any chance?)

read from input file using C

Did you mean C, or do you mean C++? You're using C++ (a very old C++ that will not work on modern compilers; looks like pre-standard C++ to me, as if you were using a decade old compiler... using Dev-C++, by any chance?)

I'm using borland c++ not dev-c++ .. I'm new to this language.. so maybe because I refer to old books.. That's why it looks like some very old c++.. hope to learn more from u guys.. :)

If you're just learning now, I think you might do better to learn modern standard C++ rather than old C++; if you try to use old, pre-standard C++ with a modern compiler that adheres to the C++ standard, you'll have trouble.

There are many free modern IDEs and compilers available to you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.