C++ help- my code reads from small files but not large

  • Context: C/C++ 
  • Thread starter Thread starter davidsmith315
  • Start date Start date
  • Tags Tags
    C++ Code files
Click For Summary

Discussion Overview

The discussion revolves around a C++ programming issue related to reading a large CSV file containing date, time, and wind speed data. The participant experiences problems when processing a file of approximately 6,000kb, while smaller files work correctly. The conversation includes troubleshooting strategies and suggestions for debugging.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Experimental/applied

Main Points Raised

  • One participant reports that their code works with smaller files but fails to read every second date and time entry in a larger file.
  • Another participant suggests testing the code with a smaller subset of the file to identify potential errors.
  • A participant notes that the code fails with half of the input file but works with a quarter, indicating a possible issue with larger data sets.
  • Suggestions are made to run the code on different machines or split the file to find the boundary where it fails.
  • Concerns are raised about potential memory leaks due to object creation within loops without proper deletion.
  • A participant proposes refactoring the code to improve readability by moving line processing into a subroutine, but the original poster struggles to implement this without losing data output.
  • Several participants recommend using debugging tools like GDB or Microsoft Visual C++ Express to identify where the failure occurs in the code.

Areas of Agreement / Disagreement

Participants generally agree that the issue may relate to how the code handles larger files, but there is no consensus on the exact cause or solution. Multiple competing views on debugging strategies and potential code improvements are presented.

Contextual Notes

Limitations include the original poster's novice programming status, which may affect their ability to implement suggested changes or debugging techniques. There are also unresolved questions about the specific nature of the data in the larger file that could be causing the issue.

Who May Find This Useful

Individuals interested in C++ programming, particularly those dealing with file I/O operations and debugging techniques, may find this discussion relevant.

davidsmith315
Messages
3
Reaction score
0
Hello, I'm trying to make part of a code that reads a csv file with data containing the date and time, and the wind speed at that time, then does various output things.
When I copy and paste 1000 entriesinto a csv file and ru it, it works great, but if I use the file that's about 6,000kb it misses every second date and time part out, code anyone help me with this?
I'm a complete novice to programming- this is part of my Electrical Engineering course.

heres my code:
Code:
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <ctime>
#include <FileHC.h>

using namespace std;

int main ()
{	  ofstream outFile("C:\\WindSpeedAnswer.csv");
  if (!outFile) {
	  cout<< "error opening file"<<endl;
	  return -1;
  }
  
  
    ifstream inFile ("C:\\Copy of Kirkwall_23_hm_proc15.csv");
    string line;
    int linenum = 0;
   if (inFile.is_open())
  {
	  double randomMax;
	  double randomMin;
	  cout<<"would you like to add a random varience, enter 'yes' if you do "<<endl;
	 string Question;
	cin >> Question;
	string answerYes= "yes";
	if (Question==answerYes){
	cout<<"enter the max value of random varience "<<endl;
	cin>>randomMax;
	randomMax=randomMax-1;

	
	cout<<"enter the min value of random varience "<<endl;
	cin>>randomMin;
	randomMin=randomMin-1;}

	while (getline (inFile, line))
    {
        linenum++;
       // cout << "\nLine #" << linenum << ":" << endl;
        istringstream linestream(line);
        string item;
        int itemnum = 0;
		
        while (getline (linestream, item, ','))
        {
            itemnum++;
			double WindSpeed=0;
		string DateAndTime;
	
              //cout << "Item #" << itemnum << ": " << item << endl;
			if (itemnum%2 == 1) {
		
			DateAndTime=item;

				
		string replace= "-99999";	while(DateAndTime==replace){
			DateAndTime= "Data Missing";}

	cout<<"at "<<DateAndTime<<endl;
		outFile<< DateAndTime<<","	;}
			
		
	if (itemnum%2 == 0) {
		
	WindSpeed=atof(item.c_str());
	if(WindSpeed==-99999){
			
	WindSpeed=9;
			}
	if (Question==answerYes){
	for(int index=0; index<itemnum; index++){
	double X = randomMax+ rand() * (randomMax - randomMin) / RAND_MAX;
		if (randomMin<X<randomMax){
		WindSpeed=X;}
			}}
cout<<"the wind speed is "<<WindSpeed<<","<<endl;
		
outFile<<WindSpeed<<","<<endl;
//outFile.write(WindSpeed); 
			}
		}
			}
		
	 inFile.close();
 }

  else cout << "Unable to open file"; 
  

  
	  outFile.close();
  
  char stopchar;
  cin>>stopchar;
  return 0;
}
 
