[C++/bin_files] a li'l problem.

Started by
6 comments, last by Thirthe 17 years, 2 months ago
So why is there a hiccup at " dat_in.get((char*)&st,sizeof(int));"?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int st;
    
    int n;
    int i;
    
    ofstream dat_out;
    ifstream dat_in;
    
    n= 8;

    dat_out.open("dat.txt", ios::binary);
      if (!dat_out.is_open()){
         cout << "Unable to open file1";
         dat_out.close();
      }
    dat_in.open("dat.txt", ios::binary);
      if (!dat_in.is_open()){
         cout << "Unable to open file2";
         dat_in.close();
      }
      
      
    for(i=0; i<n; i++){
       st= i+i;
      cout<< st<< "\t";
    dat_out.write((char*)&st, sizeof(int));         
    }
    
       
    cout<<endl;
    st= -1;
    
    for(i=0; i<n; i++){
            dat_in.seekg(i*sizeof(int));
            cout<< "tellg: " <<dat_in.tellg()<< endl;

            dat_in.get((char*)&st,sizeof(int));
            cout<< st<< "\t";
    }
                 cout<<endl;
    
    dat_in.close();
    dat_out.close();
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



PS.: i found this program that supposedly works, but it does not :|


#include <iostream.h>
#include <fstream.h>

void main()
{
    int a,b;
    fstream dat("dat.dat",ios::in|ios::out|ios::binary); 
    
	for (int i=1;i<=10;i++)
    {
        a = i * i;
        dat.write ((char *)&a,sizeof(a));
    }

    dat.seekg(0);
    dat.read((char *)&b,sizeof(int));
    cout << "b = " << b << endl;

    dat.seekg(5*sizeof(int));
    dat.read((char *)&b,sizeof(int));
    cout << "b = " << b << endl;

    dat.read((char *)&b,sizeof(int));
    cout << "b = " << b << endl;
    dat.close();

}

// 1  4  9  16  25  36  49  64  81  100



[Edited by - Thirthe on February 14, 2007 4:12:45 AM]
Advertisement
What do you mean hickup? Was there a compiler error? Did you suddenly start getting the hiccups and had to get a glass of water or something? Did the program run and crash on that line?

Same questions for the 2nd program.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

initial glance don't know the problem ... but perhaps your data has a byte that is the same as the delimiter (\n) ... so perhaps you should be using read() instead of get()? I've never used get() for binary data before, not sure if it is appropriate ... in the ref it says it is used for "unformatted" data, but I think it's still meant to be data that is character in nature (aka text format not binary).

Also, not a bug but a recommendation. Do not use sizeof(int) because its just a mainenence bug waiting to get you (when you change to float, longlong, double, etc) ... use sizeof(variable_name).
the problem at the 1st program is that the read() function doesn't store the data in the variable.

and the same is at the 2nd program, it's like the read() function isn't even used :/
it does nothing!

Xai:
oh, i thought i uploaded the previous verstion, which was where i used the read() function.

well, since that didn't work i tried get(), but still the problem occurs.
the only difference at the output is that the variable 'st' has the value -256 when i used get(), and when i used read() it has -1.

the value of tellg() is 0 at first and from there on -1.

don't know if this helps, but i'm trying to convert this pascal program to c++:
program files;uses crt;var f             :file of integer;    i,sv,nv,faktor:integer;begin  ClrScr;     Assign(f,'a:\st.dat');  Reset(f);  Write('Enter factor : ');  ReadLn(faktor);       For i:=0 To FileSize(f)-1 Do  Begin    Seek(f,i);          Read(f,sv);         WriteLn('Old value : ',sv);     nv:=sv*faktor;      Seek(f,i);          Write(f,nv);        WriteLn('New value : ',nv);    End;  Close(f);  ReadLn;End.
Perhaps the problem isn't with individual lines of code, but the overall processing. What I'm suggesting is maybe read is returning nothing in some case because either the file is empty, or at the time it returns nothing the file contents have already been read or skipped past.

To verify this you can create a little 3 liner program that does nothing but open a file, read 1 thing out of a file and diplays it ... to test your read line. Assuming it works ... then start looking for logical flaws.

Also I really don't get the seek calls.

while(file not eof)
{
read(&item, sizeof(item));
display(item);
}

should work as is. (of course that's psuedocode, but you get the idea.
it's more or less the same.
it goes in the loop only once and it returns nothing.
it outputs the last value that the variable had.

but the file _HAS_ stuff in it. i also checked the file itself:
    for(i=0; i<n; i++){       st= i+i;      cout<< st<< "\t";    dat_out.write((char*)&st, sizeof(st));    }


** it seems the problem was with having an ifstream and ofstream over one file, so i used fstream to do both.
but the problem remains :S

[Edited by - Thirthe on February 14, 2007 1:15:38 PM]
the only way I can be of any more help is to either get detailed information about what's happening (tell me the actual output when you run it) - or get your test data files, so I can run it myself (or ideally both).
well since i suck at explaining and whatnot, i decided to downgrade the assignment to use all char type.

so i did and the program works as i imagined it would.
now i'm going to build on it.

thanks for all of the advices!

This topic is closed to new replies.

Advertisement