News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

simple fstream program help

Started by Vlad417, June 07, 2007, 05:07:44 AM

Previous topic - Next topic

Vlad417

as a novice programmer im already lost on why my program isnt working
compiler: GNU GCC

#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?

dje

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

Roman

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)