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.

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.