File input/output

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.







The code with problems runs fine by me. Maybe the location is wrong? If the program is inside the same folder as the textfile, you only have to write the name of that textfile, eg test.txt. If that works, your location is wrong.

Btw, you don't need the variable 'begin', since this should always be zero. This means that end-begin==end. Another thing, wich you probebly know: the number of characters isn't the same as the size in bytes.
Thanks for swift the reply . . . I found that part of the problem was I should be using \\, instead of \. After that I was able to verify that the file was being created in the temp directory by opening it and seeing the test in it.
The only bug left is the file size, which does not seem to be working.

My code looks like this now, but it is throwng an error.

// ModifyFile.cpp : main project file.
#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 << "Wow, it worked!\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 = startfile.tellg();
startfile.seekg (0, ios::end);
end = startfile.tellg();
startfile.close();
cout << "size is: " << end << " bytes.\n";
return 0;
}



This throws no error, but stills gives zero size for the file. ( renamed the temp folder, as it should be in lower case) It's a lot easier on the eyes too, less code!

// ModifyFile.cpp : main project file.
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;

// create a file, then copy it to a folder
int main () {
ofstream startfile;
ofstream copyfile;
int length;
char * buffer;
// create a text file
startfile.open ("C:\\temp\\test.txt");
startfile << "Wow, it worked!\n";
// test for open file
if (startfile.is_open()){ // verify file is open.
cout << "File is open\n";
}
ifstream is;
is.open ("C:\\temp\\test.txt", ios::binary );
// get length of file:
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);
cout << "size is: " << length << " bytes.\n";

return 0;
}


There is a problem with the OP's original code, in that he opens a file for
writing, then writes something to the file, then opens the file for reading
without closing the file first.

he should really:
1. open file for writing.
2. write to file.
3. CLOSE file (to ensure data is flushed out to the file)
4. Open file for reading.
.....
Last edited on
I tried the last suggestion, to no avail, still file size of zero. In fact, if I look at the file after doing it this way, there is nothing in the file!

// ModifyFile.cpp : main project file.
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;

// create a file, then copy it to a folder
int main () {

ofstream startfile;
ofstream copyfile;
int length;
// create a text file
startfile.open ("C:\\temp\\test.txt");
startfile << "Wow, it worked!\n";
// test for open file
startfile.close();
if (startfile.is_open()){ // verify file is open.
cout << "File is open\n";
}
startfile.open ("C:\\temp\\test.txt");
if (startfile.is_open()){ // verify file is open.
cout << "File is open\n";
}
ifstream is;
is.open ("C:\\temp\\test.txt", ios::binary );
// get length of file:
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);
cout << "size is: " << length << " bytes.\n";
startfile.close();
return 0;
}
Last edited on
I'm not 100% sure of it yet, but I think this works . . . The cause of the problem, as best I can tell with my previous coding was on the line:

ofstream startfile;

Which I changed to:
fstream startfile;

Strange as it may sound, I tried to change the line:

ifstream is;

to
fstream is;

And it did not work, I understand that ofstream and ifstream are combine
together in fstream, so all you need is #include <fstream>, but I don't understand what the heck the difference is regarding the failed previously mentioned change. . . . ! Anyone understand that one?

I have programmed in Assembly, Visual Basic and MEL scripting, but I just started Visual C++. I like it because it is a powerful language, but I still need to do a lot of work to get fluent in the language!


// ModifyFile.cpp : main project file.
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;

// create a file, then copy it to a folder
int main () {

fstream startfile;
int length;
startfile.open ("C:\\temp\\test.txt");
startfile << "It worked! I think. . .\n" << endl;

// test for open file
if (startfile.is_open()){ // verify file is open.
cout << "File is open\n";
}

ifstream is;
is.open ("C:\\temp\\test.txt", ios::binary );
// get length of file:
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);
cout << "size is: " << length << " bytes.\n";
cout.flush();
startfile.close();
}
Last edited on
Topic archived. No new replies allowed.