473,595 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reads input endlessly

54 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. size_t maxArraySize = 1000;
  2.  
  3. int main (int argc, char* argv[])
  4. {
  5.     char* a [maxArraySize];
  6.     unsigned int count = 0;
  7.  
  8.     if (argc < 3) //checks to make sure user enter input/output files
  9.     {
  10.         std::cout << "Enter two files for input and output!" << std::endl;
  11.         std::cout << "Example: stringsort.x infile outfile. Exiting..." << std::endl;
  12.         exit(1); //exits if input/output file not entered
  13.     }
  14.  
  15.     char buffer[256];
  16.     std::ifstream inFile(argv[1]);
  17.     while (!inFile.eof()) && count < maxArraySize)
  18.     {
  19.         inFile.getline (buffer,100);
  20.         a[count] = buffer;
  21.         count++;
  22.     }
  23.  
  24.     school::g_merge_sort(a, a + count);
  25.  
  26.     unsigned int i = 0;
  27.     std::ifstream outFile (argv[2]);
  28.  
  29.     if (! outFile.is_open())
  30.     { 
  31.       std::cout << "Error opening file"; 
  32.       exit (1); 
  33.      }
  34.  
  35.      while (! outFile.eof() )
  36.      {
  37.        std::cout << a[i] << std::endl;
  38.        ++i;
  39.      }
  40.         return 0;
  41. }
Jan 6 '10 #1
1 1584
myusernotyours
188 New Member
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.
Jan 6 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
2822
by: patrickshroads | last post by:
I am running a profiler trace against a database and noticed that the reads column always shows 0. When running the same trace against another machine I get back values in the reads column. I took a query that profiler reported as having 0 reads and ran in in query analyzer wtih STATISTICS IO on and confirmed that there are in fact reads: Table 'tt_cawardalloc'. Scan count 1, logical reads 8, physical reads 0, read-ahead reads 1. Table...
4
2763
by: VivekR | last post by:
Hi, Consider the program snippet below main { .... printf("Enter Choice: "); int choice = getche();
3
1572
by: serge | last post by:
How do I determine which method I should use if I want to optimize the performance of a database. I took Northwind's database to run my example. My query is I want to retrieve the Employees' First and Last Names that sold between $100,000 and $200,000. First let me create a function that takes the EmployeeID
4
3548
by: serge | last post by:
I am running a query in SQL 2000 SP4, Windows 2000 Server that is not being shared with any other users or any sql connections users. The db involves a lot of tables, JOINs, LEFT JOINs, UNIONS etc... Ok it's not a pretty code and my job is to make it better. But for now one thing I would like to understand with your help is why the same SP on the same server and everything the same without me changing anything at all in terms of SQL...
7
12597
by: kvnsmnsn | last post by:
I've got two GIF files named "Tigger.gif" and "Angry_barbarian.gif" that contain animations that I want to display on a web page. I've written the following XHTML code which works for a fraction of a sec- ond but no more. That is, the animations run for a very short period of time and then stop. My guess is that they're running through their short animated period and then stopping. How can I display animations like this that keep...
3
1273
by: SimonSiz007 | last post by:
warp.class comes with 'Applet Effects Factory' and I want to use it to warp an image on mouse-over. The problem is, it loops endlessly. What's a clean way to have it run only while the mouse is over the hotspot? Here's the script that 'Applet Effects Factory' generates: <APPLET CODE="warp.class" WIDTH=215 HEIGHT=108> <PARAM NAME="GENERATOR" VALUE="CREATED WITH THE APPLET EFFECTS FACTORY WWW.COFFEECUP.COM"> <PARAM NAME="GENERAL"...
4
1690
by: Tarique | last post by:
Hello All. Is the following good enough to be a safe user input routine? What else should i do to improve it ? #include<stdio.h> #include<stdlib.h> #include<string.h> #define ARRAYSIZE 100 /*Clear the input stream before any input */
28
2429
by: n00m | last post by:
Both codes below read the same huge(~35MB) text file. In the file 1000000 lines, the length of each line < 99 chars. Stable result: Python runs ~0.65s C : ~0.70s Any thoughts?
5
3524
by: Yash | last post by:
Hi, I am using SQL 2000 SP4. I have compared 2 scenarios: Scenario 1: insert into #bacs_report SELECT ..... WHERE <conditions>
0
7957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8262
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8252
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6675
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5421
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1491
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.