| | |
How to control where to fprintf string?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 42
Reputation:
Solved Threads: 0
Hi,
I wish to output a string from a textbox to another file, here is what I wrote:
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?
I wish to output a string from a textbox to another file, here is what I wrote:
C++ Syntax (Toggle Plain Text)
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?
Last edited by Ancient Dragon; May 5th, 2008 at 7:02 am. Reason: disabled smilies
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 6
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" C++ Syntax (Toggle Plain 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; }
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 6
•
•
•
•
Here's how to do it.
fprintf(myfile,"Set wallpaper= \"%s\"\n",textbox1->text);
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.
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.
C++ Syntax (Toggle Plain Text)
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
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.
![]() |
Similar Threads
- data sorts in reverse order!! help!! (C)
- QuickSort Still Not Working?? (C)
- Why is qsort not initialised here!!?? (C)
Other Threads in the C++ Forum
- Previous Thread: Selection sort from read file
- Next Thread: help/feedback on my program with structs
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






