Ofstream is not making an output file

Pages: 12
Hello everyone,

I'm new to the forum world as I have generally in the past just scrolled through them rather than ask for help but today I am desperate for help as I am writing a simple program to clean some data I have attained for a project.
However whenever I make a variable using std::ofstream and create a file, the file firstly does not open and secondly does not appear in my directory. I am using Visual Studios 2013 to code and I started with an empty solution so there shouldn't have been any issues in regard to that. I tested by also making a program that just prints to the output file, yet to no avail. I am not sure as to why nothing is getting made as the program reads the files but it is not making the output file.

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

const int NUMBER_OF_FILES = 21;

void cleanData(){
	std::string buff;
	std::string inFileName;
	std::string outFileName;

	//file names
	outFileName = "output.txt";
	inFileName = "input_";

	//create output file, should be in directory
	std::ofstream output(outFileName, std::ios::out | std::ios::app | std::ios::_Nocreate);

	//iterate for 
	for (int i = 1; i <= NUMBER_OF_FILES; i++)
	{
		std::ostringstream ss;
		ss << inFileName << i << ".txt";
		std::ifstream f;

		//read input file
		f.open(ss.str().c_str(), std::ifstream::in);

		//check if input file is open
		if (f.is_open())
		{
			printf("Files opened\n");

			/*check if output file is open
			if (output.is_open()){
			printf("output file open");*/

			//clean data in the input files by deleting last two characters
			for (int j = 0; j < 100; j++){
				getline(f, buff);
				buff.pop_back();
				buff.pop_back();
				output << buff << "\n";
				output.flush();
			}
			/*}
			else{

			//if output file not open, print error
			printf("output file not open\n");
			}*/
			f.close();
			output.close();
		}
		else

			//if input file could not be opened
			printf("Could not open the file\n");
	}
}


int main()
{
	printf("Initialise cleaning\n");

	cleanData();

	printf("Press enter to exit");

	//close console when enter is pressed
	std::cin.get();

	return 0;
}


Any help would be greatly appreciated :)

