J
John Harrison
Comments below.
Why filename, and why is is static? What if file name is longer than 15?
Better and simpler is
ifstream in(argv[1], ios_base::in | ios_base:ut
| ios_base::trunc);
but you should add a check that argv[1] exists (if (argc < 2) etc.)
Unnecessary, C++ streams close themselves when the stream variable no longer
exists.
First thing to do is a seek, you cannot switch from reading to writing
without doing a seek in between. Since you are at the desired position, you
just have to seek to the current position
in.seekp(0, ios_base::cur);
Either. If you need to read before you can tell what to write then read, if
not then start writing now.
If you need to do the former then forget the above code and do this
streampos pos = in.tellg(); // save current position
read the date somehow
in.seekp(pos, ios_base::beg); // go back to saved position
start writing
Absolutely not. If the change you are making requires a change in the number
of characters in the file, then the only way to read the entire contents of
the file into memory, make the change in memory and them write the memory
back out to the file. Do this in three phases
1) open file for reading only, read file
2) make change in memory
3) open file for writing only, write file
C++ streams follow the same basic concepts (and them some) as any other file
I/O system. All the above would be true even if you were using C (but the
syntax would be different obviously).
john
Mike Dundee said:I have a file of the form:
header<newline>
data...delimiter...data...delimiter
purge_date_field...delimiter<newline>
end_of_file_marker<newline>
I want to open the file up and update the purge_date field with a
particular date. Here is most of my code.
int main(int argc, char* argv[])
{
//
// set up vars
//
static char filename[15];
char byte;
int TildeCount = 0;
strcpy(filename, argv[1]);
// open a file for read and write operations
ifstream in(filename, ios_base::in | ios_base:ut
| ios_base::trunc);
Why filename, and why is is static? What if file name is longer than 15?
Better and simpler is
ifstream in(argv[1], ios_base::in | ios_base:ut
| ios_base::trunc);
but you should add a check that argv[1] exists (if (argc < 2) etc.)
while (in.get(byte)) // read char by char until in correct
position in file
{
if(byte == '~'){ // tilde is my delimiter
TildeCount++ ;
}
if(TildeCount == 5){
cout << "reached purge date" << endl; // just my debug code
Sleep(2000);
// change purge date here to new date
in.close(); // debug code until I get this working
Unnecessary, C++ streams close themselves when the stream variable no longer
exists.
return 0;
}
}
in.close();
Ditto.
return 0;
}
How do I go about writing to the file at this position?
First thing to do is a seek, you cannot switch from reading to writing
without doing a seek in between. Since you are at the desired position, you
just have to seek to the current position
in.seekp(0, ios_base::cur);
Can I overwrite chars in the file or do I have to read to a buffer and
modify the buffer prior to writing back to a new file?
Either. If you need to read before you can tell what to write then read, if
not then start writing now.
If you need to do the former then forget the above code and do this
streampos pos = in.tellg(); // save current position
read the date somehow
in.seekp(pos, ios_base::beg); // go back to saved position
start writing
can I delete chars in a file.
Absolutely not. If the change you are making requires a change in the number
of characters in the file, then the only way to read the entire contents of
the file into memory, make the change in memory and them write the memory
back out to the file. Do this in three phases
1) open file for reading only, read file
2) make change in memory
3) open file for writing only, write file
The whole stream thing is confusing me!
C++ streams follow the same basic concepts (and them some) as any other file
I/O system. All the above would be true even if you were using C (but the
syntax would be different obviously).
Thanks in advance for any help.
I am using borland C++ pro ver. 5
Mike Dundee
john