Writing file to text document

Okay so im trying to write the sentence that the user has given me into a text file using this code

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int StoreDatabase()
{
    const int SIZE = 100;
    EntryNumber = 0;
    ofstream AddToFile;
    string sentence;
    string FindFile;
    string Extention;
    string NewFileName;
    LetterByLetter Print;
    system("cls");
    DisplayText = "Which file would you like to add too:";
    Print.Text();
    cout << endl;
    getline(cin, FindFile);
    Extention = ".txt";
    NewFileName = FindFile + Extention;
    FILE *istream;
    if ( (istream = fopen ( NewFileName.c_str(), "r" ) ) == NULL )
    {
         printf ( "File Does Not Exist!\n" );
         main();
    }
    else
    {
        fclose ( istream );
        system("cls");
        char sentence[SIZE];
        DisplayText = "What would you like to add:";
        Print.Text();
        cin.getline(sentence, SIZE);
        for (int i = 1; i < 100; i++)
            sentence[i] = tolower(sentence[i]);
        for (int i = 0; i < 1; i++)
            sentence[i] = toupper(sentence[i]);                
        time_t rawtime;
        struct tm * timeinfo;
        time ( &rawtime );
        timeinfo = localtime ( &rawtime );
        AddToFile.open(NewFileName.c_str(),ios::app);
        if( AddToFile.is_open())
        {
            AddToFile.seekp(0,ios_base::end);
            AddToFile << "<----------    Entry    ---------->" << endl;
            AddToFile << sentence<< endl;
            AddToFile << "Time Entry Added: " << asctime (timeinfo) << endl;
            AddToFile << endl;
        }
        main();
    }
    system("PAUSE");
}


When ever i do this i check the text file and the first letter of the sentence the user has given me is gone. do you no how to fix this cause im stumped?
¿Could you simplify that code?
You could clean up that code by declaring variables where they're used.

The odd comment about what you intend wouldn't hurt either, nor would providing the definition of AddToFile.

There are better ways to check if a file exists.
¿Like what, and why is it better?
¿Like what, and why is it better?
Is this about the file exist thing?

stat() uses the file system metatdata rather than accessing the file itself. There are few operations more expensive than opening a file. And if all you do is open it to ensure that you can is a waste of resources.
I can't come up with an scenario where you would not want to use the file.
I missed the "close FILE*, open ofstream" in the OP.
I see it all the time. It's also used to check a file size, a seek to the end and seek back to the beginning before a buffer's allocated. It's all caused by these bits of bad sample code lying around.

A pet hate of mine is checking if a file is open. Who cares? You should care if the stream is good or not. Accomanying example: http://www.cplusplus.com/reference/iostream/ifstream/is_open/
Topic archived. No new replies allowed.