File I/O - displaying output properly

This is a discussion on File I/O - displaying output properly within the C++ Programming forums, part of the General Programming Boards category; hi, I'm trying to find out how I go about properly displaying data from a file like this; Code: #include ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55

    File I/O - displaying output properly

    hi,

    I'm trying to find out how I go about properly displaying data from a file like this;

    Code:
    #include <sys/stat.h>
    struct stat results;
    
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    class Data
    {
    public:
    	int iInt1;
    };
    
    int main()
    {
      Data xData;
      xData.iInt1 = 3;
     	
      char *filename = "Reading&WritingComplexData1.txt";
      fstream file (filename, ios::out|ios::in);
    
      file.write((char*)&xData, sizeof(xData));
    
      if (stat(filename, &results) == 0)
         results.st_size;
    
      int size = results.st_size;
    
       if(  file.is_open())
          {
    	 cout<<"file is open"<<endl;
    
    	 char *memblock = new char[size];
    	 file.seekg(0,ios::beg);
    	 file.read(memblock, size);
    
    	 file>>memblock;
    	 cout<<memblock<<endl; //trouble with this part, displaying the data properly?
    
         }
    	cin.get();
    }
    If I only use 1 or 2 char members it displays the file as is, but soon as I use anything more (int, string etc) it changes. I'm guessing some special handling/conversion must take place somewhere at some point, I just don't know how/where.

    Most exercises/examples I've come across don't really go into the how really, with cases like this (i.e classes). Just wondering if someone could provide a workable example or (practical,not theory) material that helps explain this.


    Thanks!
    Last edited by Tropod; 10-13-2009 at 04:32 AM. Reason: I like correcting myself :)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What do you mean by "1 or 2 char members"? You can't do both file.read and file >> memblock at the same time, really, since what's the point?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    yeah opps, realised later the inclusion of file>>memblock; may of been mistake.

    1 or 2 char members;

    Code:
    class Data
    {
    public:
    	int iInt1;
    	char cChar1;
    	char cChar2;
    	char *pChar3;
    	string szChar;
    	double value;
    };
    
    //etc
    Data xData;
    
            xData.iInt1 = 1;
    	xData.cChar1 = 'a';
    	xData.cChar2 = 'b';
    	xData.pChar3 = "xyz";
    	xData.szChar = "this is a string";
    //etc
    And save this data to a file; but what I'm trying to do is retrieve this saved data back from a file and display it properly. That's where I'm having trouble (see first post). Trying to understand how to do this properly, but all the text/examples/exercises I've come across thus far don't go into enough detail in this regard (specifically with classes?).


    Thanks.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you write (in binary form) a pointer, then all you will get is the value of the pointer, not the string. You will have to write the contents of the pointer (plus the null character or some other I'm-done-here signal) at the minimum. Reading that file back in could then be a little obnoxious, since you won't know ahead of time how big your string needs to be -- all you can do is read a character at a time until you find your I'm-done-here signal and stop.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    Quote Originally Posted by tabstop View Post
    If you write (in binary form) a pointer, then all you will get is the value of the pointer, not the string. You will have to write the contents of the pointer (plus the null character or some other I'm-done-here signal) at the minimum. Reading that file back in could then be a little obnoxious, since you won't know ahead of time how big your string needs to be -- all you can do is read a character at a time until you find your I'm-done-here signal and stop.

    Thank you very much for your reply! Though it wasn't (exactly) what I was hoping for, it most definitely helped!!

    I can see that I was perhaps going about it incorrectly, the wrong way &/or didn't have all the info I needed. Your comment though really helped me help myself after spending some more time trying to understand these particular aspects.


    I managed to figure this out (I hope) & this is what I managed to come up with;
    (I'm guessing there'd be a more better/efficient way to do something like this ?)


    Code:
    #include <sys/stat.h>
    struct stat results;
    
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    class Data
    {
    public:
    	char		m_Member1;
    	int		m_Member2;
    	int		m_Member3;
    	char		m_Member4;
    	string	m_Member5;
    	bool		m_Member6;
    };
    
    int main()
    {
    	int pos = 0;
    	
    	Data xData;
    	xData.m_Member1 = 'a';
    	xData.m_Member2 = 123;
    	xData.m_Member3 = 789;
    	xData.m_Member4 = 'z';
    	xData.m_Member5 = "This is a string";
    	xData.m_Member6 = 1;
    
    	Data *yData;// = new Data[10];
     	
    	char *filename = "Reading&WritingComplexData1.txt";
    	fstream file (filename, ios::out|ios::in | ios::binary); 
    	file.write((char*)&xData, sizeof(xData));
    
    	if(  file.is_open())
    	{
    		cout<<"File is open."<<endl;
    
    		file.seekp(pos);
    		file.seekg(pos);
    		file.read((char*)&yData,sizeof(char)); //for some reason though this now creates a warning, but it didn't before.
    		cout<<(char)yData<<endl;
    		
    		pos = 4;
    		file.seekp(pos);
    		file.seekg(pos);
    		file.read((char*)&yData,sizeof(int));
    		cout<<(int)yData<<endl;
    
    		pos = 8;
    		file.seekp(pos);
    		file.seekg(pos);
    		file.read((char*)&yData,sizeof(int));
    		cout<<(int)yData<<endl;
    
    		pos = 12;
    		file.seekp(pos);
    		file.seekg(pos);
    		file.read((char*)&yData,sizeof(char)); //warning r.e. same with last char
    		cout<<(char)yData<<endl;
    
    		pos = 16;
    		file.seekp(pos);
    		file.seekg(pos);
    		file.read((char*)&yData,sizeof(string));
    		cout<<(char*)yData<<endl;
    
    		pos = 20;
    		file.seekp(pos);
    		file.seekg(pos);
    		file.read((char*)&yData,sizeof(bool));
    		cout<<(bool)yData<<endl;
    
    	}
    cin.get();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21