I have a dialog based application.I use a text box in it.what i want is write the details from that text box into a text file.
when i wrote the code ,the data written into the file was as follows..慨獮a﷽﷽楢畮ﴀ﷽ý晧晧ﴀ﷽ý
My code is,
CString str;
GetDlgItemText(IDC_EDIT1,str);
fstream f;
f.open("C:\\myfile.txt",ios::app);
f.write((LPCTSTR)str,10);
f.close();

Any error...???if so please help me to correct it....i use vc++

yes,now that works.
what if i want to write the new value in the edit box in newline.now all the data are written in single line.

i want to delete a particular row from the file.i have three values written in the single line.
i have to select a value from a combo box where one of the values was written at the time of writing it to the file.how could i accomplish this?ie,how could i delete the line with the selected item from the combo box?i use vc++

i want to delete a particular row from the file.i have three values written in the single line.
i have to select a value from a combo box where one of the values was written at the time of writing it to the file.how could i accomplish this?ie,how could i delete the line with the selected item from the combo box?i use vc++

Open a new file for output, into which you write all the lines except for the one that contains the value you wish to delete. Then eventually replace the original file with the new file.

please i dont know how to get it work.the value to be deleted is selected from a combo box.that particular row should be removed.please help.

Here's the logic:

while (lines in files)
{
  get a line
  if it [B]isn't[/B] the line to be deleted
  {
     write the line in the new file
  }
}

If you don't understand something, tell us which part you don't get. Don't say "I don't get it to work".

I have to compare the third value in each line.it will be a string(eg:name).
this name will be selected from a combo box.this has to be compared with the file contents and then deletion performed.

If you're expecting that I'm going to write the code for you: You're wrong.

I've given you a push in the right direction, now try something yourself and come back with code and a specific question

I have to compare the third value in each line.it will be a string(eg:name).
this name will be selected from a combo box.this has to be compared with the file contents and then deletion performed.

You can probably use stringstream to split an input line into three parts.

I have written the following code..

[COLOR="Green"]CString str="";


    GetDlgItemText(IDC_EDIT1,str);
    fstream f,f2;
    f.open("C:\\myfile.txt",ios::app);
    f.write(str.GetBuffer(str.GetLength()),str.GetLength()); 
    f<<endl;
    f.close();


    f2.open("C:\\myfle2.txt",ios::app);
    f.open("C:\\myfile.txt",ios::in);
    f.seekg(0,ios::beg);
    while(!EOF)
    {
        f.read(str.GetBuffer(str.GetLength()),str.GetLength());
        f2.write(str.GetBuffer(str.GetLength()),str.GetLength());
    }
    f.close();
f2.close();


    }[/COLOR]

Please check.

Edited 3 Years Ago by happygeek: fixed formatting

What does your inputfile look like?

Strongly simplified, I would do something like:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    string line; 
    string ToFind = "the string to exclude from output";
    ifstream in("C:\\input.txt");
    ofstream out("c:\\output.txt");
    while (getline(in,line))  //get one line
    {
        /*
        'line' is the complete line from the file
        But I don't know what your line looks like
        so I can't break it in pieces yet.
        That's something you should add here
        */
        if (line != ToFind)
            out << line << "\n";
    }
    in.close();
    out.close();
    return 0;
}

To break the line in pieces, you could use stringstreams as mitrmkar suggested

Ok, so you use my previous example to read the line from the file in a string, and then use a stringstream to break it up in pieces.
Here's an example that uses Stringstreams:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    string line= "192.168.1.127 12 office"; 
    stringstream ln(line);
    string ip, no, name;
    ln >> ip >> no >> name;
    cout << "name: " << name << "\nno: " << no << "\nip: " << ip ;
    return 0;
}

it gives an error while using vc++:
'stringstream' : undeclared identifier

Then you used different code then what I posted. The code I posted compiles fine on VS.
Please post your code.

string line= "192.168.1.127 12 office"; 
    stringstream ln(line);
    string ip, no, name;
    ln >> ip >> no >> name;

    CString ToFind = "the string to exclude from output";   
    ifstream in("C:\\myfile.txt");  
    ofstream out("C:\\output.txt"); 
    while (getline(in,line)) 
    {

        if (line != ToFind)           
            out << line << "\n";
    }

Edited 3 Years Ago by Dani: Formatting fixed

USE CODE TAGS when posting code.

You messed up all the code given to you in previous posts... I thought my examples were clear enough...

I'll give you a combination of the previous examples, to clear things up.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    string line; 
    string ToFind = "office";
    ifstream in("C:\\input.txt");
    ofstream out("c:\\output.txt");
    while (getline(in,line))  //get one line
    {
        stringstream ln(line);
        string ip, no, name;
        ln >> ip >> no >> name;
        if (name != ToFind)
            out << line << "\n";
        else
            cout << "Discarded line: " << ip << no << name << " (not in outputfile)"
    }
    in.close();
    out.close();
    return 0;
}

This code will
- open a file (input.txt).
- read the file 1 line at a time until the end is reached.( while (getline(in,line)) )
- break each line in 3 strings ( ln >> ip >> no >> name; )
- look if 'name' == ToFind ( the word 'office' in this example)
- if the name is not office, then write it to the ouputfile.

So the outputfile will be identical to the inputfile, with the exception that all the lines with the name 'office' are deleted.

I wrote the same code as u provided.but had to change string to char[], instead of != had to use strcmp
i also included sstream ...but still i get some errors .my code and error messages are as follows....

char line[100];
char tofind[]="office";
ifstream in("C:\\myfile.txt");
ofstream out("C:\\myfile2.txt");

while(getline(in,line))
{
    stringstream ln(line);
    char ip[15],no[2],name[25];
    ln>>ip>>no>>name;
    if(strcmp(name,tofind)!=0)
        out<<line<<"\n";
    else
        cout<<"deleted"<<"\n";
}
in.close();
out.close();

errors:

error C2065: 'getline' : undeclared identifier
error C2065: 'stringstream' : undeclared identifier
 error C2146: syntax error : missing ';' before identifier 'ln'
 error C2065: 'ln' : undeclared identifier
error C2297: '>>' : illegal, right operand has type 'char [15]'

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.