lines to txt file

I have two issues, so far, I am able to open an existing txt file, and write a line of code at the end of it. What I am trying to do is, ask the user how many of lines they wish to input, if like "3" for instance, I am supposed to ask the user three times to enter a line of text. What is happening is that it is asking 3 times, although when I check the text file, the lines are written on the same line. I am hoping to write the text on separate lines. Also, I think I am asking the user twice at the beginning? Such as
Enter a line of text.
Enter a line of text.
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 <iostream>
//#include <string>
#include <fstream>

using namespace std;

//void insert(int);

int main ()
{	
	const int SIZE = 100;
	char input[SIZE];
	fstream filestr;
	int amount;
	int i;

	filestr.open("test.txt", fstream::in|fstream::out|fstream::app);
	cout << "This will be entered at the end of the file." << endl;
	cout << "How many lines do you wish to enter?" << endl;
	cin >> amount;

	for (i = 0; i <= amount; i++)
	{
		cout << "Enter a line of text." << endl;
		cin.getline(input,SIZE);
		filestr << input;
	}
	
	filestr.close();
	return 0;
}
Last edited on
getline() extracts and discards the new line character.
Add a new line at the end (line 26).
1
2
// filestr << input;
filestr << input << '\n' ;
Thanks that was soo simple, but even more helpful.

Why am I writing the loop twice before getting an input in the beginning?
std::cin >> amount; is formatted input, it leaves the white space character after the digits of the number that was entered (typically, a new line) in the input buffer.

cin.getline(input,SIZE); is unformatted input; it does not skip over leading white space characters.

We can extract and discard the new line remaining in the buffer after std::cin >> amount; with
std::cin.ignore( 1000000, '\n' ) ; http://www.cplusplus.com/reference/istream/istream/ignore/
> if I wanted to say, add a line of text in between a certain line, would it be much different?
> For instance, having a text file with 7 lines, and the user wants to insert a line at the 5th line.


>> If the file is is not prohibitively huge, read the lines into a sequence container
>> (typically std::vector<std::string>), insert the lines to be added in the right place in the container,
>> and then overwrite the original file with the lines in the container.


>>> ...
>>> This is what I currently have. http://pastebin.com/zqzT9DDg
>>> ...

Something along these lines, perhaps:

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
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <vector>
#include <string>
#include <fstream>

// read lines (up to a maximum of max_lines) from an input stream into a vector
std::vector<std::string> get_lines( std::istream& stm, std::size_t max_lines = -1U )
{
    std::vector<std::string> lines_read ;

    std::string line ;
    while( lines_read.size() < max_lines && std::getline( stm, line ) ) lines_read.push_back(line) ;

    return lines_read ;
}

// insert lines in lines_to_be_added into vector lines at position curr_line
void insert_lines( std::vector<std::string>& lines, std::size_t curr_line,
                   std::vector<std::string> lines_to_be_added )
{
   curr_line = std::min( curr_line, lines.size() ) ;
   lines.insert( lines.begin()+curr_line, lines_to_be_added.begin(), lines_to_be_added.end() ) ;
}

// overwrite the lines in the file with the contents of the vector
void overwrite_file( const std::string& file_name, const std::vector<std::string>& lines )
{
    std::ofstream file(file_name) ; // open for output, truncate existing
    for( const std::string& ln : lines ) file << ln << '\n' ;
}

int main()
{
    const std::string file_name = "whatever.txt" ;

    std::vector<std::string> lines ;

    {
        std::ifstream file(file_name) ; // open the file for input
        lines = get_lines(file) ; // place all the lines in the file into the vector
    }

    {
        std::size_t num_lines ;
        std::cout << "how many lines to be added? " && std::cin >> num_lines ;

        std::size_t current_line ;
        std::cout << "at line (the first line is line# zero)? " && std::cin >> current_line ;

        // extract and discard the new line remaining in the input buffer
        std::cin.ignore( 1000000, '\n' ) ;

        std::cout << "enter " << num_lines << "lines:\n" ;
        // get the lines entered by the user (up to a max of num_lines)
        std::vector<std::string> lines_to_be_added = get_lines( std::cin, num_lines ) ;

        // insert the lines entered into the vector of lines
        insert_lines( lines, current_line, lines_to_be_added ) ;

        overwrite_file( file_name, lines ) ; // overwrite the file with the modified contents
    }
}
Topic archived. No new replies allowed.