[PLEASE HELP] create a file in function that returns void

I would like to create a file in function. there is no compiling error for the code below but it does not create any file either


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void Terminate (string data, int nSize,  map<int, node> &chord){

	ofstream dump("dump.txt");
	
	int key;
	string node;

	key = Hash(data, nSize);
	
	node = LookUp(key, nSize, chord);

	long size = chord[key].data.size();

	chord[key].data[size] = data;

	
	//dump.open("dump.txt");
	if(dump.is_open())
	{
		dump << data ;
	}
	dump.close();

	chord.clear();
	exit(1);

}
Last edited on
Hello JanetArthur32,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

The "ofstream" will create the file if it is does not already exist. If you found the file and it is empty then I would look at the variable "data" and make sure there is something to write to the file. A cout statement before or after the line that writes to the file would work.

Since the file was created the next question is are you looking in the right place? The output file should be in the same directory as the ".cpp" files and any header files that you created.

It would be helpful if you post the whole code that can be compiled and run. It is possible that the problem started somewhere else and someone else may see that. It also helps to be able to run the program and follow the variables.

Hope that helps.

Andy
Hi Andy,

Thank you for that tip! I'm new here and really appreciate it :)

my whole code would be as below:


Last edited on
Hello JanetArthur32,

Thank you that is much better. I will load this up after breakfast and see what happens.

Since the function ends wit "exit(1)". What do have in mind for this function to do? With what you have you are doing work that is never used, I see this with the lines I had to comment out just to get it to compile.

For what it is worth when the program ran it did crate the output file and write a string, "data", to the file.

The use of "exit(1)" will end the program at that point. The "1" denotes that there was a problem when the program ended. If there was no problem you should use zero which means a normal exit from the program just like the "return 0;" in main. Although it would be better to return to main to exit the program.

Hope that helps,

Andy
Hi Andy,

The program should be ran with a file, so I don't think you can run it the way it's supposed to.

I'm just really confused as to why the file is not created.

anyway, the exit(1) in terminate function is to terminate the program. I have changed it to zero now, thank you.
One thing I really hate about C++ is the horrible diagnostics.

There's a good chance that the stream library uses the C library, and that means it might set C's errno variable to indicate the reason for an error. Here's a modified version of Terminate that shows what I mean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cerrno>
#include <cstring>
#include <fstream>
#include <iostream>

using std::ofstream;
using std::strerror;
using std::cerr;

// ...

void Terminate (int data)
{
    errno = 0;   // clear any previous error
    ofstream dump("dump.txt");

    if (!dump) {
	cerr << "dump didn't construct: " << strerror(errno) << '\n';
    }
	
    //dump.open("dump.txt");
    if(dump.is_open()) {
	dump << data ;
    }
	
    if (!dump) {
	cerr << "Can't write to dump: " << strerror(errno) << '\n';
    }

    dump.close();
    exit(1);
}

Hello JanetArthur32,

What happened to your code. I did not have a chance to look it over or copy it.

To say the better when I fixed the function so it would compile for me it did create the output file and write the string, "data", to the file. It was not much at this point, but yo are only writing one string to the output file. Did you want something different?

Please put your code back as I was looking forward to going over it and seeing how it worked.

If by chance you use an input file you will need to include that or at least a fair sample if it is large.

Also if there any header file that you wrote you will need to include them.

Some people have used https://github.com/ or https://pastebin.com/ to post large code projects. And there are some others that are useful.

Andy
> The output file should be in the same directory as the ".cpp" files and any header files that you created.
no, it would be in the directory from which the program was executed.
you may have a directory structure like
./sources/*.cpp
./headers/*.h
./bin/debug/program.bin
./bin/release/program.bin
¿what's the working directory? ¿'.' or './bin/release/'?


1
2
	if (argv[1] != NULL){
		Read(argv[1]);
¿how are you executing your program?
¿what's the content of the input file?
Topic archived. No new replies allowed.