Simple File I/O problem

This is a discussion on Simple File I/O problem within the C++ Programming forums, part of the General Programming Boards category; The problem is simple enough... Here the "read" part of my program.. Code: #include <cstdlib> #include <iostream> #include <fstream> using ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    4

    Simple File I/O problem

    The problem is simple enough...

    Here the "read" part of my program..
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int array[100];
        ifstream file ("in.in");
        
        if(file.is_open()) 
        {
         int x=0;                                     
         while(!file.eof())
         {
          file>>array[x];
          x++;
          cout<<*array<<endl;
         }
         file.close();
         
         cout<<*array;
        }
        
        else
            cout<<"Unable to open file!"<<endl;
             
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Well my problem is how to transfer all of the data to my array, what I'm doing really is just copying the first line. Thanks in advance.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, the problem is (mostly) with your output... read on:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    /*
     * don't worry about declaring the variables if you don't intend to use them.
     */
    int main()
    {
    	int array[100];
    	fstream file ("test.dat",std::ios::in);
        
    	/*
    	 * I'd do something like this so that you don't have to wrap all
    	 * your code in an if statement
    	 */
    	if(!file.is_open()) 
    	{
    		std::cout<<"Unable to open file!"<<std::endl;
    		exit(1);
        	}
    	
        	int x=0;        
    	/*
    	 * you should generally use a read to control a loop - check the FAQ
    	 * to find out why
    	 */
    	while(file>>array[x])
        	{
    		/*
    		 * your main problem was here - you were outputting the pointer
    		 * to the first element of the array over and over again.
    		 * instead of
           		 * cout<<*array<<endl;
    		 * you should have
    		 */
    		cout<<array[x]<<endl;
    		x++;
         	}
         	file.close();
         
    	/*
    	 * you had an output here that would have tried accessing uninitialized
    	 * data if I had left it in and modified it to work.
    	 */
    	  
    	/*
    	 * avoid using system() whenever you can - it makes your code non-
    	 * portable, and much worse, opens up a huge security vulnerability
    	 */
    	std::cout<<"Press ENTER to Continue..."<<std::endl;
    	std::cin.get();
    
    	/*
    	 * also, avoid using macros if possible.  Return '0' instead of
    	 * EXIT_SUCCESS
    	 */
    	return 0;
    }
    Last edited by major_small; 01-07-2006 at 03:43 AM. Reason: cleaning up after syntax highlighter
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    Thanks that did the trick....

    Sorry about the simple mistakes I'm kinda new to FILE I/O and some of the things there were automatically put in since I was using Dev-C++ 4.9.9.2

    Thanks again

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    As long as we're cleaning up the code a little, I'd add one more check. This:
    Code:
    while(file>>array[x])
    Should maybe be this:
    Code:
    while(x < 100 && file>>array[x])
    This ensures that we don't accidentally try to read in too much data (more than the array can handle).
    I used to be an adventurer like you... then I took an arrow to the knee.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Binary file I/O
    By Tankists in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2009, 10:02 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 03:32 PM
  3. File I/O problem
    By dacbo in forum C Programming
    Replies: 4
    Last Post: 01-17-2006, 07:22 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. simple File I/O question
    By bobnet in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2003, 06:53 AM

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