I've created a program that reads lines from the text file. Each line is parsed to find a command and parameters that are executed. So, the essential code goes something like this:

while (getline(script, scriptline))
Parse Scriptline
Execture Scriptline Function

This works very well for most functions. Until I get to one that creates and writes to several files. That function operates just fine, but when it ends and control returns to the above loop, scriptline is read but sometimes no longer contains a full line of data. I've had situations where it should have read a line that looks like this:

FunctionalAlignment3, Tal-Cal, XYZ, base

but somehow read it so that it looked like this:

Functiona Tal-Cal, XYZ, base

For some reason, it just skips over the central part of the text line and seems to replace its content with spaces.

This only occurs when the script calls a function that creates and writes to five separate files. That function creates, opens and closes each file in turn. So, at best, only two files are open at one time (the file that contains the scriptline and the new file that is being created).

I've tried my scriptloop to read the next line (adding an additional getline to skip over the one with the problem) and that line seems to read just fine (at least so far). So, I've tried rewriting the scipt, in case some type of control character got into my text on the problem line. That did not help.

I should also point out that the function in question works sometimes. I can create a script that contains the same lines five times. It runs properly for three and then only partially reads the fouth line of the script.

I thought this might be a memory leak problem in the function that is called by the script. I've made sure that for every use of the word "new" there is a use of the word "delete". So, if it is a memory leak, I do not know how to find it.

So, any suggestions about what may be going wrong here?
Suggestions on where to look for possible solutions would be welcome also.

Thanks.

Recommended Answers

All 9 Replies

It's pretty hard to say where the problem could be without seeing the code in question.

I understand that. But, I have over 1000 lines of code that are involved here, and I do not think anyone would take the time to look through all of it. That is why my question took the form "where would you look?"

If it helps, I have developed what seems to be a successful workaround. I read in the entire script as a vector of strings. Then I loop through the vector, parsing each string and executing the funciton commands on that string. This way, I do not have a file open during the entire program that the program only returns to, and reads from, after executing a series of other functions.

So far, this solution seems to be working for me. However, it does not explain why I was having the problem in the first place.

Again, the bottom line problem: What would cause a program to read a line as a string, but only read part of its contents?

Something like, 1234 5678 9101112 is read as 1234 9101112

What would cause a getline function to return a string like that???

You mean if you load the file content and save in the program memory (variables), then it works fine?

Well, my guess in your program is that the way you parse String from the file. How do you parse string? If a string doesn't match, what would you do? Skip? That may be where you need to look at.

It's pretty hard to say where the problem could be
without seeing the code IN QUESTION.

If you will carefully re-read ...
@tinstaafl

It's pretty hard to say where the problem could be
without seeing the code IN QUESTION.

You might want to carefully re-read

I understand that. But, I have over 1000 lines of code that are involved here, and I do not think anyone would take the time to look through all of it. That is why my question took the form "where would you look?"

If you do not have a helpful comment, may I suggest that your best option is to remain silent. @David W

To re-iterate, I understand that examining the code would be the ideal option. However, the code in question is over 1000 lines long and spread over six different files. Is anyone really going to pick their way through all that? This is why my questions were "where would you look?" and "what would cause something like that?"

I know that problem is not where I parse the string, because I am reproducing the problem when the code merely reads (attempts to read) and writes the string. No parsing involved to cause the problem. @Taywin

Hmm... I tell you to take a look at parsing part because it is a normal behavior when the read (input) is not the same as write (output) when dealing with string from a source file. Could there be any special characters or formats when the string is being parsed? Just a thought.

I realize your reluctance to submit a large set of source files, however, without that, any suggestions are going to be merely guesses and probably won't of any real help at all. Also, debugging the code won't necessarily mean examining all the code, just the relevant parts. The rest of the code is needed to get it to compile and reproduce the error to find what's causing it.

Hey @tgreiner

Sorry about the duplicate post ... I did not notice that duplicate until just now ...

If you will check my posts you will see that they are all meant to be instructive ...

It will be good for you to isolate the code (do you need some debugging tips?) where you think the problem lays ...

Then code a test program to run that isolated code to verify that.

Then if you still cannot find/fix the bug ... all you need to do is show us that much-reduced code snippet.

Please understand that that was what was intended by bolding the words ...

the code IN QUESTION

Shalom,
David

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.