Thread: reading & writing to a file

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    reading & writing to a file

    When I run this program I get the following run time error:

    Code:
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <limits>
    using namespace std;
    
    class Matrix
    {
    	int row,column;
    	double** matrix;
    public:
    	Matrix(int,int);
    	Matrix(int,int,FILE *);//loading matrix from file
    	~Matrix();
    	void load_matrix();
    	void save_matrix();
    	void show_matrix();
    };
    Matrix* load_from_file();
    Matrix::Matrix(int r,int c):row(r),column(c)
    {
    	matrix=new double*[column];
    	for(int i=0;i<row;i++)
    		matrix[i]=new double[row];
    	for(i=0;i<row;i++)
    		for(int j=0;j<column;j++)
    			matrix[i][j]=0;
    }
    Matrix::Matrix(int r,int c,FILE *fp):row(r),column(c)
    {
    	Matrix::Matrix(r,c);
    	for(int i=0;i<r;i++)
    		for(int j=0;j<c;j++)
    			fread(&matrix[i][j],sizeof(int),1,fp);
    	fclose(fp);
    }
    Matrix::~Matrix()
    {
    	for(int i=0;i<row;i++)
    		delete matrix[i];
    	delete [] matrix;
    }
    
    void Matrix::load_matrix()
    {
    	cout<<"Enter elements:"<<endl;
    	for(int i=0;i<row;i++)
    		for(int j=0;j<column;j++)
    			cin>>matrix[i][j];
    }
    
    Matrix* load_from_file()
    {
    	FILE *fp;
    	string s;
    	int r,c;
    	cout<<"Enter file name to load: ";
    	getline(cin,s);
    	fp=fopen(s.c_str(),"rb");
    	if(!fp)
    	{
    		cout<<"Error opening file!";
    		return 0;
    	}
    	fread(&r,sizeof(int),1,fp);
    	fread(&c,sizeof(int),1,fp);
    
    	Matrix* pM=new Matrix(r,c,fp);
    	return pM;
    	
    }
    void Matrix::save_matrix()
    {
    	string s;
    	FILE *fp;
    	cout<<"Enter file name to save!";
    	getline(cin,s);
    	fp=fopen(s.c_str(),"wb");
    	if(!fp)
    	{
    		cout <<"error";
    		return;
    	}
    	fwrite(&row,sizeof(int),1,fp);
    	fwrite(&column,sizeof(int),1,fp);
    	for(int i=0;i<row;i++)
    		for(int j=0;j<row;j++)
    			fwrite(&matrix[i][j],sizeof(int),1,fp);
    	fclose(fp);
    
    }
    void Matrix::show_matrix()
    {
    	cout <<"the following matrix was loaded"<<endl;
    	for(int i=0;i<row;i++,cout<<endl)
    		for(int j=0;j<column;j++)
    			cout<<matrix[i][j]<<" ";
    }
    int main()
    {
    	int choice;
    	cout<<"Choose hoe to load matrix"<<endl;
    	cout<<"1. Load from file!"<<endl<<"2. Enter manualy"<<endl;
    	
    	do
    	{
    		cout<<"Your choice: ";
    		cin>>choice;
    			if(cin.fail())
    			{
    				cin.clear();
    				cin.ignore(numeric_limits<int>::max(), '\n');
    				cout << "\nInput Invalid. Please Try Again: "<<flush;
    			}
    	}while(choice !=1 && choice !=2);
    	if(choice==1)
    	{
    		Matrix *pk=load_from_file();
    		pk->show_matrix();
    	}
    	else
    	{
    		int r,c;
    		cout<<"Enter number of rows an columns!";
    		cin>>r;
    		cin>>c;
    		Matrix m(r,c);
    		m.load_matrix();
    		m.show_matrix();
    		//m.save_matrix(); causing run-time error
    	}
    }
    Debug assertion failed ....
    File fopen.c
    line:55

    Expression *file !=_T('\0');

    Same thing happens when I choose choice 1 in programs menu

    I don't know what is causing error. I know that is when I try to open file (just before that line).
    Maybe use of string an c_str() in fopen
    is bad or something.
    Thanks
    Last edited by Micko; 05-08-2004 at 08:57 AM.

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    I figure out why this is happening.
    getline(cin,s) is not wise way to enter name of file.
    Because, it seems that '\n' is stay in inputstream and getline picks up it before waiting input from user.
    I solved this by typing:
    Code:
    in.clear();
    	cin.ignore(80,'\n');
    	getline(cin,s);
    But here's the question: how big number to use ( I use 80)in ignore?
    Is there a better way to read line?
    In good old C flsuh would solve this

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by Micko
    I figure out why this is happening.
    getline(cin,s) is not wise way to enter name of file.
    Because, it seems that '\n' is stay in inputstream and getline picks up it before waiting input from user.
    I solved this by typing:
    Code:
    in.clear();
    	cin.ignore(80,'\n');
    	getline(cin,s);
    But here's the question: how big number to use ( I use 80)in ignore?
    Is there a better way to read line?
    In good old C flsuh would solve this
    C's fflush isn't defined for input streams. Probably the best size to use for cin.ignore is the maximum size of the streamsize type.
    Code:
    #include <limits>
    
    [...]
    
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Thanks God some answered, I thouhgt there's a boycott going on!
    By the way is limits best way.
    That means I must include onother .h file with few more 100 lines of code in my program

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Hey Kip, weren't you at the meeting, were supposed to have a boycot going on here! Anyway, I like numeric_limits rather than the macros precisely because you can lookup the real limit past potentally many typedefs. For cin, even with redirection, it's proabaly overkill, but it always works. For this case unless you want leading whitespace in your filename then what I would recommend is

    std::getline(std::cin>>std::ws, s);

    This will skip leading whitespace, while allowing you to have a filename with spaces in it (yechhh). std::cin >> s will force a non-null string, but the string will end at the first whitespace.

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Binary Tree - Reading From and Writing to a File
    By Ctank02 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2008, 09:22 PM
  4. Reading out of and writing into the same file
    By Wiretron in forum C Programming
    Replies: 8
    Last Post: 12-30-2006, 02:04 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM