Question:
C++ Problem, fstream replacing/deleting a specific line in a txt file?
Poo Particle Pete
2013-06-08 04:40:38 UTC
Hello,

say i have a file like this;
hello
1
2
goodbye

how could I delete hello? and how could i replace 2 with another int or string?

#include

using namesapce std;

int main()
{
fstream outfile;
outfile.open("file.txt", ios::out| ios::app);

return 0;
}
i'm not sure where to go or how to do it, my text book only helped me thus far.

If you could help me it would be appreciated =)
Four answers:
David
2013-06-09 09:19:35 UTC
This is how you would remove a line. Create a new file for output:



    #include

    #include

    #include

    #include



    struct caller

    {

        bool operator()(std::string const& str) { return str != "hello"; }

    };



    int main()

    {

        std::fstream out("out.txt");

        std::fstream in("in.txt");

        std::copy_if(

            std::istream_iterator< std::string >( out ),

            std::istream_iterator< std::string >(),

            std::ostream_iterator< std::string >( out, "\n" ),

            caller()

        );

    }
wexler
2016-12-11 13:17:01 UTC
Cpp Fstream
2013-06-08 10:52:50 UTC
that was a real good exrcise for me

if you are not fimiliar with the STL this will make no sens

but just try it and save it maybe you will need it later





#include

#include

#include

#include

#include

#include

#include

using namespace std;





//we will need this as a presicate

bool is_not_space(char c)

{

return !isspace(c);

}

//this function reads from a file and out it into a string just they way it is

string read_file_as_it_is( fstream & file,const string &name)

{

string str;

file.open(name,ios::in);

file.seekg(0);



if(file.fail() != true)

getline(file,str,'\0');

else

{

//do somthing print or display or reopen

cout<<"can't open file";

file.clear();

}

file.close();



return str;



}

//this fuction recieves a string and split it then returns it to a vector

vector split_string(const string str)

{

vector ret;

string::const_iterator it=str.begin();

string::const_iterator it1=str.begin();

string::const_iterator itm=str.begin();



it=find_if(it,str.end(),is_not_space);

it1=find_if(it,str.end(),isspace);

ret.push_back(string(itm,it)); //if there is spces

while( it!=str.end() )

{

ret.push_back(string(it,it1));

it=it1;

itm=it1;

it=find_if(it,str.end(),is_not_space);

it1=find_if(it,str.end(),isspace);

ret.push_back(string(itm,it));



}

return ret;

}

//this function replace the first string it finds

void replace_first_str(vector& x,const string& to_replace,

const string& replacement)

{

vector y;

y.push_back(to_replace);

vector::iterator it;

it=search(x.begin(),x.end(),y.begin(),y.end());

if(it!=x.end())

*it=replacement;

}

//this function deletes the first string it finds

void delete_first_str(vector& x,const string& to_delete)

{

vector y;

y.push_back(to_delete);

vector::iterator itb,ite;

ite=itb=search(x.begin(),x.end(),y.begin(),y.end());

if(itb!=x.end())

{

advance(ite,y.size());

x.erase(itb,ite);

}

}

//this function writes a string into a file

void write_to_file(vector x,const string& str)

{

fstream file;

file.open(str,ios::out);

if(file.fail() != true)

{

for(int i=0;i
file<
file.close();

}

else

{

//do somthing here

file.close();

file.clear();

}

}

void main()

{



fstream outfile;

string x;

vector vec;

x=read_file_as_it_is(outfile,"d://file.txt");

vec=split_string(x);

replace_first_str(vec,"2","some value");

delete_first_str(vec,"hello");

write_to_file(vec,"d://file.txt");





system("pause");

}
Bob
2013-06-08 07:47:56 UTC
include

#include

using namespace std;



struct node

{

string data;

node *next;

};



int main()

{

int lines = 0;

string line;

fstream outfile;

outfile.open("file.txt");



struct node *head = NULL;

struct node *cur;



if(outfile.is_open())

{

while(outfile.good())

{

if(!head)

{

head = new node;

head->next = NULL;

getline(outfile, head->data);

}

else

{

struct node *newNode = new node;

cur = head;



while(cur->next)

cur = cur->next;



cur->next = newNode;

newNode->next = NULL;

getline(outfile, newNode->data);

}

}



outfile.close();



FILE *pFile = fopen("file.txt", "w");



if(pFile != NULL)

{

cur = head;



while(cur)

{

if(cur->data.compare("hello") != 0)

{

if(cur->data.compare("2") != 0)

fputs(cur->data.c_str(), pFile);

else

fputs("different string", pFile);



if(cur->next)

fputs("\n", pFile);

}



cur = cur->next;

}



fclose(pFile);

}

else

printf("Unable to open file using fopen()\n\n");

}

else

printf("Unable to open fstream object 'outfile'\n\n");



if(outfile.is_open())

outfile.close();



system("pause");



return 0;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...