Hello there, I think I have a weird problem with a program I'm writing, specifically, a function to create a file. I made the following program just to test the important part of the problem, which is that it won't create the file. Here's the code.

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

ifstream fin;
ofstream fout;

void wrtfile(char b[15])
{
  fout.open(b);
  fout << "Hello there" << endl;
  fout.close();
}

void main()
{
  char a[15];
  a == "Hello.txt";
  wrtfile(a);
}

I mean, from what I've seen around the forums and the web, it should run perfectly, and it does run, but it doesn't create the file. I don't know if it is because of the compilator I'm using: Turbo C++, or if I'm missing a library. Thanks in advance for any help you can provide.

Dani AI

Generated

A few practical things to add to what already pointed out — the program itself (not the compiler) had the bug, and warnings are important.

First, common reasons a file "never appears":

  • The stream never opened successfully (no check was done).
  • The program is creating the file in a different working directory than where you are looking (IDE/runtime directory vs. project folder).
  • Permissions or OS restrictions prevent creating files in that location.
  • Use of old, nonstandard constructions can hide portability issues.

Prefer a simple, modern pattern that (1) uses a string for the filename, (2) opens the stream via constructor, and (3) checks the stream state. For example:

#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::string fname = "Hello.txt";
    std::ofstream ofs(fname); // modern C++ accepts std::string
    if (!ofs) {
        std::cerr << "Failed to create " << fname << '\n';
        return 1;
    }
    ofs << "Hello there\n";
    return 0;
}

If using an old compiler that requires const char*, pass fname.c_str() to the ofstream constructor; see std::string::c_str. Check the open/creation behavior with std::ofstream::open/state checks.

Quick troubleshooting checklist:

  • Rebuild with all warnings enabled and fix them.
  • After running, search the entire machine for "Hello.txt" — IDEs like Turbo C++ often use their own working directory.
  • Print an error message when the stream fails (use if (!ofs) or ofs.is_open()).
  • Try running the compiled EXE from a command prompt in a known directory and see where the file appears.
  • Consider moving to a maintained compiler for modern C++ behavior once class constraints allow.

Recommended Answers

All 6 Replies

1. don't use the version of fstream and iostream that have .h extension -- they are depreciated (obsolete) and were replaced by versions without extension. But if you are using an ancient compiler such as the original Turbo C++ you may have no other choice.

2. Don't use global variables for the stream class. Make it local to the function in which it is used.

3. Never erver use void main(). Always use int main instead. See this thread for more explaination about that.

4 The program didn't work because your compiler probably produced an error or warning that you ignored. Never ignore warnings because most of the time they are really errros. Example: a == "Hello.txt"; is an error that you should have corrected.

Ah, yes, about the "==", if I don't use it, it will tell me I'm missing an Lvalue and the program won't run then.

>>Ah, yes, about the "=="
that is a boolian operator, not an assignment operator.

you have a couple options
initialize the array with the string

char a[] = "Hello.txt";

or use strcpy

char a[15];
 strcpy(a,"Hello.txt");

Oh, I forgot, and about the compiler, if it were me, I'd change it, but is the compiler I have to use for my programming class, I don't know why they have us programming with that old thing, but can't do anything about it.

The problem with your program has nothing to do with your compiler -- it was you who write the bug not the compiler. The program you posted will not work correctly with any compiler.

For a beginning class in c++ there is nothing wrong with your compiler. You will however need a better compiler when you get to more advanced stuff.

Thank you very much! It seems the strcpy() is just what I was looking for, hopefully won't have more troubles with this, if you have any other idea you think I might find useful, please post it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.