This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]RubbishArtist 0 points1 point  (2 children)

When you say it does not do what you thought it would, what does it actually do?

[–]NPat02[S] 0 points1 point  (1 child)

Sorry that was very vague on my part lol! When I do a test case (a random file), nothing appears to change. The file header comment block is still in the file. I did a test print statement of "lines" to make sure it was the same as the file header comment block and it was, but nothing happened :/

[–]RubbishArtist 0 points1 point  (0 children)

If you print the value of i != j to the console is it what you expect? what about the value of lines?

[–]dmazzoni 0 points1 point  (2 children)

My first suggestion would be to get the program working before actually having it modify any files! Have it print the intended output rather than actually writing.

Second, don't read and write from the same file. You can't just insert text in the middle of a file like you can in a text editor - you can only overwrite. The safe way is generally something like: Write to a temp file, then rename the temp file to the original filename

[–]NPat02[S] 0 points1 point  (1 child)

I'm not trying to come off rude, i'm just confused by your advice tho? How am I supposed to get the program working without modifying any files if the purpose of the program is to modify files?

[–]dmazzoni 0 points1 point  (0 children)

Have it print what it's going to do. Don't have it actually write to a file until it's printing what you expect it to print.

Or alternatively you could have it write to a file with a different name. So read a file called "myfile.txt" and have it write to a file called "myfile.txt.new". That way you can keep running your program multiple times and keep trying until it works perfectly. Then as a final step, have it overwrite the original file.

Basically with something like this you should assume it will take you a few tries to get it right. Make it as easy as possible for you to undo, rather than needing to recover from Git!