Writing a String to a File

Here is the problem:

Write a program to open a text file named "CSC2134.TXT" for output, then
accept lines of text from the console and write the lines of text to the
file. An empty string can be used to cause the program to quit accepting
lines of text from the console and close the file. To test for an empty
string (which occurs when you press the Enter key without typing any text),
use the strlen() function from the string.h include file. This function will
return an integer value indicating the number of characters in a string. Here
is an example program to show you how strlen() works:

#include <iostream>
#include <string>
using namespace std;
int main() {
char str[] = { "123456789012345" };
cout << strlen(str) << endl;
return 0;
}

You will find a submethod of cin useful. Here is an example:

cout << "enter some text:";
cin.getline(str, 80); // str is returned when the enter key is pressed

Here is an example of how the input should be done:

Enter text: Now is the time
Enter text: for all good men
Enter text: to come to the aid
Enter text: of their party.
Enter text: (Enter key pressed)
(program ends)


This is my code so far:

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(void)

{

    char str[] = { "123456789012345" };
    char strlen [80];

     ofstream file1;  //declare the file variable
     file1.open("CSC2134.TXT");  //open the file for output

     cout << "Enter some text: ";

     cin.getline(str, 80); //str is returned when enter key is pressed

        if (strlen == 0)
        return 0;

        else
            {
            cout << "Enter some text: ";
            cin.getline(str, 80); //str is returned when enter key is pressed
            }

     if(file1 == 0)

        {
         cout << "error opening CSC2134.TXT" << endl;
         return 1;

        }
file1.close();  //close file

return 0;
}



The problem is that the program runs only twice and then stops. I need it to keep asking for text input until nothing is entered. I also need it to print. I'm sure I have a loop wrong somewhere.
Last edited on
closed account (NUj6URfi)
You're getting the text lines but not printing them. Also, you do not have a single loop just if statements. Plus if(file1 ==0) should be after you open the file not after everything else. I'm not sure even if if(file1==0) is the proper way of handling this.
I am revising the code, but it still does not work. Can anyone actually advise me on what i need to do?

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(void)

{

    char str[] = { "123456789012345" };
    char strlen [80];

     ofstream file1;  //declare the file variable
     file1.open("CSC2134.TXT");  //open the file for output

     cout << "Enter some text: ";

     cin.getline(str, 80); //str is returned when enter key is pressed

        if (strlen == 0)
        cout << "error opening CSC2134.TXT" << endl;


        else
            {
            cout << "Enter some text: ";
            cin.getline(str, 80); //str is returned when enter key is pressed
            }

file1.close();  //close file

return 0;
}

One thing you need to do is to actually call the strlen() function.
In the opening post, you provided an example of such a function call:
1
2
char str[] = { "123456789012345" };
cout << strlen(str) << endl; 

note: you will need to include the correct header
 
#include <cstring> 

The <string> header is for the C++ std::string which is something completely different.

Also, this character array should be renamed so as to avoid a clash of names:
 
char strlen [80];   // use some other name, strlen is already taken 


http://www.cplusplus.com/reference/cstring/strlen/

one more thing, the getline should be passed a buffer of the correct size:
cin.getline(str, 80); str is of length 16 but you told the function to expect 80.

http://www.cplusplus.com/reference/istream/istream/getline/
Last edited on
Ok, I made some changes, but I am still having trouble with the loop. When I run it, it is only asking for input twice. It should ask indefinitely unless nothing is submitted. I have it print to the screen, but it is only printing the second input. HOW do I get this to ask for the input until nothing is entered?

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
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int main(void)

{

    char str[] = { "123456789012345" };
    char strtext [80];

     ofstream file1;  //declare the file variable
     file1.open("CSC2134.TXT");  //open the file for output

     cout << "Enter some text: ";

     cin.getline(str, 16); //str is returned when enter key is pressed

        if (strlen == 0)
        cout << "error opening CSC2134.TXT" << endl;


        else
            {
            cout << "Enter some text: ";
            cin.getline(str, 16); //str is returned when enter key is pressed
            }

                {cout << str << endl;}

file1.close();  //close file

return 0;
}

Last edited on
I suggest line 11 is deleted, you don't need this: char str[] = { "123456789012345" };

In your call to getline, use the buffer now named strtext

I've no idea what this is supposed to be?
if (strlen == 0)
strlen() is a function - you need to pass it a parameter.

The logic and sequence of events is pretty much a mess too, but let's get the right ingredients in there, then they can be arranged into the right order.

here's a start:
1
2
3
4
5
6
    ofstream file1("CSC2134.TXT");  //open the file for output
    if (!file1)
    {
        cout << "error opening CSC2134.TXT" << endl;
        return 1;
    }



Last edited on
Please take some time to read on statements and flow control. This will help you. http://www.cplusplus.com/doc/tutorial/control/
I have just gotten myself more confused. Thanks for all of your help but I am giving up on this one. I'm just getting all stressed out. Thanks again:)
I'm sorry it turned out that way. It's hard to target help at the right level so that it is actually helpful, while not doing all of the work for you. Hope you will still come back another time.
Topic archived. No new replies allowed.