try specifying the file path to your desktop. It can be picky about that
Edit line 14
outFileName = "C:/Users/(username)/Desktop/output.txt";
inFileName = "C:/Users/(username)/Desktop/input_";
Last edited on
i've tried it before, unfortunately it still doesn't make a text file :(

I have a feeling it's in regards to the settings but not sure what
and create a file
std::ios::_Nocreate
Something seems slightly contradictory to me...
Oh my, I did not see that at all. Although just ran the code again and still no avail :(

Is there anything that may be causing the issue in the settings? I've only changed the working directory so that shouldn't be an issue since it is reading files, but it's still not outputting anything
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
#include <iostream>
#include <cstring> // <string.h>
#include <fstream>

const int NUMBER_OF_FILES = 21;

void cleanData()
{
    //file names
    const std::string outFileName = "output.txt";
    const std::string inFileName = "input_";
    const std::string inFileExt = ".txt";

    // create/open output file, should be in directory
    // note: std::ios::_Nocreate would be ignored; std::ios::app dominates the non-standard std::ios::_Nocreate
    // to verify that the file exists:
    // if( !std::ifstream(outFileName).is_open() ) { /* error, file does not exist */ }
    std::clog << "*** info: trying to open output file '" << outFileName << "'\n" ;
    std::ofstream output( outFileName, std::ios::out | std::ios::app );

    if( !output.is_open() )
    {
        std::cerr << "*** error: could not open output file\n" ;
    }

    else
    {
        std::clog << "*** info: opened file '" << outFileName << "' for output\n" ;
        
        for( int i = 1; i <= NUMBER_OF_FILES; i++ )
        {
            const std::string inputPath = inFileName + std::to_string(i) + inFileExt ;
            std::ifstream f(inputPath) ;
            
            if( !f.is_open() )
            {
                std::clog << "*** info: skipping file '" << inputPath << "' (could not open file)\n" ;
            }
            
            else
            {
                std::string buff ;
                while( std::getline( f, buff ) )
                {
                    const auto sz = buff.size() ;
                    if( sz > 2 ) output << buff.substr( 0, sz - 2 ) ;
                    output << '\n' ;
                }
            }
        }
    }
}
Check if your program has permission to write in working directory.
@MiiNiPaa: Do you mean the security settings on windows explorer? Because it says it can so I guess that means it should be able to?

@JLBorges: I'm not too sure as to what your code is meant to do, could you explain it to me please?

At this point in time I think it would of been faster had I fixed my data by hand lol. But I do want to try to fixing it so that when I get new data it'll just fix it automatically
> I'm not too sure as to what your code is meant to do, could you explain it to me please?

If you compile it and run it (along with your current main()), does it open the file for output? Do you get
*** info: trying to open output file 'output.txt'
*** info: opened file 'output.txt' for output
...


Or do you see:
*** info: trying to open output file 'output.txt'
*** error: could not open output file
...


(I'll post an annotated version of the function in a while.)
I'm getting a lot of errors when i try to compile it, I'm not sure as to what is wrong though. What debugger are you using JLBorges?

As I said before I'm using Visual Studios, though it has caused me more grief than help.
Really thank you all though for your suggestions thus far
> I'm getting a lot of errors when i try to compile it

Yeah, I made a mistake.

Change #include <cstring> to #include <string> and it should compile cleanly.
http://rextester.com/ZJUDA32633
Yeah I'm getting error

*** info: trying to open output file 'output.txt'
*** error: could not open output file
...


hmmmmmmm
Make sure that the file 'output.txt' does not have the read-only attribute set.

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

const int NUMBER_OF_FILES = 21;

void cleanData()
{
    //file names
    const std::string outFileName = "output.txt";
    const std::string inFileName = "input_";
    const std::string inFileExt = ".txt";

    std::clog << "*** info: trying to open output file '" << outFileName << "'\n" ;
    std::ofstream output( outFileName, std::ios::out | std::ios::app ); // constructor opens the file

    if( !output.is_open() )
    {
        // std::cerr writes (typically error messages) to the standard error stream stderr (unbuffered)
        // https://en.wikipedia.org/wiki/Stderr
        std::cerr << "*** error: could not open output file\n" ;
    }

    else
    {
        // std::clog too writes (typically informational messages) to the standard error stream stderr (buffered)
        std::clog << "*** info: opened file '" << outFileName << "' for output\n" ;

        for( int i = 1; i <= NUMBER_OF_FILES; i++ )
        {
            // std::to_string() converts a number to a std::string
            // http://en.cppreference.com/w/cpp/string/basic_string/to_string
            const std::string inputPath = inFileName + std::to_string(i) + inFileExt ;
            std::ifstream f(inputPath) ; // constructor opens the file for input

            if( !f.is_open() )
            {
                std::clog << "*** info: skipping file '" << inputPath << "' (could not open file)\n" ;
            }

            else
            {
                std::string buff ;
                while( std::getline( f, buff ) ) // for every line in the file
                {
                    const auto sz = buff.size() ; // number of characters in the line
                    if( sz > 2 ) // if the line contains three or more characters
                        output << buff.substr( 0, sz - 2 ) ; // write the substring without last two characters 
                        // http://en.cppreference.com/w/cpp/string/basic_string/substr
                    
                    output << '\n' ; // write a new line
                }
            }
        }
    }
}
This is so weird, it's still not wanting to work. I just downloaded the boost library and also eclipse so i'm gonna give those a go.
Is the file 'output.txt' open in another program?
Are you able to delete the file 'output.txt'? (after having made a copy of it first.)
>>Is the file 'output.txt' open in another program?
Not that I know of, if I make a different solution it still doesn't work. I only have one solution that is actually printing something out but it's in a completely different directory and is not related to this solution.

>>Are you able to delete the file 'output.txt'? (after having made a copy of it first.)
Issue is, it doesn't even appear in my directory to begin with so there isn't anything for me to delete. Even if I make a text file with the same name, nothing happens to it.
What happens if you give the complete path to the file. For instance:

1
2
// replace with the actual complete path
const std::string outFileName = "E:\\my_code\\vs_projects\\clean_data\\clean_data\\output.txt";
Same error still unfortunately :(
maybe get rid of const from the file declarations
Pages: 12