How to control where to fprintf string?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 42
Reputation: Adrian99420 is an unknown quantity at this point 
Solved Threads: 0
Adrian99420 Adrian99420 is offline Offline
Light Poster

How to control where to fprintf string?

 
0
  #1
May 5th, 2008
Hi,

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

  1. FILE*myfile;
  2. myfile=fopen("D:\\Profiles\\DqGw47\\Desktop\\hello.txt","a");
  3. fprintf(myfile,"%s\n",textbox1->text);
  4. 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?
Last edited by Ancient Dragon; May 5th, 2008 at 7:02 am. Reason: disabled smilies
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

Re: How to control where to fprintf string?

 
0
  #2
May 5th, 2008
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"
  1. int main(int argc, char *argv[])
  2. {
  3. string text = "text";
  4. string str;
  5. vector<string> fileContent;
  6.  
  7. ifstream infile("hello.txt");
  8. //infile.getline(str);
  9. while ( getline(infile, str) )
  10. {
  11. cout<<str<<endl;
  12. if ( str.find("Set wallpaper=") != string::npos )
  13. {
  14. str = "Set wallpaper=\"";
  15. str += text;
  16. str += "\"";
  17. }
  18. fileContent.push_back(str);
  19. }
  20. infile.close();
  21.  
  22. ofstream outfile("hello.txt");
  23. for (int i=0; i<fileContent.size(); ++i)
  24. {
  25. outfile<<fileContent[i]<<endl;
  26. }
  27. outfile.close();
  28.  
  29. system("PAUSE");
  30. return EXIT_SUCCESS;
  31. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,473
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to control where to fprintf string?

 
0
  #3
May 5th, 2008
Here's how to do it.
fprintf(myfile,"Set wallpaper= \"%s\"\n",textbox1->text);
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

Re: How to control where to fprintf string?

 
0
  #4
May 5th, 2008
Originally Posted by Ancient Dragon View Post
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"
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,473
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to control where to fprintf string?

 
0
  #5
May 5th, 2008
>>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.
  1. open original file
  2. open new output temp file
  3.  
  4. for each row in input file
  5. call fgets() to read the line
  6. check for "Set wallpaper"
  7. if check is true then write new text to output file
  8. otherwise if check is false then just write the entire row to output file
  9. end of while loop
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC