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.
#include <sstream>
#include <iostream>
#include <string.h>
#include <fstream>
constint 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;
}
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_";
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
@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
>>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.