File Handling

Read the file
Write in to file
Edit/Append file
Hi firends,
I want delete last line from one text file.
Please help me in this....thnx
Open the file for reading. Read in all of the file except the last line. Close the file. Open the file for output (truncating the file.) Write your data to the file.
thanx cire for ur reply.
but is it a efficient way?
if tf file contains 1000 of line....
> but is it a efficient way?
> if tf file contains 1000 of line....

Yes. A few thousand lines is nothing; you wouldn't even notice the elapsed time.

If the file is a few dozen megabytes in size, something like this (Unix, Linux, Cygwin, GNUWin32):
1
2
3
4
5
6
7
8
// delete last line from file foo.txt
std::system( "/usr/bin/head -n -1 foo.txt > /tmp/temp_foo.txt && mv /tmp/temp_foo.txt  foo.txt" ) ;

// delete last 5 lines from file foo.txt
std::system( "/usr/bin/head -n -5 foo.txt > /tmp/temp_foo.txt && mv /tmp/temp_foo.txt  foo.txt" ) ;

// delete first 7 lines from file foo.txt
std::system( "/usr/bin/tail -n -7 foo.txt > /tmp/temp_foo.txt && mv /tmp/temp_foo.txt  foo.txt" ) ;


If the file is really huge, you might want to take the
stat - seek and read last chunk of file - determine the offset of last line(s) - ftruncate()
(GetFileSize - seek, read - SetEndOfFile on Windows)
route.
actually m trying to read/write/append file by using fstream and string class in cpp...

Ex. XML file look like-
<ServerList>
<Name> </Name>
<URL> </URL>
...
</ServerList>

So read and write i done but while append, I just want to remove last line
i.e. </ServerList>
then append new server info and again at the end have to put that line - </ServerList>.
How big can an xml file get? A few thousand lines? So do what cire suggested.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

std::vector<std::string> read_lines( const std::string& path_to_file )
{
    std::vector<std::string> contents ;

    std::ifstream file(path_to_file) ;

    std::string line ;
    while( std::getline( file, line ) ) contents.push_back(line) ;

    return contents ;
}

void write_lines( const std::string& path_to_file, const std::vector<std::string>& lines )
{
    std::ofstream file(path_to_file) ;

    for( const std::string& ln : lines ) file << ln << '\n' ;
}

int main()
{
    const std::string path_to_file = "my_file.xml" ;

    // read the lines in the file file
    auto lines = read_lines(path_to_file) ;

    // knock off the last line
    std::string last_line = "</ServerList>" ;
    if( !lines.empty() )
    {
        last_line = lines.back() ;
        lines.pop_back() ;
    }

    // append more lines
    lines.push_back( "    <Name> Google Advanced Search </Name>" ) ;
    lines.push_back( "        <URL> http://www.google.com/advanced_search </URL>" ) ;

    // put back the last line
    lines.push_back(last_line) ;

    // write the lines to the file
    write_lines( path_to_file, lines ) ;
}


I don't think you want to delete the last line of an XML file...

1
2
3
4
5
6
7
<!-- Edited by XMLSpy -->
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note> 

Becomes:
1
2
3
4
5
6
<!-- Edited by XMLSpy -->
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body> 

Which is a totally invalid file.
> I don't think you want to delete the last line of an XML file...
> ...
> Which is a totally invalid file.

It is clear that ak16 does not want to just delete the last line of an XML file.
And no one else has stated that the last line of an XML file should be deleted.

What was wanted was very clearly stated by ak16:
So read and write i done but while append, I just want to remove last line i.e. </ServerList>
then append new server info and again at the end have to put that line - </ServerList>.

Which, broken down into steps is:
1
2
3
a. remove last line i.e. </ServerList>
b. append new server info 
c. and at the end put that line - </ServerList>


Also made quite clear from the comments in the posted code:
1
2
3
4
5
// knock off the last line
.....
// append more lines
....
// put back the last line 

very Thanx you both guys for reply.

Sorry Stewbond but JLBorges is absolutely right interpret my problem.

JLBorges, What you suggest is right solution and its working well Because I tried it previously .

But i want something, by using this I can delete last line from file without rewriting whole data from LIST/VECTOR....



> But i want something, by using this I can delete last line from file
> without rewriting whole data from LIST/VECTOR....

Why do you want it? How many lines are there in the file? Have you measured the time? If yes, did you encounter a performance issue?
But i want something, by using this I can delete last line from file?

is it possible without our above solution?
Topic archived. No new replies allowed.