Author Topic: simple fstream program help  (Read 2444 times)

Offline Vlad417

  • Multiple posting newcomer
  • *
  • Posts: 14
    • Myspace
simple fstream program help
« on: June 07, 2007, 05:07:44 am »
as a novice programmer im already lost on why my program isnt working
compiler: GNU GCC

Code: [Select]
#include <iostream>
#include <fstream>
#include <conio2.h>
using namespace std;

int main () {
  char c, str[256];
  ifstream is;

  cout << "Enter the name of an existing text file: ";
  cin.get (str,256);

  is.open (str);        // open file

  while (is.good())     // loop while extraction from file is possible
  {
    c = is.get();       // get character from file
    cout << c;
  }
  is.close();           // close file
    getch();
  return 0;
}

all i want this to do is output a file's content. i hav a sample txt file at the same path of the .exe('test.txt'). when i run the program, i've tried entering 'test' and 'test.txt'.  both times the program just pauses at getch(); and doesnt output anything. test.txt has the word 'test' as it's content.

why am i not getting the wanted result?

Offline dje

  • Lives here!
  • ****
  • Posts: 683
Re: simple fstream program help
« Reply #1 on: June 07, 2007, 08:13:45 am »
Hi !

Did you try absolute path ?
You may test is.is_open() to test success of your open call and perform error management.

Maybe you could use _MAX_PATH standard constant for your file path buffer length.

Dje

Offline Roman

  • Multiple posting newcomer
  • *
  • Posts: 78
Re: simple fstream program help
« Reply #2 on: June 07, 2007, 11:05:24 am »
Hi

I don't know what the hell is with your code but have an advice:
Use debugger and watch where between is.open() and getch() goes something wrong. Then You could ask a more precise question and have more chances to get an answer.

Good Luck
Roman
CB LSI (C::B as a Little Secret Initiative)

Offline windows_usr

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: simple fstream program help
« Reply #3 on: June 08, 2007, 10:02:55 pm »