Reads input endlessly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • randysimes
    New Member
    • Oct 2009
    • 54

    Reads input endlessly

    I have a program to read in words and sort those words into lexicographical order. The problem I have is that it reads the input file endlessly. I know it does not reach the output section because the output file is not created.

    Code:
    size_t maxArraySize = 1000;
    
    int main (int argc, char* argv[])
    {
    	char* a [maxArraySize];
        unsigned int count = 0;
        
    	if (argc < 3) //checks to make sure user enter input/output files
    	{
    		std::cout << "Enter two files for input and output!" << std::endl;
    		std::cout << "Example: stringsort.x infile outfile. Exiting..." << std::endl;
    		exit(1); //exits if input/output file not entered
    	}
    	
    	char buffer[256];
    	std::ifstream inFile(argv[1]);
    	while (!inFile.eof()) && count < maxArraySize)
    	{
    		inFile.getline (buffer,100);
    		a[count] = buffer;
    		count++;
    	}
    
    	school::g_merge_sort(a, a + count);
        
    	unsigned int i = 0;
    	std::ifstream outFile (argv[2]);
     
        if (! outFile.is_open())
        { 
          std::cout << "Error opening file"; 
          exit (1); 
         }
    
         while (! outFile.eof() )
         {
           std::cout << a[i] << std::endl;
    	   ++i;
         }
        	return 0;
    }
  • myusernotyours
    New Member
    • Nov 2007
    • 188

    #2
    First, it's not correct to infer that since the output is not created, your program reads the input file endlessly.

    Other things would cause it not to be created. At a glance, you should not use ifstream for the output file. You use ofstream or simply fstream for a read/write file.

    Also it's not nessesary to check for eof on an output file. You simply don't care since you should never get to eof. If you are paranoid, maybe the disk could fill up but that's different.

    Then you are never writting to the output file. What you have is writting to standard output. You need to provide the output file stream instead of std::cout when writting the output.

    Kind Regards,

    Alex.

    PS. Why your program seems endless is because of the last loop there. The will never be an eof on that stream there.

    Comment

    Working...