CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2013
    Location
    Langrood, Iran
    Posts
    7

    Problem With read Function

    Here is My Code:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    
    class Student
    {
    public:
    	Student(string name = "", int num = 0, int unit = 0)
    	{
    		setName(name);
    		this->num = num;
    		this->unit = unit;
    	}
    
    	void set(string name = "", int num = 0, int unit = 0)
    	{
    		setName(name);
    		this->num = num;
    		this->unit = unit;
    	}
    
    	void setName(string n)
    	{
    		int len = n.length();
    		len = (len < 15) ? len : 14;
    		n.copy(this->name, len);
    		name[len] = '\0';
    	}
    
    	char * getName()
    	{
    		return name;
    	}
    
    	int getNum()
    	{
    		return num;
    	}
    
    	int getUnit()
    	{
    		return unit;
    	}
    
    private:
    	char name[15];
    	int num, unit;
    };
    
    int main()
    {
    	ofstream o("test.bin", ios::out| ios::binary);
    	ifstream f("test.bin", ios::in| ios::binary);
    	Student s;
    	Student st;
    	Student stu;
    	if(!f || !o)
    	{
    		cerr << "Error!";
    		exit(1);
    	}
    
    	string name;
    	int num, unit;
    
    	for(int i = 0;i < 50;i++)
    	{
    		o.write(reinterpret_cast<const char *>(&s), sizeof(Student));
    	}
    
    	cin >> name >> num >> unit;
    
    	while(num > 0 && num <= 50)
    	{
    		st.set(name, num, unit);
    		o.seekp((st.getNum() - 1) * sizeof(Student));
    		o.write(reinterpret_cast<const char *>(&st), sizeof(Student));
    		cin >> name >> num >> unit;
    	}
    
    	f.read(reinterpret_cast<char *>(&stu), sizeof(Student));
    
    	while(f.good())
    	{
    		if(stu.getNum() != 0 )
    		{
    			cout << "Name : " << stu.getName() << endl;
      		}
    
    		f.read(reinterpret_cast<char *>(&stu), sizeof(Student));
    	}
    
    	f.close();
    
    	return 0;
    }
    My Problem is When I Write n Student's information in file, Read Function only read n - 1 information from file! for example if I enter this input:

    1 1 1
    2 2 2
    -1 -1 -1

    Output is :
    Name : 1
    Last edited by N@R!MAN; July 21st, 2013 at 04:50 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Problem With read Function

    Quote Originally Posted by N@R!MAN View Post
    My Problem is When I Write n Student's information in file, Read Function only read n - 1 information from file! for example if I enter this input:

    1 1 1
    2 2 2
    -1 -1 -1

    Output is :
    Name : 1
    Which "Output" do you mean? Where is it in your code?
    Did you debug your code?
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2013
    Location
    Langrood, Iran
    Posts
    7

    Re: Problem With read Function

    Which "Output" do you mean? Where is it in your code?
    Did you debug your code?
    of Course!
    after I read information from file :
    Code:
    		if(stu.getNum() != 0 )
    		{
    			cout << "Name : " << stu.getName() << endl;
      		}

  4. #4
    Join Date
    Jun 2013
    Location
    Langrood, Iran
    Posts
    7

    Re: Problem With read Function

    I used o.flush() after writing process and mu problem is solved now!!! it was not read function problem!
    tnx

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Problem With read Function

    Just as a comment. Why open the same file twice - once as an input stream, and once as an output stream? Why not open the same file just once for both input and output? You then haven't got to remember to flush the contents of the write stream to make the contents available to the read stream?

    Code:
    fstream io("test.bin", ios:: out | ios:: in | ios:: binary);
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured