Hi,

I wish to output a string from a textbox to another file, here is what I wrote:

FILE*myfile;
 myfile=fopen("D:\\Profiles\\DqGw47\\Desktop\\hello.txt","a");
 fprintf(myfile,"%s\n",textbox1->text);
 fclose(myfile);

In my hello.txt:
Set wallpaper=


My problem is how can I control where to fprintf my string. In this example, how can I output the textbox text to file hello.txt so that the file become :Set wallpaper= "...." ,the ... is text from the textbox.
So can anyfren tell me how to control string output to that location?

Recommended Answers

All 4 Replies

I am not sure I understand what you want. Maybe, the following code can help you. I write it using C++, you can translate it to C. This code can replace the text Set wallpaper="aaaa" with Set wallpaper="text"

int main(int argc, char *argv[])
{
  string text = "text";
  string str;
  vector<string> fileContent;
  
  ifstream infile("hello.txt");
  //infile.getline(str);
  while ( getline(infile, str) )
  {
    cout<<str<<endl;
    if ( str.find("Set wallpaper=") != string::npos )
    {
      str = "Set wallpaper=\"";
      str += text;
      str += "\"";
    }
    fileContent.push_back(str);    
  }
  infile.close();
  
  ofstream outfile("hello.txt");
  for (int i=0; i<fileContent.size(); ++i)
  {
    outfile<<fileContent[i]<<endl;
  }
  outfile.close();   
  
  system("PAUSE");
  return EXIT_SUCCESS;
}

Here's how to do it. fprintf(myfile,"Set wallpaper= \"%s\"\n",textbox1->text);

Here's how to do it. fprintf(myfile,"Set wallpaper= \"%s\"\n",textbox1->text);

I do not think this is what he want. I think his condition is like that:
there are a lot of line in file hello.txt , like
Set wallpaper=
Set color=
Set ....=.....
what he want is find out the line contain "Set wallpaper" and replace it with Set wallpaper="the text from the textbox"

>>So can anyfren tell me how to control string output to that location?
If you want to change the text in the file itself, then you will have to rewrite the entire file. When you come across the line "Set wallpaper" then write out the new text into the new file.
Here is some pseudocode for you to use.

open original file
open new output temp file

for each row in input file
   call fgets() to read the line
   check for "Set wallpaper"
   if check is true then write new text to output file
   otherwise if check is false then just write the entire row to output file
end of while loop
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.