Problem with creation of files

i am doing a program where i have to create a file, witch i have to ask where to save, the name, and the extension, and then it have to be writing wat i want for the all program, but at the end i have a question about want to leave the program or not, if this anwer is to get out of the program i want him to get out of the program and save the file, but if the answer is to continue in the program i want him to continue writing withou deleting anything...
i looked in this website:
http://www.mattjustice.com/cnotes/c_fileio.html
but the program allways delete the contents of the file
this is the part thtat creates the file in my program:
1
2
3
4
5
6
7
8
9
FILE *f;
if(f != NULL)
	{
		f=fopen("c:\\euromilhoes.txt","r+");
        }
else
	{
		f=fopen("c:\\euromilhoes.txt","w");
	}

this file is allways created in the beggining of the program...
if anyone can help me, i would be thankfull
Is this what you intended?
1
2
3
std::FILE* f = std::fopen( "c:\\euromilhoes.txt", "r+" ) ;
if( f == nullptr ) f = std::fopen( "c:\\euromilhoes.txt", "w" ) ;
if( f == nullptr ) { /* give up */ } 


what will that do? is to only writte in the file and to not delete wat is in it?
Line one tries to open an existing file for reading and writing.
If that does not work ( f is null ) line two tries to create a new file.

Ckeck out std::FILE* f = std::fopen( "c:\\euromilhoes.txt", "a+" ) ;
http://www.cplusplus.com/reference/clibrary/cstdio/fopen/
That may be just what you need.

Note: In the original code,
1
2
std::FILE *f ; // f is an uninitialized pointer
if( f != NULL ) // you can't use f meaningfully till after it is properly initialized  

i allready saw wat was wrong... in the website that i got it didnt meantion the "a+" and that is the solution... tks.

now how can i ask to the user to tell where to create the file and how to call it? because i cant put something like:
f=fopen("%c:\\euromilhoes.txt", driver,"a+");
> now how can i ask to the user to tell where to create the file and how to call it?

I'm not sure I understand that question.
will i am running the program, the program should ask the user in whwat driver and what the name of the file to be created.
something like this:
Wich is the drive of the file?
>
Wich is the file's name?
>


now i saw one problem... the file euromilhoes.txt that i did in f=fopen("%c:\\euromilhoes.txt", driver,"a+");
now i cant delete it... do you know why?
Last edited on
well, i'm with the same doubt.

The program should ask:

Wich is the drive of the file?
>
Wich is the folder of the file?
>
Wich is the file's name?
>


How do I do that?
Last edited on
Use a std::string to hold this information. http://www.cprogramming.com/tutorial/string.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char drive_letter ;
std::cout << "Wich is the drive of the file? " ;
std::cin >> drive_letter  ;

std::string path_to_folder ;
std::cout << "Wich is the folder of the file? " ;
std::cin >> path_to_folder ;

std::string file_name ;
std::cout << "Wich is the file's name? " ;
std::cin >> file_name  ;

std::string full_path_to_file = drive_letter  + "\\" + path_to_folder  + "\\" + file_name  ;
std::FILE* f = std::fopen( full_path_to_file.c_str(), "a+" ) ;
if( f == nullptr )
{
    std::cerr << "file name '" << full_path_to_file << "' is invalid\n" ;
    // ...
}


isnt there another way to do it? because i dont understand nothing about strings and i when i insert your program into mine it just give me errors

and i really dont understand all of that cout and cins... never used that, and in my all program i am using printf and scanf, is it possible to substitute?
Last edited on
> i dont understand nothing about strings ...
> and i really dont understand all of that cout and cins ...

It is perfectly fine that you do not understand something at this moment; folks here would be very willing to help you understand. But you have to make an effort to try and understand C++.

If you are unwilling to do that, perhaps you should post your questions on a C board. I've been told that this is a fairly good one: http://cboard.cprogramming.com/c-programming/



Last edited on
but isnt in C that it work with the cout and cin insted of the printf and scanf?
No. C++ works with cout and cin. C does not.
Topic archived. No new replies allowed.