Trouble with double spacing any input text file

Hi everyone,

I'm writing a program which will double space any given input text file. My code below works but it only works once. If I try to run it the second time for the same file, it would give me an access violation error. I've tried to track the indexes when I copy the line to the string array and don't see any index goes out of bound. Could anyone please tell me where the problem is ?

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 _tmain(int argc, _TCHAR* argv[])
{
	ifstream inFile;
	inFile.open("dog or cat.txt");
	int count = 0;

	if (inFile.is_open())
	{
		string line;
		while (getline(inFile, line))
			count++;
		inFile.close();
	}
	
	else 
		cout <<"Something's wrong" << endl;
	
	
	string* line = new string [count];
	inFile.open("dog or cat.txt");

	
	if (inFile.is_open())
	{
		int i = 0;
		while (getline(inFile, line[i]))
		{
			i++;
		}
		inFile.close();
	}
	else 
		cout <<"Something's wrong" << endl;
	
	ofstream outFile;
	outFile.open("dog or cat.txt");

	if (outFile.is_open())
	{
		for (int i = 0; i < count; i++)
		{
			outFile << line[i] << "\n" << "\n";
		}
		outFile.close();
	}
	else 
		cout <<"Something's wrong" << endl;
	delete []line;
	
	system ("pause");
	return 0;
}


Thanks in advance.
I've been able to fix the problem by replacing the while loop by the for loop. However, I still don't understand why I had the problem with the while loop like above. Could anyone tell me what the problem was? Thanks in advance.

Also the code below is the part which I changed to fix the problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
        string* line = new string [count];
	inFile.open("dog or cat.txt");

	
	if (inFile.is_open())
	{
		for (int i = 0; i < count; i++)
		{
			getline(inFile,line[i]);
		}
	}
	else 
		cout <<"Something's wrong" << endl;
Last edited on
Didn't actually run your code, but it looks like the problem is on line 26, it will still attempt to access an out-of-bounds index [i] before getline actually fails.

e.g. file
apple
banana
carrot


Lines 7-13: count --> 3 (indices 0, 1, 2)

Lines 25-29:

i = 0

access line[0] --> OK
getline --> good
i = 1

access line[1] --> OK
getline --> good
i = 2

access line[2] --> OK
getline --> good
i = 3

access line[3] --> ERROR out of bounds, undefined behavior
getline --> {doesn't matter what happens, you already have undefined behavior}
Last edited on
Thanks a lot
Hello Song Tung,

In addition to what Ganado has said I made a change to line 19. I changes "count" to "count + 1" and the program worked fine with the while loop.

The other thing I had to do to get the program to compile is change "_tmain" to "main". And I removed everything in the ()s because "_TCHAR" was also a problem. Also "argc" and "argv" are not used in the program, so there is no need for them to be there. After making these changes the program compiled.

I do not use dynamic arrays that often, but I am thinking that the size of the array that is created is something + 1. Or it could have been the blank lines in the text file that I used creating the problem. Not sure yet.

Hope that helps,

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

int main()
{
    const std::string file_name = "dog or cat.txt" ;
    std::vector<std::string> lines ;

    if( std::ifstream file{file_name} ) // if opened for input
    {
        std::string str ;
        while( std::getline( file, str ) ) lines.push_back(str) ;
    }

    else
    {
        std::cerr << "could not open input file " << std::quoted(file_name) << '\n' ;
        return 1 ;
    }


    if( std::ofstream file{file_name} ) // if opened and truncated for output
        for( const std::string& str : lines ) file << str << "\n\n" ;

    else
    {
        std::cerr << "could not overwrite file " << std::quoted(file_name) << '\n' ;
        return 1 ;
    }
}
Registered users can post here. Sign in or register to post.