My Code Only Reads the First Line of the File C++

#ane

  • D.I.C Head

Reputation: 42

  • View blog
  • Posts: 225
  • Joined: 26-October xi

only reading in first line of input

Posted 27 Feb 2012 - 09:08 PM

Howdy. I am working on a project for my cs250 class and I cannot figure out where I am going wrong. The program is meant to read in a set number of URL'southward and count the hits. After counting all of the hits, it prints them out to a file. But When I try to run the program. It seems that information technology just does the beginning line. I must utilise an array of structs in society to do this. The struct include both the URL and the number of hits for the said URL. It besides says that I may need to make my own addInOrder and binarySearch Methods. I am pretty sure that my binary search method works, merely I have business organization over my addInOrder method.

Here is my code:

counthits.cpp(master commuter program)

#include <cstdlib> #include <iostream> #include <string> #include <fstream>   #include "histogram.h"   using namespace std;   // Read log entries from the provided input stream, extracting the request // portion from each. If the request is a GET, excerpt the locator (throwing // away any part later on a '?') and update a count of how many times that locator // has appeared. // // Write the resulting wlocators and counts (in alphabetic guild past locator) //  in CSV format to the output stream. // // - Assume that the input contains a maximum of MaxPages singled-out locators. //   If more distinct locators than this are actually //   encountered, write goose egg to the output stream but print an error //   bulletin on the standard error stream. void histogram(const int MaxPages, istream& input, ostream& output);    int main (int argc, char** argv) {   if (argc != four)     {       cerr << "Usage: " << argv[0] << " MaxPages inFileName outFileName" << endl;       return -one;     }    // Get MaxPages from the command line   int MaxPages = atoi(argv[1]);    // Take input and output file names from the command line   ifstream in (argv[ii]);   ofstream out (argv[3]);   histogram (MaxPages, in, out);   in.close();   out.close();    render 0; }            

histogram.cpp (The part that needs fixing is at the bottom of this office)

#include "histogram.h" //#include "arrayUtils.h" #include <cord>   using namespace std;     struct CountedLocations {     string Location;     int numHits; };         // Extract the quoted request from a line containing a log entry string extractTheRequest(string logEntry) {   string::size_type requestStart = logEntry.find('"');   string::size_type requestEnd = logEntry.rfind('"');   if (requestStart != requestEnd)     {       render logEntry.substr(requestStart+i, requestEnd-requestStart-1);     }   else     return ""; }   template <typename T> void addElement (T* array, int& size, int index, T value) {   // Make room for the insertion   int toBeMoved = size - 1;   while (toBeMoved >= index) {     array[toBeMoved+1] = array[toBeMoved];     --toBeMoved;   }   // Insert the new value   array[alphabetize] = value;   ++size; }     int binarySearch(const CountedLocations list[], int listLength, cord searchItem) {     int starting time = 0;     int last = listLength - 1;     int mid;     string midFind;       bool institute = false;       while (first <= last && !found)     {         mid = (get-go + final) / ii;         midFind = list[mid].Location;         if (midFind == searchItem)             found = true;         else             if (searchItem < midFind)                 last = mid - 1;             else                 beginning = mid + 1;     }       if (plant)         return mid;     else         render -ane; }         // Check to meet if this is a GET request bool isAGet (cord request) {   render request.size() > iii && request.substr(0,4) == "Go "; }     // Extract the locator part of a Become asking string extractLocator (string asking) {   // strip off the Go   cord::size_type locatorStart = iii;     // Skip any blanks   while (request[locatorStart] == ' ')     ++ locatorStart;     // The locator ends at the first bare or ? subsequently that.   string::size_type locatorEnd = locatorStart;   while (request[locatorEnd] != ' ' && request[locatorEnd] != '?')     ++ locatorEnd;     string locator = request.substr(locatorStart, locatorEnd-locatorStart);   render locator; }   //template <typename T> int addInOrder (CountedLocations assortment[], int& size, string value) {   // Make room for the insertion   int toBeMoved = size - 1;   while (toBeMoved >= 0 && value < array[toBeMoved].Location) {     array[toBeMoved+1] = array[toBeMoved];     --toBeMoved;   }   // Insert the new value   array[toBeMoved+1].Location = value;   ++size;   render toBeMoved+1; }       // Read log entries from the provided input stream, extracting the request // portion from each. If the request is a GET, excerpt the locator (throwing // away any role subsequently a '?') and update a count of how many times that locator // has appeared. // // Write the resulting wlocators and counts (in alphabetic order by locator) //  in CSV format to the output stream. // // - Assume that the input contains a maximum of MaxPages distinct locators. //   If more distinct locators than this are really //   encountered, write zilch to the output stream but print an error //   message on the standard error stream. void histogram(const int MaxPages, istream& input, ostream& output) {     // Footstep 1 - prepare the information     // Data should be stored in two parallel arrays, locators and counts.     // locators[i] volition be the i_th locator string and counts[i] will exist the     // count of how many times that particular locator has been requested.      CountedLocations C [MaxPages];     int nLocators = 0;       // Step 2 - read and count the requested locators     string logEntry;     getline (input, logEntry);     while (input)     {                  string request = extractTheRequest(logEntry);         if (isAGet(asking))         {             string locator = extractLocator(request);             int position = binarySearch (C, nLocators, locator);             if (position >= 0)             {                 // Nosotros constitute this locator already in the assortment.                 // Increment its count                 C[position].numHits+=1;             }             else             {                 if (nLocators < MaxPages)                 {                     // This is a new locator. Add together it.                     position = addInOrder (C, nLocators, locator);                     // And add a count of one at the corresponding position                     // in the counts array                     //int nCounts = nLocators - 1;                     C[position].numHits = 1;                     //addElement (CountedLocations, nCounts, position, 1);                 }                 else                 {                     // Not plenty room in the arrays                     ++nLocators;                 }             }         }         getline (input, logEntry);     }       // Step 3 - write the output report     string Location;     int Hits;     if (nLocators <= MaxPages)     {         for (int i = 0; i < nLocators; ++i)         {             Location = C[i].Location;             Hits = C[i].numHits;         }         output << "\"" << Location << "\"," << Hits << endl;     }     else     {         cerr << "Input file contains more than " << MaxPages << " locators." << endl;     }   // Step 4 - cleanup }            

