Hi guys,

I've compiled the code given below which allows the user to enter the address of a text file and then view it(the whole thing). But, as soon as input is taken,

"openfile.exe has encountered a problem and needs to close. Send Error Report."

When I Press Debug
something in assembly language pops up.

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


int main() 
{
char *fname=0;
cin >> fname;
ifstream OpenFile(fname);
char ch=0;
while(!OpenFile.eof())
{
OpenFile.get(ch);
cout << ch;
}
OpenFile.close();
}

Can anyone please mention the error?
Constructive Suggestions Appreciated.


Cheers,
Anurag

char *fname=0;//Change to char fname[50] or fname[256] for max limit on Windows
cin >> fname;

Bad idea! you are passing a pointer to cin which is not referring to any variable. Use an array. Also try using fgets() or gets() as cin doesn't take white spaces.

The correct way of using a pointer is:

1. You can either initialize them at the start:

char *p="Some String";

2. Or reference them to something:

char str[10];
char *p;
p=str;

What you did was you passed a pointer which was not pointing to any location & hence it caused the crash.

As for cin>>
This lil' baby does not take whitespace: so If I input "Hello World", cin>> will take only "Hello" and discard " World" into the buffer.

Alternatives are:
fgets(), Usage:

char st[50];
fgets(st,50,stdin); //Here stdin is the [B]St[/B]andar[B]d[/B] [B]In[/B]put Stream

Since you are pretty much using stdin for input you can opt for gets(), Usage:

char st[50];
gets(st);

However fgets has a little advantage since it can limit the number of input characters.

Hope thats clear to you.

Edited 6 Years Ago by nbaztec: n/a

Another apporach would be to use getline. Something like this should work

string filename;
cout << "Enter the filename: ";
getline(cin, filename);
ifstream OpenFile(filename.c_str());
//...

You will need to add #include<string> to your code for this to work. Also you are using the headers you have are depreacted. You have

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

// change that to this
#include <iostream>
#include <fstream>

BTW what compiler are you using?

Edited 6 Years Ago by NathanOliver: n/a

Comments
Appropriate solution for the problem

I am exactly on Borland Turbo C++(sorry, i know it's obsolete, but we have to work on this itself. i suggested dev c++, but guys around here are stuck to Turbo)
And that is why I had to use "deprecated" headers

But as i use gets(), the program exits, and doesn't show me any text..

Oh sorry, the program shows an output like this
(for open.cpp)
#
i
n
c
l
u
d
e
......

Hey guys i patched the error all right

Here's the code. The case is solved. Thank You Nathan Oliver,abhimanpal, nbazteec!!
ON DEV C++

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  char fname[256];
  gets(fname);
  ifstream myfile (fname);
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
    system("PAUSE");
  }

  else cout << "Unable to open file"; 

  return 0;
}

@nbaztec

Are you actually recommending gets over fgets ?

Not really. I did mention fgets() has an advantage. But since the OP wanted to input a file name only, gets() should work fine for him. :)

Since you are pretty much using stdin for input you can opt for gets(), Usage:

char st[50];
gets(st);

However fgets has a little advantage since it can limit the number of input characters.

Hope thats clear to you.

OP wanted to input a file name only, gets() should work fine for him
int main()
{
    char str[10];    
    printf("Enter file name\n");
    gets(str);    
    printf("File name is %s\n",str);
    return 0;
}

This is a short stub which accepts the file name from the user. Are you actually saying its fine to use gets here ?

This question has already been answered. Start a new discussion instead.