0

What I need to do is read some parameters values from an input file in which comments are identified by putting * at the beginning of the line. The values in the "useful" lines are separated by semicolons like this:

10; 32541615; 0.18; 0.45; 0.00015; 0.01485; 0.03;

and I have multiple lines like this in my input file.

What I would like to do is insert a * at the beginning of each line once I've read it in such a way that if I read the file a second time I will skip that line and go directly to the following one.

I need this because my objective is to have multiple instances of my program running at the same time and accessing the file in sequence in order to get the input parameters they need. So I want that each instance will get different parameters.

What I thought to do is inserting a dummy character at the beginning of each line, like this:

b10; 32541615; 0.18; 0.45; 0.00015; 0.01485; 0.03;

and then replace it (b in this example) with * when I read it so that the second time I read the line it would be treated as a comment.

I tried to use put('*') and << '*' once I read b with a peek() call but the * character is always appended at the end of the file. I've read that although I can't write in the middle of a file I can overwrite in the middle of a file. What can I do?

Here is an example of a possible input file:

* FORMAT:
* MAX_HEIGHT; SEED; p0; p1; pd; pp; epsilon;

b10; 32541615; 0.18; 0.45; 0.00015; 0.01485; 0.03;
b40; 32541615; 0.18; 0; 0.00015; 0.01485; 0.03;
jackscorrow
  • 682
  • 1
  • 9
  • 27
  • dont you need to know what instance of you program uses what parameters? I would maybe use one parameterfile for each instance and pass the file name as command line arguement. Or if you insist on a single file then I would make each instance read its parameters from a given line in the file (again passed as command line arguement). Imho having several instances of your program reading and writing to the same file is just unnecessary overcomplication – 463035818_is_not_an_ai Apr 12 '17 at 13:48
  • A fundamental problem is that without some additional synchronization you won't be able to prevent a race condition where multiple instances of the program read the same line before any of them get a change to update the line. If/when you solve that problem then you have the added benefit of being able to solve your original problem in a cleaner way. – Michael Burr Apr 12 '17 at 14:38
  • @MichaelBurr I know that. Of course I'm going to implement some sort of synchronization before actually having multiple threads reading from the same file at once. But I was curious to know if this problem had a simple answer – jackscorrow Apr 12 '17 at 14:50

1 Answers1

1

So you want to modify the file that you're reading? Then you basically have to rewrite it, from scratch.

One common way to do it is by reading the whole file into memory, modify the in-memory buffer, then overwrite the file with the (modified) in-memory buffer.

Another common way, if the file is to big to fit in memory, is to read line by line, modify the line, and write it to a new temporary file. Then when all of the input has been read you rename the temporary file as the actual file, thereby replacing the data in it.

Both of these solutions can be used without you needing any special marker-character like the b at the beginning of the line to be replaced.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • But what I want to do is replace a `char` with another `char` that will occupy the same space. Do I still need to write the whole file from scratch? – jackscorrow Apr 12 '17 at 13:56
  • @jackscorrow You have to *seek* through the file to find the correct positions where to replace the special character. It could actually be more work than the possible methods I told about in my answer. Also, with my proposed possible solutions you don't have to have the special character, making it easier to read the first number (you don't have to read it as a string, remove the first character, and the convert the string to an integer value). – Some programmer dude Apr 12 '17 at 13:59
  • Ok. I think I'll go with this in the end. But you said _read it as a string, remove the first character, and the convert the string to an integer value_, excuse my ignorance but are there any other ways to read integer or double numbers? Because those are exactly the steps I'm following in order to store this kind of values – jackscorrow Apr 12 '17 at 14:32
  • @jackscorrow Read directly as an `int` or `double` with the `>>` operator? Like `int i; some_stream >> i;`? – Some programmer dude Apr 12 '17 at 14:36
  • @jackscorrow I don't mean to sound mean or anything, but it seems you might need to [find a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start over with how input works. – Some programmer dude Apr 12 '17 at 14:55
  • Don't worry, I understand. My university never taught me this kind of things or how C++ works. I'm trying to learn as much as I can by myself. Thank you for the advice, I'll try to read that book if I can – jackscorrow Apr 13 '17 at 13:44