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

by davidsmith315
Tags: c++, csv file, large
 Share this thread:
 P: 3 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 thats 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: #include #include #include #include #include #include #include using namespace std; int main () { ofstream outFile("C:\\WindSpeedAnswer.csv"); if (!outFile) { cout<< "error opening file"<> Question; string answerYes= "yes"; if (Question==answerYes){ cout<<"enter the max value of random varience "<>randomMax; randomMax=randomMax-1; cout<<"enter the min value of random varience "<>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 "<>stopchar; return 0; }
 P: 194 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.
 P: 3 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
P: 194
C++ help- my code reads from small files but not large

 Quote by davidsmith315 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.
 P: 662 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.
 P: 3 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";} cout<<"at "<
P: 194
 Quote by davidsmith315 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.

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.
 HW Helper P: 7,109 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.
 P: 194 ^That, too. Alternatively, if you use GCC, you can obviously download and use the GDB.
 P: 27 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 thats 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.

 Related Discussions Math & Science Software 2 Programming & Computer Science 0 Math & Science Software 2 Academic Guidance 8 Academic Guidance 3