C++ problem, reading a file

reading and displaying elements of a .txt file into an array

Page 1 of 1

3 Replies - 1447 Views - Last Post: 23 May 2008 - 10:43 AM Rate Topic: -----

#1 satjit_singh88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-May 08

C++ problem, reading a file

Post icon  Posted 23 May 2008 - 07:29 AM

I am trying to read a .txt file into an array, but i cant get the .txt file which the user inputs in the program

#include <iostream>
#include <fstream>
using namespace std;


int main ()
{
string fileName;
cout << "Enter the filename of the student data file: " << flush;
cin >> fileName;

ofstream myfile (fileName);
if (myfile.is_open())
{
infile>>name.studentID>>name.assignment1;
cout<<name.studentID<<name.assignment1;
myfile.close();
}
else cout << "Unable to open file";
return 0;
}

Is This A Good Question/Topic? 0
  • +

Replies To: C++ problem, reading a file

#2 joske  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 297
  • Joined: 04-September 07

Re: C++ problem, reading a file

Posted 23 May 2008 - 10:15 AM

I think you have to change if (myfile.is_open()) to if (myfile.open())
Was This Post Helpful? 0
  • +
  • -

#3 Dave11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-February 08

Re: C++ problem, reading a file

Posted 23 May 2008 - 10:40 AM

Try to change
string fileName;
in
char fileName[20];
and
 ofstream myfile (fileName);
in ;
 ifstream myfile (fileName);
if you want to read from the file
Was This Post Helpful? 0
  • +
  • -

#4 ahmed_wesam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-May 08

Re: C++ problem, reading a file

Posted 23 May 2008 - 10:43 AM

thank you,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1