GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-May-2009, 01:02
nanchuangyeyu nanchuangyeyu is offline
New Member
 
Join Date: Apr 2009
Posts: 15
nanchuangyeyu has a little shameless behaviour in the past

A problem with file I/O


Hi,
What I actually want to do is really quite simple,that is, convert all the data(of short type)in a file to their abstract values respectively.But I did not accomplish it. Here my code is listed:
CPP / C++ / C Code:
#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

int main()
{
	char *in_name = "predicted_file.pre";
	char *out_name = "abstracted_file.pre";
	short i;
	short i_abs;
	ifstream in_file;
	in_file.open(in_name,ios::binary);
	ofstream out_file;
	out_file.open(out_name,ios::binary | ios::app);

	while (!in_file.eof())
	{
		in_file.read(reinterpret_cast<char*>(&i),sizeof(short));
		if (in_file.eof())
			break;
		i_abs = abs(i);
		out_file.write(reinterpret_cast<char*>(&i_abs),sizeof(short));

	}
	in_file.close();
	out_file.close();
	cout<<"Completed!"<<endl;
	return 0;
}

The program can be compiled without error but it seemed to be trapped in a dead loop(a time too long for me to be tolerant,exactly) and the output file turned out to be very large in stead of being the same size as input file when I terminated the program by force. Anybody can tell me where I made mistakes and give me some clue for a effctive solution?Thank u in advance.I use VS 2005 if that makes any difference.
nanchuangyeyu
  #2  
Old 16-May-2009, 04:46
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 285
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: A problem with file I/O


Quote:
Originally Posted by nanchuangyeyu
...and the output file turned out to be very large in stead of being the same size as input file
This should give some kind of a hint, no? The program is clearly writing something. How about the reading part? Checking if the files actually WERE opened could also be very helpful. If you added such checks, you could find out your program cannot open 'predicted_file.pre'. Some weird encoding madness or the file simply doesn't exist?

After fiddling around, my guess is your 'predicted_file.pre' resides in the same folder with the executable and you're running your program from the VC++ debug menu or something. The thing seems to be, though, that it doesn't look for the files there. With my project name being 'absvalue', it looked for the files in the absvalue\absvalue directory. It also wrote the output file there. This wasn't any indication to you? Running the .exe manually works like you'd expect it to.

If this wasn't the case, then I don't know.
  #3  
Old 16-May-2009, 04:53
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: A problem with file I/O


You probably need to take a closer look at how you're going about debugging your code. Are you verifying that your input is what you expect? What output (if any) is being generated?

I didn't have your input file, so I created one:

CPP / C++ / C Code:
#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

void create_predicted_file()
{
    short value = 0xFFFF;
    ofstream out_file("predicted_file.pre", ios::binary | ios::app);
    if(out_file.is_open()) {
        while(value != 0) {
            out_file.write(reinterpret_cast<char*>(&value), sizeof(short));
            --value;
        }
        out_file.close();
    }
}

int main() {
    char* in_name  = "predicted_file.pre";
    char* out_name = "abstracted_file.pre";
    short value, abs_val;
    ifstream in_file;
    in_file.open(in_name, ios::binary);
    ofstream out_file;
    out_file.open(out_name, ios::binary | ios::app);

    if(in_file.is_open() && out_file.is_open()) {
        while(!in_file.eof()) {
            if(!in_file.eof() && in_file.good()) {
                in_file.read(reinterpret_cast<char*>(&value), sizeof(short));
                abs_val = (short)abs(value);
                out_file.write(reinterpret_cast<char*>(&abs_val), sizeof(short));
            }
        }
        in_file.close();
    }
    else {
        create_predicted_file();
    }
    out_file.close();
    cout<<"Completed!"<<endl;
    return 0;
}


Code:
$ xxd predicted_file.pre | head 0000000: ffff feff fdff fcff fbff faff f9ff f8ff ................ 0000010: f7ff f6ff f5ff f4ff f3ff f2ff f1ff f0ff ................ 0000020: efff eeff edff ecff ebff eaff e9ff e8ff ................ 0000030: e7ff e6ff e5ff e4ff e3ff e2ff e1ff e0ff ................ 0000040: dfff deff ddff dcff dbff daff d9ff d8ff ................ 0000050: d7ff d6ff d5ff d4ff d3ff d2ff d1ff d0ff ................ 0000060: cfff ceff cdff ccff cbff caff c9ff c8ff ................ 0000070: c7ff c6ff c5ff c4ff c3ff c2ff c1ff c0ff ................ 0000080: bfff beff bdff bcff bbff baff b9ff b8ff ................ 0000090: b7ff b6ff b5ff b4ff b3ff b2ff b1ff b0ff ................

Code:
$ xxd abstracted_file.pre | head 0000000: 0100 0200 0300 0400 0500 0600 0700 0800 ................ 0000010: 0900 0a00 0b00 0c00 0d00 0e00 0f00 1000 ................ 0000020: 1100 1200 1300 1400 1500 1600 1700 1800 ................ 0000030: 1900 1a00 1b00 1c00 1d00 1e00 1f00 2000 .............. . 0000040: 2100 2200 2300 2400 2500 2600 2700 2800 !.".#.$.%.&.'.(. 0000050: 2900 2a00 2b00 2c00 2d00 2e00 2f00 3000 ).*.+.,.-.../.0. 0000060: 3100 3200 3300 3400 3500 3600 3700 3800 1.2.3.4.5.6.7.8. 0000070: 3900 3a00 3b00 3c00 3d00 3e00 3f00 4000 9.:.;.<.=.>.?.@. 0000080: 4100 4200 4300 4400 4500 4600 4700 4800 A.B.C.D.E.F.G.H. 0000090: 4900 4a00 4b00 4c00 4d00 4e00 4f00 5000 I.J.K.L.M.N.O.P.


...I know that it must be considerably laborious for you to use spaces between arguments, but a bit of whitespace does improve readability in code.

You'll note also that I changed your "i" variable name to "value." Try to use 'i' 'j' 'k' etc, for loop control variables and name "real variables" something other than a single letter.


MxB
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CD Burner Help - Power Calibration Error.... JonBoy420 Computer Hardware Forum 111 19-Feb-2009 04:54
bool problem in multi file format pfanning C++ Forum 6 08-Mar-2008 18:45
How to use the data obtain in one C file to another C file? TommyC C Programming Language 12 15-Jan-2008 10:51
Problem write to file Peter_APIIT C++ Forum 6 11-May-2007 00:05
File function jamesbond000 C Programming Language 3 28-Mar-2007 23:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:36.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.