writing in files issue

Hello,

Well the program below is absolutely useless. When I execute it I create a file with the name of the address and not with the name "file.txt".In addition when I try to delete it nothing happens. What I want it to do is to create a file called file.txt and then delete it. I understand that the file is called with the address and not file.txt because I use the address pointer but I have no clue how to fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <fstream>
#include "stdlib.h"

int main(){
    void save(char fname, int x);
    void del(char fname);

    const char *fname = "file.txt";
    int x = 312;
    save(*fname, x);
    del(*fname);
}


void save(char fname, int x){
    
    std::ofstream fout(&fname);
    fout<<std::endl<<x<<std::endl;
}

void del(char fname){
    
    remove(&fname);
}


Any help appreciated (as always)
removing a file ,whatever it may be called,is done by this function :-

 
remove(yourfilenamehere);


The remove() function is in <cstdlib> or <cstudio> i dont remember.
In the function header, parameter fname is a single character void save(char fname, int x)

It needs to be a string,
void save(std::string fname, int x)


( well you could instead use char * fname but this is after all C++ code you are writing, so the C++ string seems appropriate).

So I changed the code as you told me (if I understood correctly):
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
#include <iostream>
#include <fstream>
#include "stdlib.h"

using namespace std;

int main(){
    void save(string fname, int x);
    void del(string fname);

    const string fname = "file.txt";
    
    int x = 312;
    
    save(fname, x);
    del(fname);
}


void save(string fname, int x){
    
    std::ofstream fout(fname);
    fout<<std::endl<<x<<std::endl;
}

void del(string fname){
    
    remove(fname); //No matching function for call to "remove"
}


And I get 2 errors:
1)No matching function for call to "remove"
2)Undefined symbols for architecture x86_64:
"del(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I guess I didn't understand correctly.
I think you understood. Just that getting past the first obstacle clears the path to reach the next obstacle.

1)No matching function for call to "remove"

The remove() function requires this header:
 
#include <cstdio> 


The code will also need a small adjustment. The remove() function is expecting a c-string rather than a C++ std::string.

That's easily fixed by using the c_str() member function.
 
    remove(fname.c_str());



http://www.cplusplus.com/reference/cstdio/remove/
Last edited on
I should build a church for you. Thank you, now it works fine. Just for the glory of the high heavens, I tried to run it without this line (I commented it)
#include <cstdio>

and it worked fine too. I guess that happened because I had the "stdlib.h" included but I understand that it would be a "good practise" to include the <cstdio>. One more thing. Can you explain me the difference between the 2 type of strings (the so called c-string and the std::string)?

Thank you, I appreciate it.
Regarding the #include . Often one header may include some or all of another header, so it may not be necessary to explicitly state all the required headers. It depends on which compiler you use, code which works in one may not work in another. If you put all the required includes, the code will be more portable, that is it should work on any compiler which conforms to the standard.

As for the different string types. C++ is originally based upon C (though the two languages have diverged somewhat). In C, a string is simply an array of characters. The end of such a string is marked by a null character, that is a byte with the numeric value zero.
You can read in detail about them in the tutorial here:
http://www.cplusplus.com/doc/tutorial/ntcs/

That same tutorial page also mentions the C++ std::string. In C++ code this is preferred. It is generally safer and easier to use, it takes care of the low-level operations such as allocating and freeing storage, automatically resizing as required and so on.

Topic archived. No new replies allowed.