This is my input:
70.161.31.lxxx - - [xxx/Mar/2008:23:31:21 -0500] "Get /cocoon/~cs330web/forum/show/_axle/individual.gif HTTP/1.1" 302 -
lxx.161.31.80 - - [thirty/Mar/2008:23:31:21 -0500] "Go /cocoon/~cs330web/forum/show/_axle/feed-icon16x16.png HTTP/1.1" 302 -
70.161.31.80 - - [30/Mar/2008:23:31:21 -0500] "Get /axle/Forum/_axle/private.gif HTTP/1.ane" 304 -
lxx.161.31.80 - - [xxx/Mar/2008:23:31:21 -0500] "Go /axle/Forum/fckeditor/editor/fckeditor.html?InstanceName=text&Toolbar=ForumToolbar HTTP/1.1" 304 -
seventy.161.31.80 - - [xxx/Mar/2008:23:31:21 -0500] "Go /axle/Forum/fckeditor/fckconfig.js HTTP/one.i" 304 -
127.0.0.i - - [30/Mar/2008:23:31:28 -0500] "Get / HTTP/1.0" 200 2499
127.0.0.1 - - [thirty/Mar/2008:23:31:29 -0500] "GET / HTTP/ane.0" 200 2499


Is This A Good Question/Topic? 0

  • +

#two bodom658 User is offline

Reputation: 114

  • View blog
  • Posts: 1,123
  • Joined: 22-Feb 08

Re: only reading in first line of input

Posted 27 February 2012 - 09:xx PM

Can you lot elaborate on 'just does the first line'? What kind of debugging accept you attempted?

#3 jdavi134 User is offline

  • D.I.C Head

Reputation: 42

  • View blog
  • Posts: 225
  • Joined: 26-October eleven

Re: only reading in first line of input

Posted 27 February 2012 - 09:24 PM

View Postbodom658, on 27 February 2012 - 09:twenty PM, said:

Can you elaborate on 'only does the first line'? What kind of debugging have you lot attempted?

The just does the kickoff line means that information technology seems that the plan only does the get-go line of the input. As in after information technology reads in, counts the hits, and outstreams the offset line of the input, it just stops. Is there something wrong with my while loop used in the histogram function?

And this is to be executed through the use of the command line, and debugging he has not taught united states of america yet.

#iv bodom658 User is offline

Reputation: 114

  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: only reading in showtime line of input

Posted 27 February 2012 - 09:37 PM

Debugging in this instance can be very unproblematic, as uncomplicated as a print argument in your while loop to indicate each pass and maybe print the parsed asking cord.

You seem to be utilizing getline correctly, though traditionally it would be recommended to utilise the member function of the istream object itself, i.e. input.getline(logEntry);

Perhaps you lot tin can try some of the in a higher place and cheque your input files (paying attention to line endings perhaps). Remember to not try to do everything at once though.

