Computer Science Canada keeping format of text file? |
Author: | Justin_ [ Thu Aug 17, 2006 1:24 am ] |
Post subject: | keeping format of text file? |
How does one edit a text file programmatically (replace some things in it) while keeping all the formatters in place. i.e. the file looks the exact same as before. Man, I am outputting to the file with copy but that is bad, bad bad. It is a source text file and it needs to be in a shape to compile. |
Author: | wtd [ Thu Aug 17, 2006 11:20 am ] |
Post subject: | |
Don't blame the method of output. Show us how you are editing it. Providing a tiny amount of information about the problem is not the best way to get useful suggestions. |
Author: | Justin_ [ Thu Aug 17, 2006 1:53 pm ] | ||
Post subject: | |||
Again I appologize for however bad my code is, it is the best I know how to make it. Also, does someone know why using fileVector.end() crashes the program at run time? (It clearly is going past the scope of the vector I think) |
Author: | Justin_ [ Thu Aug 17, 2006 1:59 pm ] | ||
Post subject: | |||
This would be the relevant part of the code. I tried (out_file, "\n") but that is not compile worthy either. Any suggestions? |
Author: | wtd [ Thu Aug 17, 2006 5:30 pm ] |
Post subject: | |
Well, it doesn't appear that you're including the algorithm header, or using namespace std. |
Author: | Justin_ [ Thu Aug 17, 2006 7:49 pm ] |
Post subject: | |
Why does it compile then if I need to those? |
Author: | Justin_ [ Thu Aug 17, 2006 8:05 pm ] |
Post subject: | |
I didn't think it would be that complicated. I understand that it would be easier to use a script, but still, it shouldn't be that complicated. Does anyone recall opening a file and reading the contents? Do they remember storing the contents in a variable? Do they remember editing it a little and then putting the contents back into the file? Perhaps php and perl have scewed my perception a little. With them reading a file into a variable is a piece of cake because it magically keeps it formatted as well. |
Author: | Justin_ [ Thu Aug 17, 2006 8:31 pm ] |
Post subject: | |
Okay I found a way... Yet again, C-Style arrays to the rescue. wtd, might you know of a way to maintain c++ standards compliability and complete this task? |
Author: | Justin_ [ Thu Aug 17, 2006 8:32 pm ] | ||
Post subject: | |||
|
Author: | wtd [ Thu Aug 17, 2006 9:22 pm ] | ||
Post subject: | |||
A few changes:
I'll see if I can write an example of how to read lines from a file into a vector using copy. It's been awhile, so I'm a bit fuzzy on the hoops I have to jump through. ![]() |
Author: | Justin_ [ Thu Aug 17, 2006 10:58 pm ] |
Post subject: | |
file.eof() should be: input_file.eof() Yes that way is much better. It even saved me! I was converting the char array to a string in my replacer function and after doing some comparisons, it was really wierd actually, it would cause the program to hang or create a memory error. The reason this was wierd is because after the first compare everything was smooth. If a second comparison matched it caused it to stop a few lines after the comparison and generate no error. If three comparisions matched it caused a memory error and the program would only get a few lines. Wierd huh? How would you explain that? The form of comparison I used was: if (str == " a line in the program"). When I changed to std::getline the problem was resolved... I have no other guess but to assume the problem was in the implicit conversion between string and char. |
Author: | wtd [ Thu Aug 17, 2006 11:06 pm ] |
Post subject: | |
When at all possible, using std::string is much nicer than using char arrays. |