Looks like I let that slip through the cracks...that said I had tried that syntax in an earlier version and I was getting errors about it needing to have a constant value at compile. I think I was trying to see if I would get a different error or anything that would shine some light on why the compiler couldn't understand that the buffer size was to be determined by the size of the infile given by the line
long size = infile.get();
. I expected problems at some point but it seemed no matter what I did, Visual Studio wasn't at all having it. It was also pitching a fit about using
strtok(postbody, " !.*'/");
not allowing me to compile with the demand I use strtok_s only to find out it won't accept the same arguments passed and isn't portable across all platforms. I had to include a preprocessor directive to forcibly ignore the compiler error after trying several workarounds. My computer isn't available to me right now but later I can share the error VS was giving me. I am pretty sure I got something messed up but don't know what yet.
edit: here is the first version of the source
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
|
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdio.h>
#include <new>
#pragma warning (disable : 4996)
int main( char argc, char* argv[])
{
using namespace std;
string ofileap;
string infileap;
cout << "Please enter absolute path to input file." << endl;
cin >> infileap;
ifstream infile;
infile.open(infileap, ios::in);
if (infile.is_open())
{
infile.seekg (0, infile.end); //streambuffer seek position; in this case the entire file
long size = infile.get(); //
char* buffer = new char [size];
infile.read(buffer, size);
cout << "Input file opened successfully. Creating output file..." << endl;
ofstream outfile;
cout << "Please enter a name for output file, absolute path optional: " << endl;
cin >> ofileap;
outfile.open(ofileap, ios::app);
if (outfile.is_open())
{
char * postbody{ buffer };
char * ptrbody = { strtok(postbody, " !.*'/") };
cout << "Enumerating words..." << endl;
while (ptrbody != NULL)
snprintf(ptrbody, *buffer, "%s\n" );
outfile.write(buffer, size);
delete[] buffer;
outfile.close();
infile.close();
}
else cout << "Output file failed to open.";
}
else cout << "I/O Failed.";
return 0;
}
|
char * postbody was being passed the arguments [buffer, size] which was causing the compiler errors. I put it inside braces and it compiled...not sure how well it will run though.
2nd edit: upon execution, program hangs briefly. After entering path to the input file, debug returns this error:
Unhandled exception at 0x00007FFD0D45A388 in ConsoleApplication1.exe: Microsoft C++ exception: std::bad_array_new_length at memory location 0x000000958ECFF6F0. occurred