EDIT: See the post beneath mine for a skillful case, this is sometimes known as the "RMS style debugging" though, in the long run, I'd recommend learning how to employ GDB

This post has been edited by bodom658: 27 Feb 2012 - 09:45 PM

#five jimblumberg User is online

Reputation: 5916

  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: but reading in first line of input

Posted 27 February 2012 - 09:44 PM

Have you tried printing out your information a different parts of your program using cout? For instance in your histogram function:

              cord logEntry;     getline (input, logEntry);     while (input)     {        std::cout << logEntry << std::endl; // Add together this to see if you read the file properly.          string request = extractTheRequest(logEntry);         std::cout << asking << std::endl;  // Add this to encounter if you extracted the request properly.            

Using cout is a simple debugging tool that can aid locate where the problem occurs.

View Postbodom658, on 27 February 2012 - ten:37 PM, said:

You seem to be utilizing getline correctly, though traditionally it would be recommended to utilize the fellow member part of the istream object itself, i.e. input.getline(logEntry);

There are 2 versions of getline() one that works with std::string and one that works with a C-string. You seem to be referring to the C-string version although you have left off the size of the C-string. The OP is using the right version for std::strings.

Jim

This post has been edited by jimblumberg: 27 February 2012 - 09:45 PM

#vi jdavi134 User is offline

  • D.I.C Caput

Reputation: 42

  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: only reading in get-go line of input

Posted 27 February 2012 - 09:45 PM

View Postjimblumberg, on 27 February 2012 - 09:40 PM, said:

Have you lot tried printing out your information a unlike parts of your plan using cout? For example in your histogram office:

                string logEntry;     getline (input, logEntry);     while (input)     {        std::cout << logEntry << std::endl; // Add this to see if yous read the file properly.          cord asking = extractTheRequest(logEntry);         std::cout << asking << std::endl;  // Add this to see if y'all extracted the request properly.              

Using cout is a unproblematic debugging tool that can help locate where the trouble occurs.

Jim

I added these and have determined that it is my while loop that is not working. It still only prints out the outset line that the input has.

And now I actually become this compile mistake. actually this came in earlier adding your suggestions.

c:\programme files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\four.4.one\..\..\..\libmingw32.a(main.o):main.c|| undefined reference to `[email protected]'|
||=== Build finished: 1 errors, 0 warnings ===|

This post has been edited by jdavi134: 27 February 2012 - 09:46 PM

#7 jimblumberg User is online

Reputation: 5916

  • View blog
  • Posts: 17,932
  • Joined: 25-Dec 09

Re: only reading in kickoff line of input

Posted 27 February 2012 - 09:52 PM

For your mistake bulletin you need to insure that you are creating a console awarding. I believe that message indicates you are working with a win32 program.

When I added those cout statements above I did see that the loop was reading all the lines in the file. Are you sure your files are opening correctly? You lot should e'er insure that the files open up correctly and if they don't inform the user.

Also this line:

CountedLocations C [MaxPages];            

Causes an error in my compiler, C++ does not let variable length arrays. MaxPages must exist a compile time abiding, you are trying to employ a run time constant, C++ does not permit that.

Jim

This post has been edited past jimblumberg: 27 Feb 2012 - 09:56 PM

#8 bodom658 User is offline

Reputation: 114

  • View blog
  • Posts: one,123
  • Joined: 22-Feb 08

Re: only reading in start line of input

Posted 27 February 2012 - 09:53 PM

That error seems to suggest that there is a problem with your project setup (Did you do something odd to the file with your master office in it?)

And Jim, skillful to know about istream.getline, information technology'due south been a while since I've done file manipulation in C++ (normally, I'grand working with direct C, and half the time I'm opting for things like fwrite and fread over the normal conventions)

#9 jdavi134 User is offline

  • D.I.C Head

Reputation: 42

  • View blog
  • Posts: 225
  • Joined: 26-October eleven

Re: merely reading in first line of input

Posted 27 February 2012 - x:07 PM

View Postjimblumberg, on 27 February 2012 - 09:52 PM, said:

For your error message you need to insure that you are creating a console awarding. I believe that message indicates yous are working with a win32 program.

When I added those cout statements above I did see that the loop was reading all the lines in the file. Are you certain your files are opening correctly? You should always insure that the files open correctly and if they don't inform the user.

Besides this line:

CountedLocations C [MaxPages];              

Causes an error in my compiler, C++ does not allow variable length arrays. MaxPages must exist a compile time abiding, you lot are trying to utilise a run time constant, C++ does not allow that.

Jim

View Postbodom658, on 27 February 2012 - 09:53 PM, said:

That error seems to suggest that there is a problem with your project setup (Did you practise something odd to the file with your main role in it?)

And Jim, good to know about istream.getline, it'due south been a while since I've washed file manipulation in C++ (normally, I'm working with straight C, and half the time I'm opting for things like fwrite and fread over the normal conventions)

I have fixed that error. And information technology now compiles without mistake once more. But I still can't find why it does non read the residuum of the input. I don't run across anything wrong with it at all.

#10 jimblumberg User is online

Reputation: 5916

  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: only reading in offset line of input

Posted 27 February 2012 - 10:09 PM

Take yous checked to insure that the input file is opening correctly?

Jim

#eleven jdavi134 User is offline

  • D.I.C Head

Reputation: 42

  • View blog
  • Posts: 225
  • Joined: 26-October xi

Re: only reading in first line of input

Posted 27 February 2012 - 10:12 PM

View Postjimblumberg, on 27 February 2012 - 10:09 PM, said:

Accept you checked to insure that the input file is opening correctly?

Jim

Is there a way to do this?

#12 jimblumberg User is online

Reputation: 5916

  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: only reading in get-go line of input

Posted 27 February 2012 - 10:sixteen PM

Yeah, y'all check the status of your stream after you try to open information technology.

For case when yous open your files:

std::ifstream fin("YOURFILENAME"); if(!fin) { // File did not open.    // tell the operator, so you should probably quit your program. }            

Always bank check that the files opened correctly earlier you go along.

Jim

#xiii jimblumberg User is online

Reputation: 5916

  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: simply reading in first line of input

Posted 27 February 2012 - x:21 PM

Also note with the input y'all specified I get the post-obit output:

Quote

70.161.31.fourscore - - [30/Mar/2008:23:31:21 -0500] "GET /cocoon/~cs330web/forum/show/_axle/private.gif HTTP/1.one" 302 -

REQUEST GET /cocoon/~cs330web/forum/bear witness/_axle/private.gif HTTP/1.1

lxx.161.31.80 - - [thirty/Mar/2008:23:31:21 -0500] "Go /cocoon/~cs330web/forum/show/_axle/feed-icon16x16.png HTTP/1.1" 302 -

Request Get /cocoon/~cs330web/forum/show/_axle/feed-icon16x16.png HTTP/1.1

70.161.31.lxxx - - [30/Mar/2008:23:31:21 -0500] "Get /axle/Forum/_axle/private.gif HTTP/1.1" 304 -

REQUEST GET /beam/Forum/_axle/private.gif HTTP/i.1

lxx.161.31.lxxx - - [30/Mar/2008:23:31:21 -0500] "GET /axle/Forum/fckeditor/editor/fckeditor.html?InstanceName=text&Toolbar=ForumToolbar HTTP/i.one" 304 -

Asking Become /beam/Forum/fckeditor/editor/fckeditor.html?InstanceName=text&Toolbar=ForumToolbar HTTP/one.1

seventy.161.31.fourscore - - [30/Mar/2008:23:31:21 -0500] "Become /axle/Forum/fckeditor/fckconfig.js HTTP/i.ane" 304 -

REQUEST GET /axle/Forum/fckeditor/fckconfig.js HTTP/1.one

127.0.0.1 - - [30/Mar/2008:23:31:28 -0500] "GET / HTTP/ane.0" 200 2499

REQUEST Get / HTTP/1.0

127.0.0.ane - - [xxx/Mar/2008:23:31:29 -0500] "GET / HTTP/1.0" 200 2499

Request Become / HTTP/one.0

Jim

#14 bodom658 User is offline

Reputation: 114

  • View blog
  • Posts: 1,123
  • Joined: 22-Feb 08

Re: only reading in beginning line of input

Posted 27 February 2012 - ten:24 PM

Take a look at where you are writing your output, in step three. Look at where your output statement is and where you lot are iterating through what yous desire to write out.

It's a simple, silly answer, but it happens to all of us.

#fifteen jdavi134 User is offline

  • D.I.C Head

Reputation: 42

  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: only reading in first line of input

Posted 27 February 2012 - ten:45 PM

View Postbodom658, on 27 Feb 2012 - 10:24 PM, said:

Take a look at where you lot are writing your output, in step 3. Expect at where your output argument is and where you lot are iterating through what you want to write out.

Information technology'due south a simple, silly answer, only it happens to all of united states of america.

Wow. You're right, that was pretty ridiculous. Lol. Didn't put the output inside of the for loop.

Cheers very much! You lot just made my day!

oxfordfruck1988.blogspot.com

Source: https://www.dreamincode.net/forums/topic/268605-only-reading-in-first-line-of-input/

0 Response to "My Code Only Reads the First Line of the File C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel