I am learning how to do file input/output, because I am using OCR and I am scanning multi-lingual text, and I want to be able to go in with a utility and change certain characters so they will print out correctly.
The code I am using is listed below, the present problem is when I check for file size, it always reads zero, so no file ever exists. I got most of this code from "Input/Output with files" on this site at:
http://www.cplusplus.com/doc/tutorial/files.html
So I am not quite sure what I am doing incorrectly. I am using Visual Studio 2008 for this, as a console program. Here is the code:
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;
using namespace System;
// create a file, then copy it to a folder
int main () {
char ch;
ofstream startfile;
ofstream copyfile;
long begin,end;
filebuf *inbuf, *outbuf;
// create a text file
startfile.open ("C:/temp/test.txt");
startfile << "Writing this to file.\n";
// test for open file
if (startfile.is_open()){ // verify file is open.
cout << "File is open\n";
}
// find length of file
ifstream myfile ("C:/temp/test.txt");
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
startfile.close();
cout << "size is: " << (end-begin) << " bytes.\n";
// length of file show as zero in console
// If LOF is zero, it must not be writing to the file.
// open new file and copy test.txt contents to new file
startfile.open ("C:/temp/test.txt");
copyfile.open ("C:/temp/result.txt");
inbuf=startfile.rdbuf();
outbuf=copyfile.rdbuf();
ch = inbuf->sgetc();
while ( ch != EOF){
//int x;
outbuf->sputc (ch);
cout << ch; //<< hex << x++;
ch= inbuf->snextc();
}
copyfile.close();
startfile.close();
return 0;
}
When this runs, I get: "File is open" "Size is: 0 bytes.", but no file shows on the directory, and, the file obviously should not be zero bytes.