Streams & files

Hey there! The program below are supposed to take as an argument a few files and place them in a vector. Then creates the file and writes the text of incoming files. But the command line I run the exe, it writes - work program stopped ... What can u advice?

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  #include <iostream>
#include <fstream>
#include <string>
#include <vector>

class Concatenation
{

private:

    int m_numbFiles;

    std::vector<std::string> m_OutputFile;

    std::ifstream m_ArrIstreams[];

    std::string m_ArrStrIn;

public:

    Concatenation( int, char*[] );
    void to_buffer();
    void output_file();

};

//--------------------------------------------------------

int main( int argc, char* argv[] )
{

    Concatenation C( argc, argv );

    C.to_buffer();
    C.output_file();

    std::system( "pause" );

    return 0;
}

//--------------------------------------------------------------

Concatenation::Concatenation( int argc, char* argv[] ) :
    m_numbFiles( argc )
{
    const int SIZE = 100;

    std::ifstream m_ArrIstreams[SIZE];

    for( int i=1; i<argc; i++ )
    {

        m_ArrIstreams[i-1].open( argv[i] ); 

        if( !argv[i] ){
            std::cerr << "Error: unable to open file!" << argv[i] << std::endl;
            m_isOpenAllFiles = false;
        }
    }
}
//----------------------------------------------------------------
void Concatenation::to_buffer()
{

    for( int i=0; i<m_numbFiles-1; i++ )
    {
        while( std::getline( m_ArrIstreams[i], m_ArrStrIn ) ){
            m_OutputFile.push_back( m_ArrStrIn );
            m_OutputFile.push_back( "\n " );
        }
    }
}
//---------------------------------------------------------------
void Concatenation::output_file()
{
    std::ofstream m_Ostream( "OutputFile.TXT" );

    for( int i=0; i<m_OutputFile.size(); i++ ){
        m_Ostream << m_OutputFile[i];
    }

    std::cout << "The file is recorded!" << std::endl;
}
your code does not compile on my machine.
i made a few changes
i just store the string open the file when you call to buffer.

1
2
// gives me an error
 std::ifstream m_ArrIstreams[];


1
2
// is undefined
m_isOpenAllFiles 


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

class Concatenation
{

private:

    int m_numbFiles;

	bool m_isOpenAllFiles;

	std::vector<std::string> m_fileArray;
    std::vector<std::string> m_OutputFile;

    std::string m_ArrStrIn;

public:

    Concatenation( int, char*[] );
    void to_buffer();
    void output_file();

};

//--------------------------------------------------------

int main( int argc, char* argv[] )
{

    Concatenation C( argc, argv );

    C.to_buffer();
    C.output_file();

    std::system( "pause" );

    return 0;
}

//--------------------------------------------------------------

Concatenation::Concatenation( int argc, char* argv[] ) :
    m_numbFiles( argc )
{
    // check if files open
	
    for( int i=1; i<argc; i++ )
    {		
		m_fileArray.push_back(argv[i]);

		std::ifstream fs;
		fs.open(m_fileArray[i-1].c_str());

		 if (fs.is_open())
		  {
			std::cout << "File successfully open" << std::endl;
			fs.close();
		  }
		  else
		  {
			std::cout << "Error opening file" << std::endl;
		  }		
    }

	
}
//----------------------------------------------------------------
void Concatenation::to_buffer()
{
	for (auto fs : m_fileArray)
	{
		std::ifstream file;
		file.open(fs);

		if(file.is_open())
		{
			for( int i=0; i < m_numbFiles-1; i++ )
			{
				while( std::getline( file, m_ArrStrIn ) ){
					m_OutputFile.push_back( m_ArrStrIn );
					m_OutputFile.push_back( "\n " );
				}
			}
		}
	}
}
//---------------------------------------------------------------
void Concatenation::output_file()
{
    std::ofstream m_Ostream( "OutputFile.TXT" );

    for( int i=0; i<m_OutputFile.size(); i++ ){
        m_Ostream << m_OutputFile[i];
    }

    std::cout << "The file is recorded!" << std::endl;
}
Last edited on
Thanks, but it write: syntax error!

for (auto fs : m_fileArray)
just change it to an index loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Concatenation::to_buffer()
{
	for (int iter = 0; iter < m_fileArray.size(); iter++ )
	{
		std::ifstream file;
		file.open(m_fileArray[iter]);

		if(file.is_open())
		{
			for( int i=0; i < m_numbFiles-1; i++ )
			{
				while( std::getline( file, m_ArrStrIn ) ){
					m_OutputFile.push_back( m_ArrStrIn );
					m_OutputFile.push_back( "\n " );
				}
			}
		}
	}
}
Thanks a lot)
I have tried to do as you. Already without errors, but id doesn't record file(((((
Works. Did a little change though.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>

class Concatenation
{

private:

    int m_numbFiles;

	bool m_isOpenAllFiles;

	std::vector<std::string> m_fileArray;
    std::vector<std::string> m_OutputFile;

    std::string m_ArrStrIn;

public:

    Concatenation( int, char*[] );
    void to_buffer();
    void output_file();

};

//--------------------------------------------------------

int main( int argc, char* argv[] )
{

    Concatenation C( argc, argv );

    C.to_buffer();
    C.output_file();

    system( "pause" );   //change from std::system()..
                  // system is not in namespace std. its a system call

    return 0;
}

//--------------------------------------------------------------

Concatenation::Concatenation( int argc, char* argv[] ) :
    m_numbFiles( argc )
{
    // check if files open
	
    for( int i=1; i<argc; i++ )
    {		
		m_fileArray.push_back(argv[i]);

		std::ifstream fs;
		fs.open(m_fileArray[i-1].c_str());

		 if (fs.is_open())
		  {
			std::cout << "File successfully open" << std::endl;
			fs.close();
		  }
		  else
		  {
			std::cout << "Error opening file" << std::endl;
		  }		
    }

	
}
//----------------------------------------------------------------
void Concatenation::to_buffer()
{
	for (auto fs : m_fileArray)
	{
		std::ifstream file;
		file.open(fs);

		if(file.is_open())
		{
			for( int i=0; i < m_numbFiles-1; i++ )
			{
				while( std::getline( file, m_ArrStrIn ) ){
					m_OutputFile.push_back( m_ArrStrIn );
					m_OutputFile.push_back( "\n " );
				}
			}
		}
	}
}
//---------------------------------------------------------------
void Concatenation::output_file()
{
    std::ofstream m_Ostream( "OutputFile.TXT" );

    for( int i=0; i<m_OutputFile.size(); i++ ){
        m_Ostream << m_OutputFile[i];
    }

    std::cout << "The file is recorded!" << std::endl;
}


1
2
g++ -o test test.cpp -std=c++11  //compile
test test.cpp test.xml  //concatenate the 2 files in OutputFile.txt 
Thank u!
Topic archived. No new replies allowed.