Last edited by a moderator:
Technology news on Phys.org
Here's something to try: See if it still doesn't work when you only take the first 100 lines or so of the 6,000kb file. If it still doesn't work, check the file for errors. It's much easier to check a 100-line file, and it's quite possible that there are a few thingies that make your program go weird.
 
Re Hobin:
Thanks fo reply
It work great if i take the firs 100 lines, or 1000 lines- its only when I run the 6,oookb file it doesntwork
 
davidsmith315 said:
It work great if i take the firs 100 lines, or 1000 lines- its only when I run the 6,oookb file it doesntwork

That's odd. To be perfectly honest, I can't find anything in your code that would make it stop working at some arbitrary amount of data - have you tried running your code on a different machine? Try splitting the file in the middle (if that's possible), if it works, take 3/4 of the file, if it doesn't, take 1/4 - repeat until you find the boundary where it stops working.
 
Sounds like you have a memory leak somewhere, for example objects are being created inside the loop, but never deleted.

For better readability, I would put the treatment of each complete line (everything inside the while(getline) loop) into a subroutine.
 
Thanks for replies, I tried using half the input file and it failed, but using 1/4 f it worked perfectly.

Re M Quck- "For better readability, I would put the treatment of each complete line (everything inside the while(getline) loop) into a subroutine"
Sorry to ask but how would I go about doing this?
I tried this:
if (itemnum%2 == 1) {

DateAndTime=item;
string replace= "-99999";
while(DateAndTime==replace){
DateAndTime= "Data Missing";}
count<<"at "<<DateAndTime<<endl;
}
if (itemnum%2 == 0) {
WindSpeed=atof(item.c_str());
if(WindSpeed==-99999){
WindSpeed=9;
}
if (Question==answerYes){
for(int index=0; index<itemnum; index++){
double X = randomMax+ rand() * (randomMax - randomMin) / RAND_MAX;
if (randomMin<X<randomMax){
WindSpeed=X;}
}}

count<<"the wind speed is "<<WindSpeed<<","<<endl;

outFile<<DateAndTime<<","<<WindSpeed<<","<<endl;



It nearly worked- except it left out the time and date part- the time and date row was blank, but the WindSpeed row was output perfectly
 
davidsmith315 said:
Thanks for replies, I tried using half the input file and it failed, but using 1/4 f it worked perfectly.

Very good, now try (3/4)/2 part of the file. :wink:

I hope you see where I'm going with this. By cutting what's left in half every time, you could theoretically very quickly go to the point where you come to the specific line where your file fails to work properly. Going to that extreme is probably unnecessary, but as you 'zoom in' on the part where it fails to work, take a very good look at possible anomalies in the file.
 
Couldn't you use a source level debugger to see where the failure occurs? If you have windows, you can get microsoft visual c++ express for free, and it includes a source level debugger.
 
^That, too. Alternatively, if you use GCC, you can obviously download and use the GDB.
 
  • #10
Whenever this happens for me, I just use GDB and run the code with the proper arguments and print the stack trace to see where the fault occurred.

Doing what the previous user suggested above in terms of essentially binary searching for the line where the code stops at is helpful but won't solve your problem if the issue is a memory leak. That style of debugging will only be helpful if that's the line of input error.

GDB will tell you if the input is faulty or if you have a memory leak. Also consider running your executable with memcheck to see if you have memory leaks.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K