File I/O

This is a discussion on File I/O within the C++ Programming forums, part of the General Programming Boards category; Having not done much file i/o, Im working with a student who is trying to read from a file, write ...

  1. #1
    Registered User zerodevide's Avatar
    Join Date
    Apr 2003
    Posts
    6

    File I/O

    Having not done much file i/o, Im working with a student who is trying to read from a file, write to the file, and later be able to re-read the file. We can write as many times as we want, but after the first read no further reads can be done (until the program is terminated and restarted). Testing shows that the file is able to be opened for reading (InArray.is_open()), but !InArray or InArray.fail() throws an error. Ideas?

    IDE = MS VC++ 6.0 /Win2K

    Code:
    Code:
    //arrays
    //takes in 5 names and prints to screen
    
    
    # include <iostream>
    # include <iomanip>
    # include <fstream>
    		
    using namespace::std;
    
    typedef char Q[26];
    typedef char filenamep [8];
    
    int main()
    {
    	int i=0, count=0;
    	char choice1;
    	ifstream InArray;
    	ofstream OutArray;
    	Q string [200];
    	filenamep filenameo;	//elegant
    	filenamep filenames;
    	int salary [200]={0};
    	
    	cout <<"Would you like to begin a new table or open an old one?"<<endl<<"Type n for new or o for open."<<endl;
    	cin >> choice1;
    	
            switch (choice1){
    	case 'o':
    	case 'O':
    	cout << "What file would you like to open?\n  Must be 8 charecters or less\n";
    	cin >> filenameo;
    		
    	InArray.open (filenameo);
    	if (!InArray)
    	{
    	cout << "File could not be opened." << endl;
    	cout << InArray.is_open()<<"  "<<OutArray.is_open() << endl; 
    	}
    	else 
    	{
    	int c=0;
    	while (InArray)
    	{
                    	InArray.get (string[c],26);
    		InArray >>(salary[c]);
    		InArray.ignore ('\n');
    		cout << c+1 << ")  " << setw(26) << string[c]<< "     $" << salary [c] <<endl<<endl;
    		c++;
    		i++;
    	}
    	i--;
    	cout << InArray.is_open()<<"  "<<OutArray.is_open() << endl;
    	}
    	InArray.close();
    	break;
    			 
    	case 'n':
    	case 'N':{
    	cout << "How many names are you going to enter? (between 1 and 200)" << endl;
    	cin >> i;
    	cout << "Please input each name (less than 25 charecters) and press return, \nthen input their salary." << endl;
    	for (int count=0; count<=i-1; count++)
                    	{
    		cin >> string [count];
    		cin >> salary [count];	
    		}
    		break;
    	}
    	}
    	int whilecount=0, num=0;
    	char choice;
    	while (whilecount<=0)
    	{
    	cout << "Would you like to view the list, modify the list, save the list, open and existing list, or quit the program?" <<endl;
    		
    	cout << "Type v for view, m for modify, s for save, o for open or q for quit." << endl;
    	cout << InArray.is_open()<<"  "<<OutArray.is_open() << endl;
    	cin >> choice;
    	
    	switch (choice)
    		{
    		case'v': 
    		case'V':
    		cout<<endl<<endl;
    		for (count=0; count<=i-1; count++)
    		cout << count+1 << ")  " << setw(26) << string[count]<< "     $" << salary [count] <<endl<<endl;
    		break;
    					
    		case'm':
    		case'M':
    	                 cout<<endl<<endl;
    		 for ( count=0; count<=i-1; count++)
    		 cout << count+1 << ")  " << setw(26) << string[count]<< "     $" << salary [count] <<endl<<endl;
    		 cout << "Which # would you like to modify?" << endl;
    		 cin >> num;
    		 cout << string[num-1] << "     $" << salary [num-1] << endl;
    		 cout << "Please enter the new values."<< endl;
    		 cin >> string [num-1];
    		 cin >> salary [num-1];
    		 cout <<"The new values are:" <<endl;
    		 for ( count=0; count<=i-1; count++)
    		    cout << count+1 << ")  " << setw(26) << string[count]<< "     $" << salary [count] <<endl<<endl;
    		 break;
    			
    			
    		case'q': 
    		case'Q':
    		  cout << "Have a nice day" <<endl;
    		  whilecount++;
    		  break;
    		
                                    default:
    		  cout << "Try again." <<endl<<endl;
    		  break;
    
    
    		case 'o':
    		case 'O':
                                        cout << "What file would you like to open?\n  Must be 8 charecters or less\n";
    		    cin >> filenameo;
    
    			
    		    InArray.open (filenameo);
    		    if (InArray.fail())
    		       {
    		        cout << endl<<"File could not be opened." <<endl<< endl;
    		        cout << InArray.is_open()<<"  "<<OutArray.is_open() << endl; 
    		        }
    		     else 
    		         {
    		          int i=0;
    		          while (InArray)
    			{
    			InArray.get (string[i],26);
    			InArray >>(salary[i]);
    			InArray.ignore ('\n');
    			cout << i+1 << ")  " << setw(26) << string[i]<< "     $" << salary [i] <<endl<<endl;
    			i++;
    			}
    		i--;
    		cout << InArray.is_open()<<"  "<<OutArray.is_open() << endl;
    		}
    		InArray.close();
    		//overwrite whatever is in memory
    		break;
    
    		case 's':
    		case 'S':
                                        cout << "What would you like to save your file as?\n  Must be 8 charecters or less.\n";
    		    cin >> filenames;
                                        OutArray.open (filenames);
                                        if (!OutArray)
    		         {
    		          cout << "File could not be opened." << endl;
    		          cout << InArray.is_open()<<"  "<<OutArray.is_open() << endl; 
    		           }
    		     else
    		         {
    		          for ( count=0; count<=i-1; count++)
    			 OutArray << setw(25) << string[count]<< salary [count] << "         "<< endl;  //width is one less than saved
    			cout << InArray.is_open()<<"  "<<OutArray.is_open() << endl;
    			}
    			OutArray.close();
    			break;
    		}
    	}
    return 0;
    }
    Last edited by zerodevide; 04-14-2003 at 05:38 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    There's an internal file pointer which marks where you're reading from in the stream. The current position is returned by the tellg() function for input stream objects. You can define a new position in the stream with the seekg() function for input stream objects. Once you've read to the end of the file, you have to reset the internal pointer if you want to read again. To set it at the beginning of the file do this:

    inFile.seekg(0);


    "but !InArray or InArray.fail() throws an error."

    What error? Post the error message and put a comment in your code where the error occured. !InArray and InArray.fail() are equivalent and both return true if any file error occured.
    Last edited by 7stud; 04-14-2003 at 08:58 PM.

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    in addition to what was said above, the seekg() is to seek with the 'get' pointer for reading. to move the 'put' pointer (for writting) use seekp()

  4. #4
    Registered User zerodevide's Avatar
    Join Date
    Apr 2003
    Posts
    6
    Originally posted by 7stud
    There's an internal file pointer which marks where you're reading from in the stream. ......
    inFile.seekg(0);


    "but !InArray or InArray.fail() throws an error."

    What error? Post the error message and put a comment in your code where the error occured. !InArray and InArray.fail() are equivalent and both return true if any file error occured.
    Based on the code:
    Code:
           if (InArray.fail())
    				{
    					cout << endl<<"File could not be opened." <<endl<< endl;
    we get the "File could no be opened." which means InArray.fail() is returning true. The interesting side is that the following line:

    Code:
    cout << InArray.is_open()<<"  "<<OutArray.is_open() << endl;
    shows a 1 for InArray.is_open() and a 0 for OutArray.is_open(), which means the file IS open for reading according to the system, but the program can't access it. After the pass and we close the file we get 0 0 for InArray.is_open()<<" "<<OutArray.is_open(),
    so it's being closed roperly. <sigh>

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    interesting... stick a call to perror("") inside the if statement to see exactly what the problem with the file is

    Code:
    #include <stdio.h>
    ...
    if (!InArray)
    {
        cout << "File could not be opened." << endl;
        cout << InArray.is_open()<<"  "<<OutArray.is_open() << endl;
        perror(""); 
    }
    ...
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  6. #6
    Registered User zerodevide's Avatar
    Join Date
    Apr 2003
    Posts
    6
    Originally posted by *ClownPimp*
    interesting... stick a call to perror("") inside the if statement to see exactly what the problem with the file is

    Code:
    #include <stdio.h>
    ...
    if (!InArray)
    {
        cout << "File could not be opened." << endl;
        cout << InArray.is_open()<<"  "<<OutArray.is_open() << endl;
        perror(""); 
    }
    ...
    perror returned "no error" - which goes with the InArry.is_open() returning a 1 (which means it is open) <sigh>

  7. #7
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    is it the second 'open()' call that is giving the error? you are closing inArray once then trying to open it later in the program. try leaving it open the entire time and just closing it at the end of the program (or when you are completely done with it)

  8. #8
    Registered User zerodevide's Avatar
    Join Date
    Apr 2003
    Posts
    6
    Originally posted by Perspective
    is it the second 'open()' call that is giving the error? you are closing inArray once then trying to open it later in the program. try leaving it open the entire time and just closing it at the end of the program (or when you are completely done with it)
    Tried that, but didn't seem to make a difference, plus the writting part (since we're writing to the same file were reading from - basic read and update) complains if we don't close the read before the write.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:56 AM

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