User forums > General (but related to Code::Blocks)

simple fstream program help

(1/1)

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


--- Code: ---#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;
}
--- End code ---

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

windows_usr:
Try this one...http://www.cplusplus.com/reference/iostream/istream/get.html

Navigation

[0] Message Index

Go to full version