![]() |
|
#1
|
|||
|
|||
More Reading in problemsOk I'm having problems reading in a file again. Its the same CD Database but I decided to add a track listing to it. So, since different cds have different numbers of tracks, I added a loop in there to read in the tracks until it reads in a string that I compare to break it out of the loop. However, everything seems to be working fine except I get a segmenation fault. I tried messing around with cout's and found that everything works fine but it wont print my last cout once its supposed to break out of the loop once it reaches the end of the file, meaning it most not be reaching the end of the file.
I don't get it... Here is the code: CPP / C++ / C Code:
This is the databaseFile.txt: Code:
It wont print the last cout that says After breaking out of reading file. Anyone got any ideas? Last edited by dsmith : 27-Jan-2005 at 06:45.
Reason: Please use [c] & [/c] when posting C code
|
|
#2
|
|||
|
|||
|
Quote:
The art of debugging depends on the ability to use the information that you are given so that you can get to the next step: If it bombs before you print the "after breaking out" message after breaking out of the do{} loop, then it isn't breaking out of the do {}loop. Check for end of file after the getline statement (sometimes your program is not reading what you think it is). (It's just logic, you know.) Also, I added a few more couts to see in greater detail what's happening. Try the following. If it keeps your program from bombing, then you might do this: Comment out the "if(infile.eof()){}" block and see if the cout statements help you see what's going on. CPP / C++ / C Code:
Regards, Dave Last edited by davekw7x : 27-Jan-2005 at 09:06.
|
|
#3
|
|||
|
|||
|
Ok, I tried out your idea just now. I think I may have to play around with the loop a little more because it might be running one more time than I want it to, but it shouldnt be causing the seg fault.
CPP / C++ / C Code:
Ok that's the code I tried to run with more cout's and found out that it did break out of the loop. Here are the results: Code:
So from what the last variables indicate, it should be at the end of the file. So I dont understand why it doesn't break out of the loop and print that last cout at the end of the program. Did you think it wasn't breaking out of the small do/while loop or the big while loop that searches for infile.peek() != eof? Last edited by LuciWiz : 27-Jan-2005 at 14:58.
Reason: Please insert your C code betweem [c] & [/c] tags
|
|
#4
|
|||
|
|||
|
Quote:
Here's how you figure it out: Do you have cout statements at the beginning of the big loop? Do you always test the return value of strtok()? Try something like this: CPP / C++ / C Code:
Note that giving a NULL src string to strcpy will (usually) cause a segfault. Regards, Dave |
|
#5
|
|||
|
|||
|
Ok, I did what you said and here are the results:
For some reason it doesn't read the end of my file, which I dont understand. It goes back to the top of the loop and starts reading in the file, and then segfaults when it tries to strcpy a null string like u said. Here's the top of the loop where it faults: Code:
Here is the output: Code:
Ok so it broke out of the loop without segfaulting, which is good. Also, if I just keep that if statement in the loop, then it will break out without segfaulting, would there be a problem with my program or can I leave it like that. Of course I would like to know the answer to the previous question because I wanna know what's going on and why it is breaking out and why it is not reading the end of my file and trying to read in blank stuff. swayp EDIT: I tried adding this to cout #3 to check it out: Code:
And it gives me the same result. It doesnt print out " test", so the program breaks out (it does NOT seg fault) right before that. That makes no sense to me LOL Help oh wise dave |
|
#6
|
|||
|
|||
|
Quote:
I have resisted the urge to comment on your program, because my main purpose was to get you to the point where you could see how to track down the point of the error. Now, here's the drill: It's sometimes difficult to see how to get a program to work with known good data. When you finally get it to spit out good results, the inclination is to quit the development. (That is, everything works with good input: go on to better things.) It is usually vastly more difficult to see how to put special tests to make sure that the program does not misbehave when presented with data that is different than the programmer had in mind. (You can't prove a program is OK by testing, but you should test as many things as you can.) So: delete or comment out all of the extra cout statements that are cluttering up the program, but leave tests for eof and NULL return values from strtok(), since they prevent bad things from happening (we hope), and also may tell you when data in the input file is not correctly formatted. What if a program has an extra blank line at the end? Does it still work, or does it bomb when going back through a loop? What if the data lines are not exactly what you had in mind. Etc., etc. Here's a couple of general instructions. When using getline(), make sure there is no eof after you get the line. When using strtok(), test the return value to make sure it's not NULL (so you don't try to use that in a strcpy(). Now, totally unrelated to getline(), etc.: When your program obtains storage by using new, it is your responsibility to delete everything before the program exits. If you don't, this is called a "memory leak", and, depending on the operating system, you will find that after running the program lots of times, you may run out of system resources. That's right: sometimes the memory that you obtained and didn't give back will never, ever be available again until you reboot your machine. Of course if your program ends with a segfault, there is no way of knowing whether the memory was returned (probably not would be my guess, but I don't really know). By the way, there is no reason in the world to use dynamic storage allocation at all in this program. You could try CPP / C++ / C Code:
Now, since this doesn't use new you don't have to worry about delete. The storage from these declarations is automatically returned to the operating system when the program exits (even with a segfault). Regards, Dave |
|
#7
|
|||
|
|||
|
Ok sounds good.
This program is just a simple test program I'm running in trying to figure out how to read in this information. Eventually im gonna move the information to the bigger program that actually uses this information. But I understand what you are saying and thanks for your help. |
|
#8
|
|||
|
|||
|
Quote:
I typically spend 80% of programming and testing time on input/output (mostly input). Your decision to make sure that the input is absolutely correct before going on to a grander application is excellent!!! Regards, Dave |
Recent GIDBlog
Developing GUIs with wxPython (Part 2) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| warning cannot open 'extra/browscap.ini' for reading | allelopath | Apache Web Server Forum | 1 | 08-Sep-2004 05:22 |
| Damn RAM problems | mrnobody | Computer Hardware Forum | 2 | 14-Aug-2004 20:29 |
| Chaintech Geforce 5600 FX problems | bartster74 | Computer Hardware Forum | 8 | 04-May-2004 13:16 |
| reading a char* into struct data | spike666 | C Programming Language | 7 | 19-Apr-2004 12:06 |
| Having problems displaying a line of variable length using winio. | warny_maelstrom | C Programming Language | 8 | 15-Feb-2004 11:56 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The